0

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..

  • 1
    Consider whether your `attack` method is something that an actor should do, in which case it should be in the actor's interface, or the game, which is where you have it now. Answer this question for yourself: What attacks? The thing that attacks should implement an interface that includes that method. – David Koelle Nov 30 '20 at 22:10
  • Please consider [mcve] for future questions. You bring a lot of content to the table, and few people are willing to sit down and digest all of that. Please understand that we dont do homework reviews here for example, and this is also not a free tutor service. So whilst nothing is really wrong with your question, don't be surprised that not much feedback is coming. You want to *discuss* your homework to a certain degree, and that is what you better do with your peers and tutors. – GhostCat Dec 01 '20 at 07:34
  • The one thing I can tell you right away: learn about java naming conventions. You use ALL_UPPER_CASE only for constants. A good field name wold be `healthPoints` instead of HP for example. You write code so that human readers understand what is going on. HP, ATK, ... all mean nothing. – GhostCat Dec 01 '20 at 07:35
  • I thought that I can find some help, I am a beginner and I learn alone Java...I just wanted some hints.I tried to make my post clear and if it's not ok I will consider in the future to be more careful..but it's ok, I will try alone, thank you – Eugen Gîrlescu Dec 01 '20 at 09:57

0 Answers0