/* Class GuessMyNumber This class plays a game with a user. The program generates a random number between 1 and 100. The player then has 10 turns to guess the number. The program prompts the player for their guess. The player's guess is validated. If the guess is not valid a message is displayed and the turn is over. If the guess is valid then a message is displayed stating whether the correct number is higher, lower, or exactly correct. If the player guesses the number within 10 turns they win and the game ends. Inputs: There are no command line inputs to this program. The user is prompted to enter integers. Outputs: Messages are displayed to the screen. History: 06/17/02 Jeff LeBeau. ******************************************************/ public class GuessMyNumber { static int number; public static void main(String[] args) { // Generate the number number = generateNumber(); if (playTurn(1)) return; if (playTurn(2)) return; if (playTurn(3)) return; if (playTurn(4)) return; if (playTurn(5)) return; if (playTurn(6)) return; if (playTurn(7)) return; if (playTurn(8)) return; if (playTurn(9)) return; if (!playTurn(10)) { System.out.println("Sorry! You lose."); } } /******************************************************* The printRules method does not receive any input. It just prints some lines to the screen *******************************************************/ public static void printRules() { System.out.println("Welcome to Guess My Number"); System.out.println("I have picked a number between 1 and 100 inclusive."); System.out.println("You must try to guess my number in 10 turns or less.\n\n"); } /******************************************************* The generateNumber() method will not need any inputs. It will generate a random int between 1 and 100 inclusive and return that int. *******************************************************/ public static int generateNumber() { return (int)(Math.random() * 100) + 1; } /******************************************************* The playTurn() method receives a number that says which turn we are playing. We will need this method to return true or false telling the caller whether the player won the game. This method prompts the player for their guess. It reads the input and validates it. If the input is not valid a message is displayed and the method returns false. If the input is valid then this method determines if the player's guess is correct, too high, or too low. A message is displayed accordingly. If the guess was the correct number the method returns true, otherwise false. *******************************************************/ public static boolean playTurn(int turn) { // Prompt player for their guess and capture it. int guess = getGuess(turn); // validate the guess boolean isValid = validateInput(guess); // Handle the case where the input is invalid. if (!isValid) { System.out.println("Please guess a number between 1 and 100 inclusive"); return false; } // If we made it here then the input was valid so compare it to // the correct number if (guess == number) { System.out.println("Congratulations! You win!"); return true; } else { // Print proper message and return false if (number < guess) { System.out.println("The correct number is lower"); } else { System.out.println("The correct number is higher"); } return false; } } /******************************************************* The getGuess() method receives 1 parameter as input. That is the current turn number. It prompts the player for a number between 1 and 100 and returns the response from the user as an int. *******************************************************/ public static int getGuess(int turn) { // We will need a TextInput object to read in the number TextInput tin = new TextInput(); System.out.print("Turn " + turn + ": Enter an integer between 1 and 100 inclusive. "); return tin.readInt(); } /******************************************************* The validateInput method receives the guess that the player entered and returns true if the number lies between 1 and 100 inclusive, false otherwise. *******************************************************/ public static boolean validateInput(int myGuess) { if ((myGuess < 1) || (myGuess > 100)) { return false; } else { return true; } } }