PLEASE CORRECT THE CODING ERRORS THAT I MADE : I GOT COMMENTS THAT \" you neglec
ID: 3837954 • Letter: P
Question
PLEASE CORRECT THE CODING ERRORS THAT I MADE : I GOT COMMENTS THAT
" you neglected to actually instantiate the die with the number of sides, so it crashed right away. once that was fixed, it worked fine."
__________________________________________________
you are going to create a simple battle game.
You 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, number of battles won, number 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, number of battles won, number 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 the 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 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.
You will be graded based on the programming rubric for the course. This project incorporates:
Loops
Random numbers
Classes & objects
Arrays of objects
Methods
_________________________________
//RPG battle
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class RPGBattle {
public static void main(String args[])
{
DiceRoll die = null;
Hydron[] hydronList = new Hydron[3];
Zexor[] zexorList = new Zexor[3];
boolean battleFinished = false;
Scanner sc = new Scanner(System.in);
System.out.println("***************************************");
System.out.print(" WELCOME TO RPG BATTLE ");
System.out.println();
System.out.println("***************************************");
System.out.print("Please select no of Sides of the Dice: ");
int noOfSides = sc.nextInt(); sc.nextLine(); //Creating a die object with user defined no of sides die = new DiceRoll(noOfSides);
System.out.println();
System.out.println("Please create '3 Hydrons' by providing required Data for each Hydron:");
for(int i=0; i<3; i++)
{
System.out.println("<" +"Hydron "+(i+1)+">");
System.out.print("Hydron Color: ");
String color = sc.nextLine().trim();
System.out.print("Hydron Height: ");
double height = sc.nextDouble(); sc.nextLine(); System.out.print("Hydron Weight: ");
double weight = sc.nextDouble();
sc.nextLine();
System.out.print("Hydron Damage Potential(Between 1-10): ");
double damagePotential = sc.nextDouble();
sc.nextLine();
System.out.print("Hydron Attack Type(Offensive/Defensive/Mixed): ");
String attackType = sc.nextLine().trim();
System.out.print("Hydron Name: ");
String name = sc.nextLine().trim();
name = "Hydron"+name;
System.out.print("Hydron Home Planet: ");
String homePlanet = sc.nextLine().trim();
Hydron hydron = new Hydron(color, height, weight, damagePotential, attackType, name, homePlanet);
hydronList[i] = hydron;
System.out.println("-------------------------------------------------------");
}
System.out.println("Please create '3 Zexors' by providing required Data for each Zexor:");
for(int i=0; i<3; i++)
{
System.out.println("<"+"Zexor "+(i+1)+">");
System.out.print("Zexor Color: ");
String color = sc.nextLine().trim();
System.out.print("Zexor Height: ");
double height = sc.nextDouble();
sc.nextLine();
System.out.print("Zexor Weight: ");
double weight = sc.nextDouble();
sc.nextLine();
System.out.print("Zexor Damage Potential(Between 1-10): ");
double damagePotential = sc.nextDouble();
sc.nextLine();
System.out.print("Zexor Attack Type(Offensive/Defensive/Mixed): ");
String attackType = sc.nextLine().trim();
System.out.print("Zexor Name: ");
String name = sc.nextLine().trim();
name = "Zexor"+name;
System.out.print("Zexor Species: ");
String species = sc.nextLine().trim();
Zexor zexor = new Zexor(color, height, weight, damagePotential, attackType, name, species);
zexorList[i] = zexor;
System.out.println("--------------------------------------------------------------");
}
System.out.println("-----------------------Battle Begins------------------------");
int roundNumber = 1;
while(!battleFinished)
{
System.out.println("--------------------Round "+roundNumber+"--------------------");
int hydronPlayerIndex = generateRandomNumber() - 1;
int zexorPlayerIndex = generateRandomNumber() - 1;
Hydron playerHydron = hydronList[hydronPlayerIndex];
Zexor playerZexor = zexorList[zexorPlayerIndex];
System.out.println("Hydron Player: "+playerHydron.getName());
System.out.println("Zexor Player: "+playerZexor.getName());
int hydronDieRoll = die.rollDice();
int zexorDieRoll = die.rollDice();
System.out.println(playerHydron.getName()+" Die Roll: "+hydronDieRoll);
System.out.println(playerZexor.getName()+" Die Roll: "+zexorDieRoll);
if(hydronDieRoll > zexorDieRoll)
{
System.out.println(playerHydron.getName()+ "Wins");
if((playerZexor.getHealth() - playerHydron.getDamagePotential()) <= 0)
{
playerZexor.setHealth(0);
battleFinished = true;
}
else {
playerZexor.setHealth(playerZexor.getHealth() - playerHydron.getDamagePotential());
}
double scalingFactor = (playerZexor.getHealth()/25);
double newDamagePotential = scalingFactor*playerZexor.getDamagePotential();
playerZexor.setDamagePotential(newDamagePotential);
}
else {
System.out.println(playerZexor.getName()+ "Wins");
if((playerHydron.getHealth() - playerZexor.getDamagePotential()) <= 0) { playerHydron.setHealth(0); battleFinished = true;
}
else {
playerHydron.setHealth(playerHydron.getHealth() - playerZexor.getDamagePotential());
}
double scalingFactor = (playerHydron.getHealth()/25);
double newDamagePotential = scalingFactor*playerHydron.getDamagePotential();
playerHydron.setDamagePotential(newDamagePotential);
}
for(int i=0; i<3; i++)
{
if(i != hydronPlayerIndex)
{
if((hydronList[i].getHealth()) < 25)
{
if((hydronList[i].getHealth()+1) > 25)
{
hydronList[i].setHealth(25);
}
else {
hydronList[i].setHealth(hydronList[i].getHealth() + 1);
}
}
}
if( i != zexorPlayerIndex)
{
if(zexorList[i].getHealth() < 25)
{
if((zexorList[i].getHealth()+1) > 25)
{
zexorList[i].setHealth(25);
}
else {
zexorList[i].setHealth(hydronList[i].getHealth() + 1);
}
}
}
}
System.out.println("Health Info After Battle Round "+roundNumber+" For All 6 Creatures:");
System.out.println("Hydrons Zexors");
System.out.println("------------------------------------------------------------------");
for(int i=0; i<3; i++)
{
System.out.println(hydronList[i].getName()+": "+hydronList[i].getHealth()+" " +zexorList[i].getName()+": "+zexorList[i].getHealth());
}
roundNumber++;
}
}
public static int generateRandomNumber()
{
Random rand = new Random();
int low = 1;
int high = 4;
int result = rand.nextInt(high - low) + low;
return result;
}}
____________________________________
//Zexor
class Zexor {
String color;
double height;
double weight;
double health = 25;
double damagePotential;
String attackType;
int noOfBattlesWon = 0;
int noOfBattlesLost = 0;
String name;
String species;
public Zexor(String color, double height, double weight, double damagePotential, String attackType, String name, String species)
{
this.color = color;
this.height = height;
this.weight = weight;
this.damagePotential = damagePotential;
this.attackType = attackType;
this.name = name;
this.species = species;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHealth() {
return health;
}
public void setHealth(double health) {
this.health = health;
}
public double getDamagePotential() {
return damagePotential;
}
public void setDamagePotential(double damagePotential) {
this.damagePotential = damagePotential;
}
public String getAttackType() {
return attackType;
}
public void setAttackType(String attackType) {
this.attackType = attackType;
}
public int getNoOfBattlesWon() {
return noOfBattlesWon;
}
public void setNoOfBattlesWon(int noOfBattlesWon) {
this.noOfBattlesWon = noOfBattlesWon;
}
public int getNoOfBattlesLost() {
return noOfBattlesLost;
}
public void setNoOfBattlesLost(int noOfBattlesLost) {
this.noOfBattlesLost = noOfBattlesLost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
}
___________________________________
//DiceRoll
import java.util.Random;
class DiceRoll {
]int noOfSides;
public DiceRoll(int noOfSides)
{
this.noOfSides = noOfSides;
}
public int rollDice()
{
Random rand = new Random();
int low = 1;
int high = noOfSides + 1;
int result = rand.nextInt(high - low) + low;
return result;
}}
________________________________
//Hydron
class Hydron {
String color;
double height;
double weight;
double health = 25;
double damagePotential;
String attackType;
int noOfBattlesWon=0;
int noOfBattlesLost=0;
String name;
String homePlanet;
public Hydron(String color, double height, double weight,
double damagePotential, String attackType, String name, String homePlanet)
{
this.color = color;
this.height = height;
this.weight = weight;
this.damagePotential = damagePotential;
this.attackType = attackType;
this.name = name;
this.homePlanet = homePlanet;
}
//Getters and Setters
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHealth() {
return health;
}
public void setHealth(double health) {
this.health = health;
}
public double getDamagePotential() {
return damagePotential;
}
public void setDamagePotential(double damagePotential) {
this.damagePotential = damagePotential;
}
public String getAttackType() {
return attackType;
}
public void setAttackType(String attackType) {
this.attackType = attackType;
}
public int getNoOfBattlesWon() {
return noOfBattlesWon;
}
public void setNoOfBattlesWon(int noOfBattlesWon) {
this.noOfBattlesWon = noOfBattlesWon;
}
public int getNoOfBattlesLost() {
return noOfBattlesLost;
}
public void setNoOfBattlesLost(int noOfBattlesLost) {
this.noOfBattlesLost = noOfBattlesLost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHomePlanet() {
return homePlanet;
}
public void setHomePlanet(String homePlanet) {
this.homePlanet = homePlanet;
}
}
Explanation / Answer
//This file is modified and modifications is in bold
//RPG battle
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class RPGBattle
{
public static void main(String args[])
{
//DiceRoll variable is craeated
//and it is an object type, it is
//set to null
DiceRoll die = null;
Hydron[] hydronList = new Hydron[3];
Zexor[] zexorList = new Zexor[3];
boolean battleFinished = false;
Scanner sc = new Scanner(System.in);
System.out.println("***************************************");
System.out.print(" WELCOME TO RPG BATTLE ");
System.out.println();
System.out.println("***************************************");
System.out.print("Please select no of Sides of the Dice: ");
int noOfSides = sc.nextInt(); sc.nextLine(); //Creating a die object with user defined no of sides die = new DiceRoll(noOfSides);
/**
* Note:
* To instantiate the DiceRoll with user given number
* of sides is as follows
* die=new DiceRoll(noOfSides);
* so that die object is created .
* */
//create an instance of DiceRoll with number of sides as integer argument
die=new DiceRoll(noOfSides);
System.out.println();
System.out.println("Please create '3 Hydrons' by providing required Data for each Hydron:");
for(int i=0; i<3; i++)
{
System.out.println("<" +"Hydron "+(i+1)+">");
System.out.print("Hydron Color: ");
String color = sc.nextLine().trim();
System.out.print("Hydron Height: ");
double height = sc.nextDouble(); sc.nextLine(); System.out.print("Hydron Weight: ");
double weight = sc.nextDouble();
sc.nextLine();
System.out.print("Hydron Damage Potential(Between 1-10): ");
double damagePotential = sc.nextDouble();
sc.nextLine();
System.out.print("Hydron Attack Type(Offensive/Defensive/Mixed): ");
String attackType = sc.nextLine().trim();
System.out.print("Hydron Name: ");
String name = sc.nextLine().trim();
name = "Hydron"+name;
System.out.print("Hydron Home Planet: ");
String homePlanet = sc.nextLine().trim();
Hydron hydron = new Hydron(color, height, weight, damagePotential, attackType, name, homePlanet);
hydronList[i] = hydron;
System.out.println("-------------------------------------------------------");
}
System.out.println("Please create '3 Zexors' by providing required Data for each Zexor:");
for(int i=0; i<3; i++)
{
System.out.println("<"+"Zexor "+(i+1)+">");
System.out.print("Zexor Color: ");
String color = sc.nextLine().trim();
System.out.print("Zexor Height: ");
double height = sc.nextDouble();
sc.nextLine();
System.out.print("Zexor Weight: ");
double weight = sc.nextDouble();
sc.nextLine();
System.out.print("Zexor Damage Potential(Between 1-10): ");
double damagePotential = sc.nextDouble();
sc.nextLine();
System.out.print("Zexor Attack Type(Offensive/Defensive/Mixed): ");
String attackType = sc.nextLine().trim();
System.out.print("Zexor Name: ");
String name = sc.nextLine().trim();
name = "Zexor"+name;
System.out.print("Zexor Species: ");
String species = sc.nextLine().trim();
Zexor zexor = new Zexor(color, height, weight, damagePotential, attackType, name, species);
zexorList[i] = zexor;
System.out.println("--------------------------------------------------------------");
}
System.out.println("-----------------------Battle Begins------------------------");
int roundNumber = 1;
while(!battleFinished)
{
System.out.println("--------------------Round "+roundNumber+"--------------------");
int hydronPlayerIndex = generateRandomNumber() - 1;
int zexorPlayerIndex = generateRandomNumber() - 1;
Hydron playerHydron = hydronList[hydronPlayerIndex];
Zexor playerZexor = zexorList[zexorPlayerIndex];
System.out.println("Hydron Player: "+playerHydron.getName());
System.out.println("Zexor Player: "+playerZexor.getName());
int hydronDieRoll = die.rollDice();
int zexorDieRoll = die.rollDice();
System.out.println(playerHydron.getName()+" Die Roll: "+hydronDieRoll);
System.out.println(playerZexor.getName()+" Die Roll: "+zexorDieRoll);
if(hydronDieRoll > zexorDieRoll)
{
System.out.println(playerHydron.getName()+ "Wins");
if((playerZexor.getHealth() - playerHydron.getDamagePotential()) <= 0)
{
playerZexor.setHealth(0);
battleFinished = true;
}
else {
playerZexor.setHealth(playerZexor.getHealth() - playerHydron.getDamagePotential());
}
double scalingFactor = (playerZexor.getHealth()/25);
double newDamagePotential = scalingFactor*playerZexor.getDamagePotential();
playerZexor.setDamagePotential(newDamagePotential);
}
else {
System.out.println(playerZexor.getName()+ "Wins");
if((playerHydron.getHealth() - playerZexor.getDamagePotential()) <= 0) { playerHydron.setHealth(0); battleFinished = true;
}
else {
playerHydron.setHealth(playerHydron.getHealth() - playerZexor.getDamagePotential());
}
double scalingFactor = (playerHydron.getHealth()/25);
double newDamagePotential = scalingFactor*playerHydron.getDamagePotential();
playerHydron.setDamagePotential(newDamagePotential);
}
for(int i=0; i<3; i++)
{
if(i != hydronPlayerIndex)
{
if((hydronList[i].getHealth()) < 25)
{
if((hydronList[i].getHealth()+1) > 25)
{
hydronList[i].setHealth(25);
}
else {
hydronList[i].setHealth(hydronList[i].getHealth() + 1);
}
}
}
if( i != zexorPlayerIndex)
{
if(zexorList[i].getHealth() < 25)
{
if((zexorList[i].getHealth()+1) > 25)
{
zexorList[i].setHealth(25);
}
else {
zexorList[i].setHealth(hydronList[i].getHealth() + 1);
}
}
}
}
System.out.println("Health Info After Battle Round "+roundNumber+" For All 6 Creatures:");
System.out.println("Hydrons Zexors");
System.out.println("------------------------------------------------------------------");
for(int i=0; i<3; i++)
{
System.out.println(hydronList[i].getName()+": "+hydronList[i].getHealth()+" " +zexorList[i].getName()+": "+zexorList[i].getHealth());
}
roundNumber++;
}
}
public static int generateRandomNumber()
{
Random rand = new Random();
int low = 1;
int high = 4;
int result = rand.nextInt(high - low) + low;
return result;
}
}
Note :Hydron.java, DiceRoll.java and Zexor.java has no modifications.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.