In this rpg battle games, please create a java for a simple battle game. it will
ID: 3790348 • Letter: I
Question
In this rpg battle games, please create a java for a simple battle game. it will have two types of opponents:
Hydrons and Zexors. Create two classes: one for Hydrons and one for Zexors. Create a DiceRoll class.
The attributes of the classes are as follows:
Hydrons: have color, height, weight, health (starts at 25), damage potential (0-10, scaled by health),
attack type, numbers of battles won, numbers of battles lost, name, and home planet.
Zexors: have color, height, weight, health (starts at 25), damage potential (0-10, scaled by health),
attack type, numbers of battles won, numbers of battles lost, name, and species.
DiceRoll: number of sides
The methods are up to you.
Setup: The user is asked to create 3 Hydrons and 3 Zexors, and determines how many sides to the dice.
The program randomly chooses a Hydron and pits it against a Zexor. Each battle is determined by a
dice roll – each Hydron and Zexor rolls a single die. The higher of the dice rolls wins the battle. Damage
is attributed to the loser, which also affects the damage potential of the loser. If a Hydron or Zexor does
not engage in a battle, then their health increases by 1 to a maximum of 25. Appropriate information
should be printed at the end of each battle round for all 6 creatures.
For example:
Battle 1: HydronFred (damage potential 8) battles ZexorSally (damage potential 6). They are
both at full health and roll a 7 sided die. ZexorSally rolls a 6 and HydronFred rolls a 1. ZexorSally
wins, HydronFred has a health of 19 (25-6).
Battle 2: HydronFred battles ZexorThor (damage potential 9). ZexorThor is at full health.
ZexorThor rolls a 2 and HydronFred rolls 7. ZexorThor takes damage of 76% of 8.
And so on until one of the 6 creatures has a health of 0.
This project incorporates:
Loops
Random numbers
Classes & objects
Arrays of objects
Methods
Explanation / Answer
I am giving you a solution, which should be very straight. Although, in your question, there are some points which are not clearly stated:
=> Battle 2: HydronFred battles ZexorThor (damage potential 9). ZexorThor is at full health.
ZexorThor rolls a 2 and HydronFred rolls 7. ZexorThor takes damage of 76% of 8.
And so on until one of the 6 creatures has a health of 0.
how is this 76% damage figuere is derieved??
I am creating a common class Opponent.java to which Zexor and Hydrons extend. I did this, because of reusability. A lot of attributes in both the classes are same and can be resused.
Opponent.java
public abstract class Opponent {
protected String color;
protected int height;
protected int weight;
protected int health = 25;
protected int damagePotential;
protected String attacktype;
protected int battlesWon=0;
protected int battlesLost=0;
protected String name;
public Opponent(String name, String color, int height, int weight, int damagePotential,
String attacktype) {
this.color = color;
this.height = height;
this.weight = weight;
this.damagePotential = damagePotential;
this.attacktype = attacktype;
this.name = name;
}
public void increaseHealthByOne() {
if(health < 25) {
health++;
}
}
public void reduceHealth(int value) {
health -= value;
if(health < 0) {
health = 0;
}
}
public int getHealth() {
return health;
}
public int getDamagepotential() {
return damagePotential;
}
public void won() {
battlesWon++;
}
public void lost() {
battlesLost++;
}
public abstract String toString();
}
Hydron.java
public class Hydron extends Opponent {
private String homePlanet;
public Hydron(String name, String color, int height, int weight,
int damagePotential, String attacktype, String homePlanet) {
super(name, color, height, weight, damagePotential, attacktype);
this.homePlanet = homePlanet;
}
public Hydron(String name, int damagePotential) {
super(name, "", 0, 0, damagePotential, "");
this.homePlanet = "";
}
@Override
public String toString() {
return "Hydron: " + name + " Health: " + health + " DamagePotential: "
+ damagePotential + " Won: " + battlesWon + " Lost: "
+ battlesLost;
}
}
Zexor.java
public class Zexor extends Opponent {
private String species;
public Zexor(String name, String color, int height, int weight,
int damagePotential, String attacktype, String species) {
super(name, color, height, weight, damagePotential, attacktype);
this.species = species;
}
public Zexor(String name, int damagePotential) {
super(name, "", 0, 0, damagePotential, "");
this.species = "";
}
@Override
public String toString() {
return "Zexor: " + name + " Health: " + health + " DamagePotential: "
+ damagePotential + " Won: " + battlesWon + " Lost: "
+ battlesLost;
}
}
DiceRoll.java
import java.util.Random;
public class DiceRoll {
private int noOfSides;
Random random = new Random();
public DiceRoll(int noOfSides) {
this.noOfSides = noOfSides;
}
public int rollDice() {
return 1 + random.nextInt(noOfSides);
}
}
Match.java
import java.util.List;
import java.util.Random;
public class Match {
List<Opponent> hydrons;
List<Opponent> zexors;
DiceRoll diceRoll;
Random random = new Random();
/*
* Constructor, which takes players list and noOfSides of dices
*/
public Match(List<Opponent> hydrons, List<Opponent> zexors, int noOfSides) {
if(hydrons == null || hydrons.size() != 3 || zexors == null || zexors.size() != 3) {
throw new IllegalArgumentException();
}
this.hydrons = hydrons;
this.zexors = zexors;
diceRoll = new DiceRoll(noOfSides);
}
// method to start the match
public void startMatch() {
System.out.println("Match has started. ");
System.out.println("Initial States");
printInfoRelatedToPlayers();
// keep picking players untill some player has got 0 health
while(true) {
// picking 0, 1st or 2nd hydron
int hydronIndex = random.nextInt(3);
int zexorIndex = random.nextInt(3);
System.out.println(" Picked " + hydrons.get(hydronIndex).name + " " + zexors.get(zexorIndex).name + " for match");
boolean somebodyWon = false;
// roll the dice and determine who will win
while(!somebodyWon) {
somebodyWon = true;
int hydronRoll = diceRoll.rollDice();
int zexorRoll = diceRoll.rollDice();
System.out.println("Dice roll===> Hydron:" + hydronRoll + " Zexor:" + zexorRoll);
// check who has won the match
if(hydronRoll > zexorRoll) {
// hydron won
int potential = hydrons.get(hydronIndex).getDamagepotential();
zexors.get(zexorIndex).reduceHealth(potential);
hydrons.get(hydronIndex).won();
zexors.get(zexorIndex).lost();
System.out.println("******************************** Hydron won ********************************");
} else if(zexorRoll > hydronRoll) {
// zexors won
int potential = zexors.get(zexorIndex).getDamagepotential();
hydrons.get(hydronIndex).reduceHealth(potential);
zexors.get(hydronIndex).won();
hydrons.get(zexorIndex).lost();
System.out.println("******************************** Zexor won ********************************");
} else {
// if nobody won, give one more try with dice
somebodyWon = false;
}
}
// increase other player health to max of 25
for(int i=0; i< hydrons.size(); i++) {
if(i != hydronIndex) {
hydrons.get(i).increaseHealthByOne();
}
}
for(int i=0; i< zexors.size(); i++) {
if(i != zexorIndex) {
zexors.get(i).increaseHealthByOne();
}
}
// print health of every player
printInfoRelatedToPlayers();
if(hasGameOver()) {
System.out.println("Game is over now.");
break;
}
}
}
// prints info related to players
private void printInfoRelatedToPlayers() {
for(int i=0; i< hydrons.size(); i++) {
System.out.println(hydrons.get(i));
}
for(int i=0; i< zexors.size(); i++) {
System.out.println(zexors.get(i));
}
}
// check if some player has got 0 health
private boolean hasGameOver() {
boolean gameEnd = false;
for(int i=0; i< hydrons.size(); i++) {
if(hydrons.get(i).getHealth() == 0) {
gameEnd = true;
}
}
for(int i=0; !gameEnd && i< zexors.size(); i++) {
if(zexors.get(i).getHealth() == 0) {
gameEnd = true;
}
}
return gameEnd;
}
}
Demo.java
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String args[]) {
// Creating Zexors
Zexor zexor1 = new Zexor("Zexor1", 7);
Zexor zexor2 = new Zexor("Zexor2", 4);
Zexor zexor3 = new Zexor("Zexor3", 9);
Hydron hydron1 = new Hydron("Hydron1", 8);
Hydron hydron2 = new Hydron("Hydron2", 6);
Hydron hydron3 = new Hydron("Hydron3", 10);
List<Opponent> hydrons = new ArrayList<>();
List<Opponent> zexors = new ArrayList<>();
hydrons.add(hydron1);
hydrons.add(hydron2);
hydrons.add(hydron3);
zexors.add(zexor1);
zexors.add(zexor2);
zexors.add(zexor3);
Match match = new Match(zexors, hydrons, 6);
match.startMatch();
}
}
// do let me know in case you require some more functionality
Sample Output:
Match has started.
Initial States
Zexor: Zexor1 Health: 25 DamagePotential: 7 Won: 0 Lost: 0
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 0 Lost: 0
Zexor: Zexor3 Health: 25 DamagePotential: 9 Won: 0 Lost: 0
Hydron: Hydron1 Health: 25 DamagePotential: 8 Won: 0 Lost: 0
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 25 DamagePotential: 10 Won: 0 Lost: 0
Picked Zexor2 Hydron3 for match
Dice roll===> Hydron:6 Zexor:4
******************************** Hydron won ********************************
Zexor: Zexor1 Health: 25 DamagePotential: 7 Won: 0 Lost: 0
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 1 Lost: 0
Zexor: Zexor3 Health: 25 DamagePotential: 9 Won: 0 Lost: 0
Hydron: Hydron1 Health: 25 DamagePotential: 8 Won: 0 Lost: 0
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 21 DamagePotential: 10 Won: 0 Lost: 1
Picked Zexor1 Hydron1 for match
Dice roll===> Hydron:4 Zexor:2
******************************** Hydron won ********************************
Zexor: Zexor1 Health: 25 DamagePotential: 7 Won: 1 Lost: 0
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 1 Lost: 0
Zexor: Zexor3 Health: 25 DamagePotential: 9 Won: 0 Lost: 0
Hydron: Hydron1 Health: 18 DamagePotential: 8 Won: 0 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 22 DamagePotential: 10 Won: 0 Lost: 1
Picked Zexor1 Hydron3 for match
Dice roll===> Hydron:3 Zexor:2
******************************** Hydron won ********************************
Zexor: Zexor1 Health: 25 DamagePotential: 7 Won: 2 Lost: 0
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 1 Lost: 0
Zexor: Zexor3 Health: 25 DamagePotential: 9 Won: 0 Lost: 0
Hydron: Hydron1 Health: 19 DamagePotential: 8 Won: 0 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 15 DamagePotential: 10 Won: 0 Lost: 2
Picked Zexor2 Hydron3 for match
Dice roll===> Hydron:6 Zexor:2
******************************** Hydron won ********************************
Zexor: Zexor1 Health: 25 DamagePotential: 7 Won: 2 Lost: 0
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 2 Lost: 0
Zexor: Zexor3 Health: 25 DamagePotential: 9 Won: 0 Lost: 0
Hydron: Hydron1 Health: 20 DamagePotential: 8 Won: 0 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 11 DamagePotential: 10 Won: 0 Lost: 3
Picked Zexor3 Hydron3 for match
Dice roll===> Hydron:4 Zexor:5
******************************** Zexor won ********************************
Zexor: Zexor1 Health: 25 DamagePotential: 7 Won: 2 Lost: 0
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 2 Lost: 0
Zexor: Zexor3 Health: 15 DamagePotential: 9 Won: 0 Lost: 1
Hydron: Hydron1 Health: 21 DamagePotential: 8 Won: 0 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 11 DamagePotential: 10 Won: 1 Lost: 3
Picked Zexor1 Hydron2 for match
Dice roll===> Hydron:1 Zexor:3
******************************** Zexor won ********************************
Zexor: Zexor1 Health: 19 DamagePotential: 7 Won: 2 Lost: 0
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 2 Lost: 1
Zexor: Zexor3 Health: 16 DamagePotential: 9 Won: 0 Lost: 1
Hydron: Hydron1 Health: 22 DamagePotential: 8 Won: 1 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 12 DamagePotential: 10 Won: 1 Lost: 3
Picked Zexor3 Hydron2 for match
Dice roll===> Hydron:6 Zexor:6
Dice roll===> Hydron:6 Zexor:6
Dice roll===> Hydron:1 Zexor:3
******************************** Zexor won ********************************
Zexor: Zexor1 Health: 20 DamagePotential: 7 Won: 2 Lost: 0
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 2 Lost: 2
Zexor: Zexor3 Health: 10 DamagePotential: 9 Won: 0 Lost: 1
Hydron: Hydron1 Health: 23 DamagePotential: 8 Won: 1 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 13 DamagePotential: 10 Won: 2 Lost: 3
Picked Zexor1 Hydron1 for match
Dice roll===> Hydron:2 Zexor:3
******************************** Zexor won ********************************
Zexor: Zexor1 Health: 12 DamagePotential: 7 Won: 2 Lost: 1
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 2 Lost: 2
Zexor: Zexor3 Health: 11 DamagePotential: 9 Won: 0 Lost: 1
Hydron: Hydron1 Health: 23 DamagePotential: 8 Won: 2 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 14 DamagePotential: 10 Won: 2 Lost: 3
Picked Zexor3 Hydron2 for match
Dice roll===> Hydron:3 Zexor:4
******************************** Zexor won ********************************
Zexor: Zexor1 Health: 13 DamagePotential: 7 Won: 2 Lost: 1
Zexor: Zexor2 Health: 25 DamagePotential: 4 Won: 2 Lost: 3
Zexor: Zexor3 Health: 5 DamagePotential: 9 Won: 0 Lost: 1
Hydron: Hydron1 Health: 24 DamagePotential: 8 Won: 2 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 0 Lost: 0
Hydron: Hydron3 Health: 15 DamagePotential: 10 Won: 3 Lost: 3
Picked Zexor2 Hydron3 for match
Dice roll===> Hydron:4 Zexor:6
******************************** Zexor won ********************************
Zexor: Zexor1 Health: 14 DamagePotential: 7 Won: 2 Lost: 1
Zexor: Zexor2 Health: 15 DamagePotential: 4 Won: 2 Lost: 3
Zexor: Zexor3 Health: 6 DamagePotential: 9 Won: 0 Lost: 2
Hydron: Hydron1 Health: 25 DamagePotential: 8 Won: 2 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 1 Lost: 0
Hydron: Hydron3 Health: 15 DamagePotential: 10 Won: 3 Lost: 3
Picked Zexor2 Hydron1 for match
Dice roll===> Hydron:2 Zexor:4
******************************** Zexor won ********************************
Zexor: Zexor1 Health: 15 DamagePotential: 7 Won: 2 Lost: 2
Zexor: Zexor2 Health: 7 DamagePotential: 4 Won: 2 Lost: 3
Zexor: Zexor3 Health: 7 DamagePotential: 9 Won: 0 Lost: 2
Hydron: Hydron1 Health: 25 DamagePotential: 8 Won: 2 Lost: 1
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 2 Lost: 0
Hydron: Hydron3 Health: 16 DamagePotential: 10 Won: 3 Lost: 3
Picked Zexor3 Hydron1 for match
Dice roll===> Hydron:5 Zexor:2
******************************** Hydron won ********************************
Zexor: Zexor1 Health: 16 DamagePotential: 7 Won: 2 Lost: 2
Zexor: Zexor2 Health: 8 DamagePotential: 4 Won: 2 Lost: 3
Zexor: Zexor3 Health: 7 DamagePotential: 9 Won: 1 Lost: 2
Hydron: Hydron1 Health: 16 DamagePotential: 8 Won: 2 Lost: 2
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 2 Lost: 0
Hydron: Hydron3 Health: 17 DamagePotential: 10 Won: 3 Lost: 3
Picked Zexor3 Hydron3 for match
Dice roll===> Hydron:1 Zexor:2
******************************** Zexor won ********************************
Zexor: Zexor1 Health: 17 DamagePotential: 7 Won: 2 Lost: 2
Zexor: Zexor2 Health: 9 DamagePotential: 4 Won: 2 Lost: 3
Zexor: Zexor3 Health: 0 DamagePotential: 9 Won: 1 Lost: 3
Hydron: Hydron1 Health: 17 DamagePotential: 8 Won: 2 Lost: 2
Hydron: Hydron2 Health: 25 DamagePotential: 6 Won: 2 Lost: 0
Hydron: Hydron3 Health: 17 DamagePotential: 10 Won: 4 Lost: 3
Game is over now.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.