A rough UML of the files at the bottom of the page:
| Being | Goblin (inherits from Being) | Game |
| int skills[] name type |
Being me |
|
| Being getType setName points updateSkills getSING_TO_SLEEP getFIREBALL getLIGHTNING getRUN_AWAY getSHOOT_ARROW getSkills fight |
Goblin | game |
Your task is to finish the game. Some people have evolved this assignment into their final game ("The Bread and Eggs escape from the breakfast table" and a court case game come to mind). If you would like to do this, you will likely need to rethink the Being class a bit and plan out your story line.
| Level | Criteria |
| 1 | Added a second monster |
| 2 | Added three monsters (now a total of 4 monsters) |
| 3 | You die or lose the game when all of your points are gone. |
| 4 | New scenario: skills are changed or monsters are different or whatever. |
| 4+ | The monsters (or tasks) are in an array, which you loop through. They can also be in a Queue or a List. |
public class game
{
public static void main (String args[])
{
new game ();
}
public game ()
{
Goblin joe = new Goblin ();
Being me = new Being ();
System.out.println ("Welcome. \nPlease enter your name");
String name = IBIO.inputString (">>");
System.out.println ("You are walking through a forest. You meet a \n");
System.out.println (joe.getType () + ". It is not happy to see you.\n");
System.out.println ("What do you do? \n");
System.out.println ("(1) Start singing. \n");
System.out.println ("(2) Throw a fireball. \n");
System.out.println ("(3) Strike him with lightning. \n");
System.out.println ("(4) Run away. \n");
System.out.println ("(5) Shoot an arrow at him. \n");
int choice = IBIO.inputInt (">>");
int results = me.fight (choice - 1, joe.getSkills (choice - 1));
if (results == 1)
{
System.out.println ("You won! You have two more points. \n");
me.updateSkills (choice - 1, 2);
}
else if (results == -1)
{
System.out.println ("You lose! You have no points left in that category.
\n");
int i = me.getSkills (choice - 1);
me.updateSkills (choice - 1, i);
}
else
{
System.out.println ("The " + joe.getType () + " walks away.\n");
}
//Add in the next monster here
}
}
public class Goblin extends Being
{
public Goblin ()
{
skills [0] = 0;
skills [1] = 0;
skills [2] = 2;
skills [3] = 8;
skills [4] = 9;
name="Slimey";
type="Goblin";
}
}
public class Being
{
public static final int SING_TO_SLEEP = 0;
public static final int FIREBALL = 1;
public static final int LIGHTNING = 2;
public static final int RUN_AWAY = 3;
public static final int SHOOT_ARROW = 4;
protected int skills[] = new int [5];
protected String name;
protected String type;
public Being ()
{
for (int i = 0 ; i < 5 ; i++)
{
skills [i] = 2;
}
name = "Sarah";
type = "Human";
}
public String getType ()
{
return type;
}
public void setName (String n)
{
name = n;
}
public int points ()
{
int sum = 0;
for (int i = 0 ; i < 5 ; i++)
{
sum += skills [i];
}
return sum;
}
public void updateSkills (int index, int amount)
{ //use static finals above for index
if (index >= 1 && index <= 5)
skills [index] += amount;
}
public int getSING_TO_SLEEP ()
{
return skills [0];
}
public int getFIREBALL ()
{
return skills [1];
}
public int getLIGHTNING ()
{
return skills [2];
}
public int getRUN_AWAY ()
{
return skills [3];
}
public int getSHOOT_ARROW ()
{
return skills [4];
}
public int getSkills (int fightType)
{
if (fightType >= 1 && fightType <= 5)
return skills [fightType];
return -2;
}
public int fight (int fightType, int opponentSkills)
{ //1 is you win, 0 is tie, -1 is you loose, -2 is error
if (fightType >= 1 && fightType <= 5)
{
if (skills [fightType] > opponentSkills)
return 1;
else if (skills [fightType] == opponentSkills)
return 0;
else
return -1;
}
else
return -2;
}
}