Java game classes: Hero vs Monsters

1.7k Views Asked by At

Hello I want to solve this problem:

You are making a role playing game. First, you'll need a hero whose role to play. As in all RPGs the hero has health points (life), attack strength and defense. He also has experience points, and a name, given at birth. No RPG is fun with the hero only wandering around. They need some monsters to fight. Not all monsters are the same, some hit you harder, others take more damage at once (read: they have variable attack strength and defense). While mosters have no name, they all belong to a specific race that is known. They also have no experience points (since they are meant to be defeated). Different monster races have different magical skills which may enhance their attack or defense

• What actions are common to the hero and the monsters? Group them together! • Make a Game class with a main method to test your characters. Make them attack each other, and see how they lose HP! For simplicity, you may consider that on each attack the target loses an amount of HP equal to the attacker's strength.

OUTPUT:

Created hero Chuck. HP = 100, ATK = 10
Created monster of race Rock skin cyclops. HP = 30, ATK = 8
Created monster of race Fire wizard. HP = 50, ATK = 6
Chuck attacks Rock skin cyclops
  Chuck's attributes: hp = 100, xp = 10
  Rock skin cyclops's attributes: hp = 20
Rock skin cyclops starts using magic
Chuck attacks Rock skin cyclops
  Chuck's attributes: hp = 100, xp = 20
  Rock skin cyclops's attributes: hp = 15
Fire wizard attacks Chuck
  Fire wizard's attributes: hp = 50
  Chuck's attributes: hp = 94, xp = 20
Chuck attacks Fire wizard
  Chuck's attributes: hp = 94, xp = 30
  Fire wizard's attributes: hp = 40

I tried to solve the problem like this:

Interface:

public interface RpgGame {
void attack(Character character);

}

Character class:

public class Character {
private int HP;
private int ATK;
private int defense;

public Character(int HP, int ATK, int defense) {
    this.HP = HP;
    this.ATK = ATK;
    this.defense = defense;
}

public void setATK(int ATK) {
    this.ATK = ATK;
}

public int getATK() {
    return ATK;
}

public void setDefense(int defense) {
    this.defense = defense;
}

public int getDefense() {
    return defense;
}

public void setHP(int HP) {
    this.HP = HP;
}

public int getHP() {
    return HP;
}

}

Hero class:

public class Hero extends Character implements RpgGame {
private int XP;
private String name;

public Hero(int HP, int ATK, int defense, int XP, String name) {
    super(HP, ATK, defense);
    this.XP = XP;
    this.name = name;
    System.out.println("Created hero " + name + "," + "HP = " + HP + " , " + "ATK = " + ATK);
}

public void setXP(int XP) {
    this.XP = XP;
}

public int getXP() {
    return XP;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void attack(Character character) {
   
    if(character.getHP() < 0) {
        System.out.println("Monster is dead");
    }else {
        this.XP += getATK();
        int monsterHp = character.getHP();
        monsterHp -= getATK();
        character.setHP(monsterHp);
    }
}

}

Monster class:

public class Monster extends Character {
private String race;

public Monster(int HP, int ATK, int defense, String race) {

    super(HP, ATK, defense);
    this.race = race;
}

public void setRace(String race) {
    this.race = race;
}

public String getRace() {
    return race;
}

}

FireWizard class:

public class FireWizard extends Monster {


public FireWizard(int HP, int ATK, int defense,String race) {
    super(HP, ATK, defense,race);
    System.out.println("Created monster of Fire Wizard, " + "HP = " + HP + " , " + "ATK = " + ATK);
}

public void usingMagic() {

}

}

RockCyclop class:

public class RockCyclop extends Monster {

public RockCyclop(int HP, int ATK, int defense,String race) {
    super(HP, ATK, defense,race);
    System.out.println("Created monster of " + race + "," +  " HP = " + HP + " , " + "ATK =" + ATK);
}

}

Game class:

public class Game {

public static void main(String[] args) {
    Hero chuck = new Hero(100, 10, 50, 0, "Chuck");
    RockCyclop rock = new RockCyclop(30, 8, 40, "Rock skin cyclops");
    FireWizard wizard = new FireWizard(50, 6, 40, "Fire Wizard");

}

} \

I am not sure how to use the attack method from the interface to make the program work properly..and how to use it correctly to send a Character .for example if I want to access character.getRace() I can't because race is not in Character, is just in Monster.. I tried to respect Open Closed principle to extend classes when I need a new character..

0

There are 0 best solutions below