Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1: Battle Game (50 points) For this question, you will write a number o

ID: 3813470 • Letter: Q

Question

Question 1: Battle Game (50 points) For this question, you will write a number of classes to create a battle game between a player and a monster. Your code for this question will go in multiple a files. We strongly recommend that you complete all the warm-up questions before starting this problem. Note that in addition to the required methods below, you are free to add as many other private methods as you want. (a) Character Class Character.java represents a character in our battle game. The monster and the player will both be instances of the Character class, each with their own attributes. The Character class should contain the following (private attributes A string name A double attack value A double maximum health value A double current health value A int number of wins in the battle game Here are the required public methods for this class. Note that you will also have to add getters and setters for the instance attributes as needed 1) A constructor The constructor for the Character class takes one String, two doubles, and one int as input. These parameters represent the name, attack value, maximum health, and number of wins in the battle game for the character, in that order. Note that the current health of a new character is the same as the maximum health 2) The toString method This method returns a String consisting of the character's name and current health. Format the String in any way you want. This method will be very handy for debugging your code, and will be used during the battle game to keep track of the health of each character

Explanation / Answer

Here's the Monster class:

public final class Monster {

private final String name;
private final String description;
private int hitPoints;
private final int minDamage;
private final int maxDamage;
private final static Random random = new Random();
private final static Set<Integer> monstersSeen = new HashSet<Integer>();
private final static int NUM_MONSTERS = 3;

public static Monster newRandomInstance() {
if (monstersSeen.size() == NUM_MONSTERS) {
monstersSeen.clear();
}
int i;
do {
i = random.nextInt(NUM_MONSTERS);
} while (monstersSeen.contains(i));
monstersSeen.add(i);

if (i == 0) {
return new Monster("Harpy", Art.HARPY, 40, 8, 12);
} else if (i == 1) {
return new Monster("Gargoyle", Art.GARGOYLE, 26, 4, 6);
} else {
return new Monster("Hobgoblin", Art.HOBGOBLIN, 18, 1, 2);
}
}

public static Monster newBossInstance() {
return new Monster("Dragon", Art.DRAGON, 60, 10, 20);
}

private Monster(String name, String description, int hitPoints, int minDamage, int maxDamage) {
this.name = name;
this.description = description;
this.minDamage = minDamage;
this.maxDamage = maxDamage;
this.hitPoints = hitPoints;
}

@Override
public String toString() {
return name;
}

public String getDescription() {
return description;
}

public String getStatus() {
return "Monster HP: " + hitPoints;
}

public int attack() {
return random.nextInt(maxDamage - minDamage + 1) + minDamage;
}

public void defend(Player player) {
int attackStrength = player.attack();
hitPoints = (hitPoints > attackStrength) ? hitPoints - attackStrength : 0;
System.out.printf(" %s hits %s for %d HP of damage (%s) ", player, name, attackStrength,
getStatus());
if (hitPoints == 0) {
System.out.println(" " + player + " transforms the skull of " + name
+ " into a red pancake with his stone hammer");
}
}

public boolean isAlive() {
return hitPoints > 0;
}

}
Battle class:

public final class Battle {

public Battle(Player player, Monster monster) throws IOException {
System.out.println("You encounter " + monster + ": " + monster.getDescription() + " ");
System.out.println("Battle with " + monster + " starts (" + player.getStatus() + " / "
+ monster.getStatus() + ")");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (player.isAlive() && monster.isAlive()) {
System.out.print("Attack (a) or heal (h)? ");
String action = in.readLine();
if (action.equals("h")) {
player.heal();
} else {
monster.defend(player);
}
if (monster.isAlive()) {
player.defend(monster);
}
}
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote