I am writing a program that basically is a guessing game that makes two players guess a random number between 50 and 90--there will be three files (GameLauncher, GuessGame, Player)--now I am trying to make each player both not guess the same number and also not guess the same number later. I am a bit stuck at how to make that happen--it seems like there is an Array of all guesses remembered, but I am a bit lost at how to incorporate in the GuessGame file to make sure the players remember to not guess the same number. Any help would be really great! I have written the program here--I might have to add the updateMemory code in the GuessGame file instead as well:
public class Player {
ArrayList myMemory = new ArrayList();
//helps player remember other numbers
public int guess(){
int myGuess;
myMemory.add(0);
// myGuess = 50 + (int) (Math.random () * 40);
myGuess = 50 + (int) (Math.random () * 40);
while(myMemory.contains(myGuess)) {
// myGuess = 50 + (int) (Math.random () * 40);
myGuess = 50 + (int) (Math.random () * 40);
}
return myGuess;
}
public void updateMemory(int number)
{
myMemory.add(number);
}
}
public class GuessGame {
private int secretNumber = 50 + (int) (Math.random () * 40);
Player playerOne = new Player();
Player playerTwo = new Player();
int playerOneGuess;
int playerTwoGuess;
boolean playerOneisRight = false;
boolean playerTwoisRight = false;
void start(){
while(true) {
playerOneGuess = playerOne.guess();
playerTwoGuess = playerTwo.guess();
System.out.println("Player One guessed: " + playerOneGuess);
System.out.println("Player Two guessed: " + playerTwoGuess);
if (playerOneGuess == secretNumber) {
playerOneisRight = true;
System.out.println("Player One guessed correctly!");
break;
}
else if (playerTwoGuess == secretNumber){
playerTwoisRight = true;
System.out.println("Player Two guessed correctly!");
break;
}
else {
System.out.println("Sorry, nobody guessed right. Try again! ");
playerOneGuess++;
playerTwoGuess++;
}
}
}
}
public class GameLauncher {
public static void main (String [] args) {
GuessGame myGame = new GuessGame();
myGame.start();
}
Aucun commentaire:
Enregistrer un commentaire