This third assignment will allow you to explore by comparing
Solution
//--------------------- Class Player ------------------------------
import java.util.Scanner;
class Player {
// to store array
private char a[] = new char[5];
private int index = 0;
private final char horse[] = { \'H\', \'O\', \'R\', \'S\', \'E\' };
public char[] getA() {
return a;
}
public void setA(char[] a) {
this.a = a;
}
public int nextShot() {
return (int) (Math.random() * 10);
}
public boolean hasLost() {
return \"HORSE\".equals(String.valueOf(this.a));
}
public char add() {
if (index < a.length) {
a[index] = horse[index];
index++;
}
return a[index-1];
}
}
// --------------- Class Game ---------------------------
class Game {
public void startGame() {
Scanner sc = new Scanner(System.in);
System.out.println(\"Starting Game\");
while (true) {
System.out.println(\"Player1 created\");
Player player1 = new Player();
System.out.println(\"Player2 created\");
Player player2 = new Player();
while(true) {
if(player1.hasLost()) {
System.out.println(\"\ \ Player 2 Wins :: Player 1 = HORSE\ \");
break;
}
if(player2.hasLost()) {
System.out.println(\"\ \ Player 1 Wins :: Player 2 = HORSE\ \");
break;
}
// player2 hit when nextShot is even
boolean hit1 = player1.nextShot()%2==0;
// player2 hit when nextShot is Odd
boolean hit2 = player2.nextShot()%2==1;
if(hit1) {
System.out.println(\"Player #1: Hit Shot\");
}else {
System.out.println(\"Player #1: Missed Shot\");
}
if(hit2) {
System.out.println(\"Player #2: Hit Shot\");
}else {
System.out.println(\"Player #2: Missed Shot\");
}
if(!hit1 && !hit2) {
continue;
}
if(!hit1) {
System.out.println(\"\\t\\tPlayer #1: Added an \"+player1.add());
}
if(!hit2) {
System.out.println(\"\\t\\tPlayer #2: Added an \"+player2.add());
}
}
System.out.println(\"Would you like to play again (Y/N) \");
char c = sc.next().toLowerCase().charAt(0);
if (c == \'n\') {
break;
}
}
System.out.println(\"Game Ends --- Thank you for playing\");
}
}
//------------------------- Tester Class-------------------
public class TesterClass{
public static void main(String[] args) {
Game g = new Game();
g.startGame();
}
}


