public class GuessGame {
//class GuessGame has three instance variable to hold three player objects
Player p1;
Player p2;
Player p3;
public void startGame(){
p1 = new Player(); //assigns new player
p2 = new Player(); //objects to instance variables
p3 = new Player(); //of player
int guessp1 = 0; //holds the guess of p1
int guessp2 = 0; //holds the guess of p2
int guessp3 = 0; //holds the guess of p3
//declare three variables to hold true or false based on player's guess
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;
int targetNumber = (int) (Math.random() * 10); //generates a random number between 0 and 9
System.out.println("I m thinking of a number between 0 and 9");
while(true){
System.out.println("Number to be guessed is " + targetNumber);
p1.guess();
p2.guess();
p3.guess();
//get the guesses of all three players through number variable
guessp1 = p1.number;
System.out.println("Guess of player p1 is " + guessp1);
guessp2 = p2.number;
System.out.println("Guess of player p2 is " + guessp2);
guessp3 = p3.number;
System.out.println("Guess of player p3 is " + guessp3);
if(guessp1 == targetNumber){
p1isRight = true;
}
if(guessp2 == targetNumber){
p2isRight = true;
}
if(guessp3 == targetNumber){
p3isRight = true;
}
if(p1isRight || p2isRight || p3isRight){
System.out.println("We have a Winner");
System.out.println("player one got it right? " + p1isRight);
System.out.println("palyer two got it right? " + p2isRight);
System.out.println("player three got it right? " + p3isRight);
System.out.println("game over");
break;
}
else{
System.out.println("try out another time");
}
}
}
}