IN JAVA You will build a turn based battle game called Battl
IN JAVA:
You will build a turn based battle game called BattleCode. The characters in the game are called Heros - a data class detailed below. In BattleCode, two Heroes enter a battle. The battle is managed by a manager class called the BattleManager - detailed below.
Game Play
Setup
The game starts by initializing Heroes. Heroes have 3 properties: strength, agility, and health. Health is 100, but strength and agility must be initialized by randomness. Strength helps with attacking and agility helps with blocking. Roll three 6-sided die. Choose one die and assign that value to strength. Choose another die to assign to agility.
The next step is determining the battle conditions - a battle can have a fixed number of rounds or it can run until one player runs out of health.
Battles
A battle consists of rounds. In each round, a player can choose how to move their hero - attack, block, or rest.
Once both players select a move damage is calculated.
This completes a round. If battle conditions are not met, another round is initiated.
Once battle conditions are met, there must be a winner. The winner is the hero with the most health. If at the end of a battle, both heroes have the same value for health, the battle is extended by one round. New rounds are initiated until there is a winner.
Hero Class
A hero performs the following
BattleManager Class
A battle manager has a single public method (other private methods are encouraged and should be used)
Instructions
| Move | Result | Calculation |
| Attack | Attack the opponent and apply damage | Roll a two 6-sided die, add sum to strength to determine attack value. |
| Block | Block an attack | Roll an 10-sided die. If the value is less than 4, double it and add it to agility. Otherwise, add the value rolled to agility. |
| Rest | Rest and heal | Roll two 8-sided die. Add the sum to your health |
Solution
package Tests;
import java.util.Random;
import java.util.Scanner;
public class DuelMain {
public static void main(String[] args) {
Random battle = new Random();
@SuppressWarnings(\"resource\")
Scanner input = new Scanner(System.in);
int hero, match, restart;
Duel valon = new Duel();
Duel rintar = new Duel();
Duel zersious = new Duel();
Duel balrock = new Duel();
Duel hawkeye = new Duel();
Duel yusef = new Duel();
valon.attack = 6;
valon.defense = 0;
valon.health = 19;
rintar.attack = 8;
rintar.defense = 1;
rintar.health = 16;
zersious.attack = 5;
zersious.defense = 2;
zersious.health = 18;
balrock.attack = 10;
balrock.defense =0;
balrock.health = 15;
hawkeye.attack = 7;
hawkeye.defense = 1;
hawkeye.health = 17;
yusef.attack = 13;
yusef.defense = 2;
yusef.health = 10;
System.out.println(\"Choose your HERO\");
System.out.println(\"Press 1 for the Mage Lord Valon Press 2 for the Warrior Rintar\");
System.out.println(\"Attack 6 Attack 8\");
System.out.println(\"Defense 0 Defense 1\");
System.out.println(\"Health 19 Health 16\");
System.out.println(\"\");
System.out.println(\"\");
System.out.println(\"Press 3 for the Paladin Prince Zersious Press 4 for the Orc Balrock\");
System.out.println(\"Attack 5 Attack 10\");
System.out.println(\"Defense 2 Defense 0\");
System.out.println(\"Health 18 Health 15\");
System.out.println(\"\");
System.out.println(\"\");
System.out.println(\"Press 5 for the Elf Hawkeye Press 6 for the Dragon Summoner Yusef\");
System.out.println(\"Attack 7 Attack 13\");
System.out.println(\"Defense 1 Defense 2\");
System.out.println(\"Health 17 Health 10\");
hero = input.nextInt();
if(hero == 1){System.out.println(\"You have chosen Lord Valon!\");
for(hero = 1;;){
System.out.println(\"Your battle is against....\");
System.out.println(\"\");
match = 1+battle.nextInt(5);
if(match == 1){
System.out.println(\"Rintar!\");
System.out.println(\"\");
valon.attack1 = 8;
valon.defense1 = 1;
valon.health1 = 16;
valon.calculateWinner();
}else if(match == 2){
System.out.println(\"Prince Zersious!\");
System.out.println(\"\");
valon.attack1 = 5;
valon.defense1 = 2;
valon.health1 = 18;
valon.calculateWinner();
}else if(match == 3){
System.out.println(\"Balrock!\");
System.out.println(\"\");
valon.attack1 = 10;
valon.defense1 = 0;
valon.health1 = 15;
valon.calculateWinner();
}else if(match == 4){
System.out.println(\"Hawkeye!\");
System.out.println(\"\");
valon.attack1 = 7;
valon.defense1 = 1;
valon.health1 = 17;
valon.calculateWinner();
}else if(match == 5){
System.out.println(\"Yusef!\");
System.out.println(\"\");
valon.attack1 = 13;
valon.defense1 = 2;
valon.health1 = 10;
valon.calculateWinner();}
System.out.println(\"\");
System.out.println(\"Fight again?\");
System.out.println(\"1 for Yes\");
System.out.println(\"2 for No\");
restart = input.nextInt();
if(restart == 1){
System.out.println(\"\");
}else if(restart == 2){
System.out.println(\"Thank you for playing!\");
break;}
}
}
if(hero == 2){System.out.println(\"You have chosen Rintar!\");
for(hero = 2;;){
System.out.println(\"Your battle is against....\");
System.out.println(\"\");
match = 1+battle.nextInt(5);
if(match == 1){
System.out.println(\"Lord Valon!\");
System.out.println(\"\");
rintar.attack1 = 6;
rintar.defense1 = 0;
rintar.health1 = 19;
rintar.calculateWinner();
}else if(match == 2){
System.out.println(\"Prince Zersious!\");
System.out.println(\"\");
rintar.attack1 = 5;
rintar.defense1 = 2;
rintar.health1 = 18;
rintar.calculateWinner();
}else if(match == 3){
System.out.println(\"Balrock!\");
System.out.println(\"\");
rintar.attack1 = 10;
rintar.defense1 = 0;
rintar.health1 = 15;
rintar.calculateWinner();
}else if(match == 4){
System.out.println(\"Hawkeye!\");
System.out.println(\"\");
rintar.attack1 = 7;
rintar.defense1 = 1;
rintar.health1 = 17;
rintar.calculateWinner();
}else if(match == 5){
System.out.println(\"Yusef!\");
System.out.println(\"\");
rintar.attack1 = 13;
rintar.defense1 = 2;
rintar.health1 = 10;
rintar.calculateWinner();}
System.out.println(\"\");
System.out.println(\"Fight again?\");
System.out.println(\"1 for Yes\");
System.out.println(\"2 for No\");
restart = input.nextInt();
if(restart == 1){
System.out.println(\"\");
}else if(restart == 2){
System.out.println(\"Thank you for playing!\");
break;}
}
}
if(hero == 3){System.out.println(\"You have chosen Prince Zersious!\");
for(hero = 3;;){
System.out.println(\"Your battle is against....\");
System.out.println(\"\");
match = 1+battle.nextInt(5);
if(match == 1){
System.out.println(\"Lord Valon!\");
System.out.println(\"\");
zersious.attack1 = 6;
zersious.defense1 = 0;
zersious.health1 = 19;
zersious.calculateWinner();
}else if(match == 2){
System.out.println(\"Rintar!\");
System.out.println(\"\");
zersious.attack1 = 8;
zersious.defense1 = 1;
zersious.health1 = 16;
zersious.calculateWinner();
}else if(match == 3){
System.out.println(\"Balrock!\");
System.out.println(\"\");
zersious.attack1 = 10;
zersious.defense1 = 0;
zersious.health1 = 15;
zersious.calculateWinner();
}else if(match == 4){
System.out.println(\"Hawkeye!\");
System.out.println(\"\");
zersious.attack1 = 7;
zersious.defense1 = 1;
zersious.health1 = 17;
zersious.calculateWinner();
}else if(match == 5){
System.out.println(\"Yusef!\");
System.out.println(\"\");
zersious.attack1 = 13;
zersious.defense1 = 2;
zersious.health1 = 10;
zersious.calculateWinner();}
System.out.println(\"\");
System.out.println(\"Fight again?\");
System.out.println(\"1 for Yes\");
System.out.println(\"2 for No\");
restart = input.nextInt();
if(restart == 1){
System.out.println(\"\");
}else if(restart == 2){
System.out.println(\"Thank you for playing!\");
break;}
}
}
if(hero == 4){System.out.println(\"You have chosen Balrock!\");
for(hero = 4;;){
System.out.println(\"Your battle is against....\");
System.out.println(\"\");
match = 1+battle.nextInt(5);
if(match == 1){
System.out.println(\"Lord Valon!\");
System.out.println(\"\");
balrock.attack1 = 6;
balrock.defense1 =0;
balrock.health1 = 19;
balrock.calculateWinner();
}else if(match == 2){
System.out.println(\"Prince Zersious!\");
System.out.println(\"\");
balrock.attack1 = 5;
balrock.defense1 =2;
balrock.health1 = 18;
balrock.calculateWinner();
}else if(match == 3){
System.out.println(\"Rintar!\");
System.out.println(\"\");
balrock.attack1 = 8;
balrock.defense1 =1;
balrock.health1 = 16;
balrock.calculateWinner();
}else if(match == 4){
System.out.println(\"Hawkeye!\");
System.out.println(\"\");
balrock.attack1 = 7;
balrock.defense1 =1;
balrock.health1 = 17;
balrock.calculateWinner();
}else if(match == 5){
System.out.println(\"Yusef!\");
System.out.println(\"\");
balrock.attack1 = 13;
balrock.defense1 =2;
balrock.health1 = 10;
balrock.calculateWinner();}
System.out.println(\"\");
System.out.println(\"Fight again?\");
System.out.println(\"1 for Yes\");
System.out.println(\"2 for No\");
restart = input.nextInt();
if(restart == 1){
System.out.println(\"\");
}else if(restart == 2){
System.out.println(\"Thank you for playing!\");
break;}
}
}
if(hero == 5){System.out.println(\"You have chosen Hawkeye!\");
for(hero = 5;;){
System.out.println(\"Your battle is against....\");
System.out.println(\"\");
match = 1+battle.nextInt(5);
if(match == 1){
System.out.println(\"Lord Valon!\");
System.out.println(\"\");
hawkeye.attack1 = 6;
hawkeye.defense1 = 0;
hawkeye.health1 = 19;
hawkeye.calculateWinner();
}else if(match == 2){
System.out.println(\"Prince Zersious!\");
System.out.println(\"\");
hawkeye.attack1 = 5;
hawkeye.defense1 = 2;
hawkeye.health1 = 18;
hawkeye.calculateWinner();
}else if(match == 3){
System.out.println(\"Balrock!\");
System.out.println(\"\");
hawkeye.attack1 = 10;
hawkeye.defense1 = 0;
hawkeye.health1= 15;
hawkeye.calculateWinner();
}else if(match == 4){
System.out.println(\"Rintar!\");
System.out.println(\"\");
hawkeye.attack1 = 8;
hawkeye.defense1 = 1;
hawkeye.health1 = 16;
hawkeye.calculateWinner();
}else if(match == 5){
System.out.println(\"Yusef!\");
System.out.println(\"\");
hawkeye.attack1 = 13;
hawkeye.defense1 = 2;
hawkeye.health1 = 10;
hawkeye.calculateWinner();}
System.out.println(\"\");
System.out.println(\"Fight again?\");
System.out.println(\"1 for Yes\");
System.out.println(\"2 for No\");
restart = input.nextInt();
if(restart == 1){
System.out.println(\"\");
}else if(restart == 2){
System.out.println(\"Thank you for playing!\");
break;}
}
}
if(hero == 6){System.out.println(\"You have chosen Yusef!\");
for(hero = 6;;){
System.out.println(\"Your battle is against....\");
System.out.println(\"\");
match = 1+battle.nextInt(5);
if(match == 1){
System.out.println(\"Lord Valon!\");
System.out.println(\"\");
yusef.attack1 = 6;
yusef.defense1 = 0;
yusef.health1 = 19;
yusef.calculateWinner();
}else if(match == 2){
System.out.println(\"Prince Zersious!\");
System.out.println(\"\");
yusef.attack1 = 5;
yusef.defense1 = 2;
yusef.health1 = 18;
yusef.calculateWinner();
}else if(match == 3){
System.out.println(\"Balrock!\");
System.out.println(\"\");
yusef.attack1 = 10;
yusef.defense1 = 0;
yusef.health1 = 15;
yusef.calculateWinner();
}else if(match == 4){
System.out.println(\"Rintar!\");
System.out.println(\"\");
yusef.attack1 = 8;
yusef.defense1 = 1;
yusef.health1 = 16;
yusef.calculateWinner();
}else if(match == 5){
System.out.println(\"Hawkeye!\");
System.out.println(\"\");
yusef.attack1 = 13;
yusef.defense1 = 2;
yusef.health1 = 10;
yusef.calculateWinner();}
System.out.println(\"\");
System.out.println(\"Fight again?\");
System.out.println(\"1 for Yes\");
System.out.println(\"2 for No\");
restart = input.nextInt();
if(restart == 1){
System.out.println(\"\");
}else if(restart == 2){
System.out.println(\"Thank you for playing!\");
break;}
}
}
}
}
package Tests;
import java.util.Random;
public class Duel {
Random battle = new Random();
int newHealth, newHealth1;
int outcome, outcome1, outcome2, outcome3;
int attack, attack1;
int defense, defense1;
int health, health1;
void calculateWinner(){
do{ outcome = attack - defense1;
newHealth1 = health1 - outcome;
System.out.println(\"Your attack does \" + outcome + \" damage!\");
System.out.println(\"\");
System.out.println(\"Enemy Health\");
System.out.println(newHealth1);
System.out.println(\"\");
outcome2 = attack1 - defense;
newHealth = health - outcome2;
System.out.println(\"Enemies attack does \" + outcome2 + \" damage!\");
System.out.println(\"\");
System.out.println(\"Your Health\");
System.out.println(newHealth);
if(newHealth1 > 0 && newHealth > 0){
outcome = attack - defense1;
outcome1 = 2 * outcome;
newHealth1 = health1 - outcome1;
System.out.println(\"Your attack does \" + outcome + \" damage!\");
System.out.println(\"\");
System.out.println(\"Enemy Health\");
System.out.println(newHealth1);
outcome = attack1 - defense;
outcome1 = 2 * outcome;
newHealth = health - outcome1;
System.out.println(\"\");
System.out.println(\"Enemies attack does \" + outcome2 + \" damage!\");
System.out.println(\"\");
System.out.println(\"Your Health\");
System.out.println(newHealth);
if(newHealth1 > 0 && newHealth > 0){
outcome = attack - defense1;
outcome1 = 3 * outcome;
newHealth1 = health1 - outcome1;
System.out.println(\"Your attack does \" + outcome + \" damage!\");
System.out.println(\"\");
System.out.println(\"Enemy Health\");
System.out.println(newHealth1);
outcome = attack1 - defense;
outcome1 = 3 * outcome;
newHealth = health - outcome1;
System.out.println(\"\");
System.out.println(\"Enemies attack does \" + outcome2 + \" damage!\");
System.out.println(\"\");
System.out.println(\"Your Health\");
System.out.println(newHealth);
if(newHealth1 > 0 && newHealth > 0){
outcome = attack - defense1;
outcome1 = 4 * outcome;
newHealth1 = health1 - outcome1;
System.out.println(\"Your attack does \" + outcome + \" damage!\");
System.out.println(\"\");
System.out.println(\"Enemy Health\");
System.out.println(newHealth1);
outcome = attack1 - defense;
outcome1 = 4 * outcome;
newHealth = health - outcome1;
System.out.println(\"\");
System.out.println(\"Enemies attack does \" + outcome2 + \" damage!\");
System.out.println(\"\");
System.out.println(\"Your Health\");
System.out.println(newHealth);
if(newHealth1 > 0 && newHealth > 0){
outcome = attack - defense1;
outcome1 = 5 * outcome;
newHealth1 = health1 - outcome1;
System.out.println(\"Your attack does \" + outcome + \" damage!\");
System.out.println(\"\");
System.out.println(\"Enemy Health\");
System.out.println(newHealth1);
outcome = attack1 - defense;
outcome1 = 5 * outcome;
newHealth = health - outcome1;
System.out.println(\"\");
System.out.println(\"Enemies attack does \" + outcome2 + \" damage!\");
System.out.println(\"\");
System.out.println(\"Your Health\");
System.out.println(newHealth);}
}
}
}
}while(newHealth1 > 0 && newHealth > 0);
if(newHealth1 <= 0 && newHealth > 0){
System.out.println(\"You win!\");}
if(newHealth <= 0 && newHealth1 > 0){
System.out.println(\"You lose!\");}
if(newHealth <= 0 && newHealth1 <= 0){
System.out.println(\"Draw!\");}
}
}














