/*
This lab exercise was downloaded from a page which i forgot. my apology. The downloaded file was edited by the programmer.
HARRY POTTER & THE AMAZING WIZARD.
Declare and initialize a 20-element array. In each element place one of the following characters:
‘W’ – Wizard
‘S’ — Sword
‘D’ — Demon
‘B’ — Blank – Save spot.
Example an array can look like this:
W S S D B D W B B D W S W D B D S W B S
You ask the player to select a location in Hogarth Tower. (Choose a number from 0 – 19) That places the player at a position in the array. In the example if the user enters a 4 as a starting position (starting at 0). So, the entry at that position is a B (Blank).
Based on the rules below the player can then move North (add 1 to the array index) or South (subtract 1 from the array index). Here are the rules:
(1) For each Blank (B) spot, output the message ‘Safe Place’; 2 points is awarded.
(2) For each Wizard spot, output the message ‘You picked up a Wizard’; 3 Points is Awarded.
(3) For each Sword (S) spot, output the message ‘You picked up a Sword’; 4 Points is awarded.
(4) For each Demon (D) encountered, display the message ‘A demon has attacked’; subtract 5 points.
The game is over when (a) the player types in 99 this ends the game. Print out the accumulated points. Or (b) when the points are below 0 – the player is killed. Display ‘You are killed’ if the point garnered by the player is below zero.
Make sure the player does not go beyond the array boundary. The boundary has is limited by the size of the array 19 elements.
*/
/*programmed by: iana mel – school of information technology, dwcl*/
import javax.swing.JOptionPane;
class harryPot
{
static int score(char c)
{
switch(c)
{
case 66: // ‘B’
JOptionPane.showMessageDialog(null,”SAFE PLACE. +2 pts”);
return 2;
case 87: // ‘W’
JOptionPane.showMessageDialog(null,”YOU PICKED UP A WIZARD. +3 pts”);
return 3;
case 83: // ‘S’
JOptionPane.showMessageDialog(null,”YOU PICKED UP A SWORD. +4 pts”);
return 4;
case 68: // ‘D’
JOptionPane.showMessageDialog(null,”A DEMON HAS ATTACKED. -5 pts”);
return 5;
}
return 0;
}
public static void main(String args[])
{
char ac[] = { ‘W’, ‘S’, ‘S’, ‘D’, ‘B’, ‘D’, ‘W’, ‘B’, ‘B’, ‘D’,
‘W’, ‘S’, ‘W’, ‘D’, ‘B’, ‘D’, ‘S’, ‘W’, ‘B’, ‘S’};
int i = 0, j=0, k, l;
String s = JOptionPane.showInputDialog(“Choose a number from 0 – 19″);
k = Integer.parseInt(s);
if(k < 0 || k > 19)
JOptionPane.showMessageDialog(null,”INVALID POSITION. \nPROGRAM RUN TERMINATED.”);
else
do
{
if(k<0)
{
JOptionPane.showMessageDialog(null,”You’re already at index 0. \nYou can’t move South anymore.”);
k=0;
}
else if(k>19)
{
JOptionPane.showMessageDialog(null,”You’re already at index 19. \nYou can’t move North anymore.”);
k=19;
}
else
{
l = score(ac[k]);
if(l < 5)
i += l;
else
i -= l;
if(i < 1)
{
JOptionPane.showMessageDialog(null,”YOU ARE KILLED!”);
break;
}
}
s =JOptionPane.showInputDialog(“Score: ” + i + “\n\n[1] north \n[2] south \n[99]exit \n\n Choose : “);
j = Integer.parseInt(s);
if(j == 1)
k++;
else if(j == 2)
k–;
} while(j != 99);
if(i > 0)
JOptionPane.showMessageDialog(null,”TOTAL SCORE = ” + i);
}
}
OUTPUT: