Assignment: #GuessingGame Due: 01/31/2017 02/05/2017 Points: 5

Specification

Write a program that enables a user to play number guessing games. The following is a nutshell description of a number guessing game.

   + a random number is generated
   + loop prompting the user to enter guesses
     until the user guesses the numbers or hits
     hits the maximum number of allowed guesses
     or enters the "quit" sentinel value

The rest of this specification explains number guessing games in more detail.

Playing a Guessing Game

Use a class Random object to get a random number between MIN_NUMBER and MAX_NUMBER, inclusive. [Note: The Random object should be instantiated prior to playing the first game.]

Print the ENTER_GUESS_PROMPT and read the user input. Loop until one of the following are true.

   + the user enters the random number 
     - print WINNER_MSG along with number of guesses

   + the user enters wrong guess number MAX_GUESSES
     - print LOSER_MSG along with the random number

   + the user enters QUIT_VALUE
     - print QUITTER_MSG

   The current game ends in all three of these cases.
   (see "End of Game Processing" section)

If the user enters BACKDOOR_VALUE, print the random number and re-prompt the user to enter a guess. BACKDOOR_VALUE input is not counted as wrong guess.

Print a TOO_SMALL_MSG or TOO_LARGE_MSG message when the user enters a number that is either less than MIN_NUMBER or greater than MAX_NUMBER, respectively. Re-prompt the user to enter a guess after printing the message. Invalid inputs are not counted as wrong guesses.

Print a NOPE_NOPE_MSG message when the user enters a wrong guess more than once and re-prompt the user to enter a guess. Duplicate wrong guesses count as only one wrong guess.

Print a NOPE_MSG when the user enter a wrong guess. After HINT_THRESHOLD wrong guesses, print a HIGHER_MSG or LOWER_MSG message. Re-prompt the user to enter a guess.

End of Game Processing

If number of games played equals MAX_GAMES, then do "Post Game Playing Processing." Otherwise, issue the PLAY_AGAIN_PROMPT. Do "Post Game Playing Processing" if users enters 'n', else start new game.

Post Game Playing Processing

Print the following prior to exiting the program.

Required Manifest (Named) Constants

Your program must define the following manifest constants prior to the main() method and they should be used through your code.

   private static final int MIN_NUMBER = 1;
   private static final int MAX_NUMBER = 205;
   private static final int QUIT_VALUE = -1;
   private static final int LOSE_VALUE = -2;
   private static final int MAX_GAMES = 4;
   private static final int MAX_GUESSES = 10;
   private static final int HINT_THRESHOLD = 5;
   private static final int BACKDOOR_VALUE = -314;

   private static final String NOPE_MSG = "nope...";
   private static final String NOPE_NOPE_MSG = 
      "you've already guessed that wrong guess...";
   private static final String INVALID_INPUT_BEGIN = 
      "*** invalid input -- ";
   private static final String INVALID_INPUT_LESS_MIN_MSG = 
      INVALID_INPUT_BEGIN + "must be greater than " + (MIN_NUMBER - 1);
   private static final String INVALID_INPUT_GREATER_MAX_MSG = 
      INVALID_INPUT_BEGIN + "must be less than " + (MAX_NUMBER + 1);
   private static final String INVALID_INPUT_YN_MSG = 
      INVALID_INPUT_BEGIN + "must be n or y";
   private static final String WINNER_MSG = 
      "you're a winner... # of guesses: ";
   private static final String LOSER_MSG = 
      "too many guesses entered... the number was ";
   private static final String QUITTER_MSG = 
      "you're a quitter... the number was ";
   private static final String MAX_GAMES_PLAYED_MSG = 
      "you've played the maximum number (" + MAX_GAMES + ") of games";
   private static final String ENTER_GUESS_PROMPT = 
      "enter a guess between " + MIN_NUMBER + " and " + MAX_NUMBER +
      " (" + QUIT_VALUE + " to quit): ";
   private static final String PLAY_AGAIN_PROMPT = 
      "Do you want to play again (n or y)? ";

   private static final String BOLD_BEGIN = "*** ";
   private static final String BOLD_END = " ***";
   private static final String PLAY_MSG = " playing the CSC" + MAX_NUMBER +
                                          " guessing game." + BOLD_END;
   private static final String WELCOME_MSG = 
      BOLD_BEGIN + "Hello! Have fun" + PLAY_MSG;
   private static final String THANKS_MSG = 
      BOLD_BEGIN + "Thanks for" + PLAY_MSG;
Help Generating Random Number

Instantiate a class Random objecting using the current time on the computer as a seed.

   Random rng = new Random(System.currentTimeMillis());

Generate a random between MIN_NUMBER and MAX_NUMBER.

   int n = MIN_NUMER + rng.nextInt(MAX_NUMBER); 
Example Games
*** Hello! Have fun playing the CSC205 guessing game. ***

enter a guess between 1 and 205 (-1 to quit): -50
*** invalid input -- must be greater than 0

enter a guess between 1 and 205 (-1 to quit): 500
*** invalid input -- must be less than 206

enter a guess between 1 and 205 (-1 to quit): -314
...the number is 183

enter a guess between 1 and 205 (-1 to quit): 1
nope...

enter a guess between 1 and 205 (-1 to quit): 2
nope...

enter a guess between 1 and 205 (-1 to quit): 3
nope...

enter a guess between 1 and 205 (-1 to quit): 4
nope...

enter a guess between 1 and 205 (-1 to quit): 5
nope... higher

enter a guess between 1 and 205 (-1 to quit): 100
nope... higher

enter a guess between 1 and 205 (-1 to quit): 200
nope... lower

enter a guess between 1 and 205 (-1 to quit): 5
you've already guessed that wrong guess...

enter a guess between 1 and 205 (-1 to quit): 150
nope... higher

enter a guess between 1 and 205 (-1 to quit): 3
you've already guessed that wrong guess...

enter a guess between 1 and 205 (-1 to quit): 183

you're a winner... # of guesses: 9

Do you want to play again (n or y)? y

enter a guess between 1 and 205 (-1 to quit): -314
...the number is 9

enter a guess between 1 and 205 (-1 to quit): 9
you're a winner... # of guesses: 1

Do you want to play again (n or y)? y

enter a guess between 1 and 205 (-1 to quit): 21
nope...

enter a guess between 1 and 205 (-1 to quit): -1
you're a quitter... the number was 1

Do you want to play again (n or y)? y

enter a guess between 1 and 205 (-1 to quit): -314
...the number is 173

enter a guess between 1 and 205 (-1 to quit): 170
nope...

enter a guess between 1 and 205 (-1 to quit): 5
nope...

enter a guess between 1 and 205 (-1 to quit): 5
you've already guessed that wrong guess...

enter a guess between 1 and 205 (-1 to quit): 173
you're a winner... # of guesses: 3

you've played the maximum number (4) of games

*** Thanks for playing the CSC205 guessing game. ***

played: 4; won: 3; lost: 0; quit: 1; winning pct.: 75.0