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
Solution
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;
}
publi.
In this rpg battle games, please create a java for a simple battle g.pdf
1. 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
2. Classes & objects
Arrays of objects
Methods
Solution
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;
3. 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
4. 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, "");
5. 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 hydrons;
6. List zexors;
DiceRoll diceRoll;
Random random = new Random();
/*
* Constructor, which takes players list and noOfSides of dices
*/
public Match(List hydrons, List 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;
7. 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) {
8. 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;
9. }
}
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 hydrons = new ArrayList<>();
List 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: