Designing an RPG character class where they have certain attributes.
import java.util.Random;
public class Character
{
private String name;
private int strength;
private int dexterity;
private int intelligence;
private int hp;
private Random randomiser;
private int stat = 16;
public Character(String name)
{
this.name = name;
randomiser = new Random();
//random stats between 3 and 18
strength = 3 + randomiser.nextInt(stat);
dexterity = 3 + randomiser.nextInt(stat);
intelligence = 3 + randomiser.nextInt(stat);
hp = strength;
}
public String getName()
{
return name;
}
...
//Does damage to the character's hp
public void gotHit(int dmg)
{
hp = hp - dmg;
}
//checks if hp is less than or equal to 0, returns true if dead; false if not dead.
//resets hp back to 0
public boolean isDead()
{
if(hp < 0)
{
System.out.print("Character is dead");
hp = 0;
return true;
}
else
{
return false;
}
}
public int getHp()
{
return hp;
}
public String toString()
{
String info = (name + ", Strength: " + strength + ", Dexterity: " + dexterity
+ ", Intelligence: " + intelligence + ", Hit Points: " + hp );
return info;
}
driver
...
Character Chicken = new Character("Chicken");
Chicken.gotHit(100);
System.out.println(Chicken.toString());
...
The problem is that when I do "damage" it subtracts it from hp, but it's not checked by the isDead() method when it's printed in the toString() method. So I get a negative number instead of 0 being printed.
Hope you can help.

New Topic/Question
Reply




MultiQuote







|