Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 number or hits the maximum number of allowed guesses or enters the "quit" sentinel value 

The rest of this specification documents number guessing games in more detail. The documentation uses manifest constants that can be used by your program.

Playing a Guessing Game

Use a class Random object to get a random number between MIN_NUMBER (1) and MAX_NUMBER (205), inclusive.

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 MAX_GUESSES (10) wrong guesses - print LOSER_MSG along with the random number + the user enters QUIT_VALUE (-1) - 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 (-314), print the random number and re-prompt the user to enter a guess. BACKDOOR_VALUE input is not counted as a wrong guess.

Print a INPUT_TOO_SMALL_MSG or INPUT_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 are not counted as wrong guess.

Print a NOPE_MSG when the user enters a wrong guess. At HINT_THRESHOLD (5) 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 (4), then do "Post Game Playing Processing." Otherwise, issue the PLAY_AGAIN_PROMPT. Do "Post Game Playing Processing" if user enters 'n', else start new game.

Post Game Playing Processing

Print the following prior to exiting the program.

number of games played

number of games won

number of games lost

number of games quit

winning percentage

Also print game summaries for each game played.

games played: 3; won: 1; lost: 1; quit: 1; winning pct.: 33.33% game 1: Won; the number was: 145; #guesses: 6; backdoored: true ...the guesses were: 1,2,3,4,5, game 2: Quit; the number was: 49; #guesses: 3; backdoored: false ...the guesses were: 1,2,3, game 3: Lost; the number was: 182; #guesses: 10; backdoored: true ...the guesses were: 1,5,144,150,181,183,191,199,200,201, 

Manifest Constants

Your program can implements I_GG to use the manifest constants defined in the interface. I_GG can be copied/pasted after (or before) the declaration of your class.

To Support Testing (i.e. test mode)

If the program is executed with a command-line argument, then the random number for each game is DFLT_NUMBER (60).

The motivation for supporting test mode is to play games without having to play games.

 shell-prompt: cat gg.in -314 10 20 30 foo 40 50 70 60 y 10 20 10 0 210 -1 y 1 2 3 4 5 6 7 8 9 10 11 12 y 1 2 3 1 2 60 w y shell-prompt: cat gg.in | java GG testing > gg.out 

Example Games

*** Hello! Have fun playing the CSC205 guessing game. *** enter a guess between 1 and 205 (-1 to quit): 1 // ENTER_GUESS_PROMPT nope... // NOPE_MSG 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 // NOPE_MSG with hint enter a guess between 1 and 205 (-1 to quit): 3 you've already guessed that wrong guess... // NOPE_NOPE_MSG enter a guess between 1 and 205 (-1 to quit): 314 *** invalid input -- must be less than 206 // INPUT_TOO_LARGE_MSG enter a guess between 1 and 205 (-1 to quit): -5 *** invalid input -- must be greater than 0 // INPUT_TOO_SMALL_MSG enter a guess between 1 and 205 (-1 to quit): -314 ...the number is 145 enter a guess between 1 and 205 (-1 to quit): 145 you're a winner... # of guesses: 6 // WINNER_MSG Do you want to play again (n or y)? y // PLAY_AGAIN_PROMPT 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): -1 you're a quitter... the number was 49 // QUITTER_MSG Do you want to play again (n or y)? what *** invalid input -- must be n or y // NOT_YN_MSG Do you want to play again (n or y)? y enter a guess between 1 and 205 (-1 to quit): -314 ...the number is 182 enter a guess between 1 and 205 (-1 to quit): 150 nope... enter a guess between 1 and 205 (-1 to quit): 200 nope... enter a guess between 1 and 205 (-1 to quit): 1 nope... enter a guess between 1 and 205 (-1 to quit): 5 nope... enter a guess between 1 and 205 (-1 to quit): 200 you've already guessed that wrong guess... enter a guess between 1 and 205 (-1 to quit): 206 *** invalid input -- must be less than 206 enter a guess between 1 and 205 (-1 to quit): 0 *** invalid input -- must be greater than 0 enter a guess between 1 and 205 (-1 to quit): 144 nope... higher enter a guess between 1 and 205 (-1 to quit): foo *** invalid input -- must be an whole number enter a guess between 1 and 205 (-1 to quit): 201 nope... lower enter a guess between 1 and 205 (-1 to quit): 183 nope... lower enter a guess between 1 and 205 (-1 to quit): 200 you've already guessed that wrong guess... enter a guess between 1 and 205 (-1 to quit): 199 nope... lower enter a guess between 1 and 205 (-1 to quit): -314 ...the number is 182 enter a guess between 1 and 205 (-1 to quit): 181 nope... higher enter a guess between 1 and 205 (-1 to quit): 191 too many guesses entered... the number was 182 // LOSER_MSG Do you want to play again (n or y)? n *** Thanks for playing the CSC205 guessing game. *** games played: 3; won: 1; lost: 1; quit: 1; winning pct.: 33.33% game 1: Won; the number was: 145; #guesses: 6; backdoored: true ...the guesses were: 1,2,3,4,5, game 2: Quit; the number was: 49; #guesses: 3; backdoored: false ...the guesses were: 1,2,3, game 3: Lost; the number was: 182; #guesses: 10; backdoored: true ...the guesses were: 1,5,144,150,181,183,191,199,200,201, 

Example Games (second round to test MAX_GAMES played)

*** Hello! Have fun playing the CSC205 guessing game. *** enter a guess between 1 and 205 (-1 to quit): -1 you're a quitter... the number was 157 Do you want to play again (n or y)? y enter a guess between 1 and 205 (-1 to quit): -1 you're a quitter... the number was 4 Do you want to play again (n or y)? y enter a guess between 1 and 205 (-1 to quit): -314 ...the number is 35 enter a guess between 1 and 205 (-1 to quit): 35 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): -1 you're a quitter... the number was 118 Maximum number (4) of games have been played. *** Thanks for playing the CSC205 guessing game. *** games played: 4; won: 1; lost: 0; quit: 3; winning pct.: 25.00% game 1: Quit; the number was: 157; #guesses: 0; backdoored: false game 2: Quit; the number was: 4; #guesses: 0; backdoored: false game 3: Won; the number was: 35; #guesses: 1; backdoored: true game 4: Quit; the number was: 118; #guesses: 0; backdoored: false 

Example Games (test mode)

*** Hello! Have fun playing the CSC205 guessing game. *** enter a guess between 1 and 205 (-1 to quit): -314 ...the number is 60 enter a guess between 1 and 205 (-1 to quit): 10 nope... enter a guess between 1 and 205 (-1 to quit): 20 nope... enter a guess between 1 and 205 (-1 to quit): 30 nope... enter a guess between 1 and 205 (-1 to quit): foo *** invalid input -- must be an whole number enter a guess between 1 and 205 (-1 to quit): 40 nope... enter a guess between 1 and 205 (-1 to quit): 50 nope... higher enter a guess between 1 and 205 (-1 to quit): 70 nope... lower enter a guess between 1 and 205 (-1 to quit): 60 you're a winner... # of guesses: 7 Do you want to play again (n or y)? y enter a guess between 1 and 205 (-1 to quit): 10 nope... enter a guess between 1 and 205 (-1 to quit): 20 nope... enter a guess between 1 and 205 (-1 to quit): 10 you've already guessed that wrong guess... enter a guess between 1 and 205 (-1 to quit): 0 *** invalid input -- must be greater than 0 enter a guess between 1 and 205 (-1 to quit): 210 *** invalid input -- must be less than 206 enter a guess between 1 and 205 (-1 to quit): -1 you're a quitter... the number was 60 Do you want to play again (n or y)? y 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): 6 nope... higher enter a guess between 1 and 205 (-1 to quit): 7 nope... higher enter a guess between 1 and 205 (-1 to quit): 8 nope... higher enter a guess between 1 and 205 (-1 to quit): 9 nope... higher enter a guess between 1 and 205 (-1 to quit): 10 too many guesses entered... the number was 60 Do you want to play again (n or y)? 11 *** invalid input -- must be n or y Do you want to play again (n or y)? 12 *** invalid input -- must be n or y Do you want to play again (n or y)? y 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): 1 you've already guessed that wrong guess... enter a guess between 1 and 205 (-1 to quit): 2 you've already guessed that wrong guess... enter a guess between 1 and 205 (-1 to quit): 60 you're a winner... # of guesses: 4 Maximum number (4) of games played. *** Thanks for playing the CSC205 guessing game. *** games played: 4; won: 2; lost: 1; quit: 1; winning pct.: 50.00% game 1: Won; the number was: 60; #guesses: 7; backdoored: true ...the guesses were: 10,20,30,40,50,70, game 2: Quit; the number was: 60; #guesses: 2; backdoored: false ...the guesses were: 10,20, game 3: Lost; the number was: 60; #guesses: 10; backdoored: false ...the guesses were: 1,2,3,4,5,6,7,8,9,10, game 4: Won; the number was: 60; #guesses: 4; backdoored: false ...the guesses were: 1,2,3, ______________________________________________________________________________________________ 

interface I_GG { int MIN_NUMBER = 1; int MAX_NUMBER = 205; int BACKDOOR_NUMBER = -314; int DFLT_NUMBER = 60; int WON_VALUE = 0; int QUIT_VALUE = -1; int LOSE_VALUE = -2; int MAX_GAMES = 4; int MAX_GUESSES = 10; int HINT_THRESHOLD = 5; char NO = 'n', NO_UPPER = 'N'; char YES = 'y', YES_UPPER = 'Y'; String NOPE_MSG = "nope..."; String NOPE_NOPE_MSG = "you've already guessed that wrong guess..."; String INVALID_INPUT_BEGIN = "*** invalid input -- "; String INPUT_TOO_LOW_MSG = INVALID_INPUT_BEGIN + "must be greater than " + (MIN_NUMBER - 1); String INPUT_TOO_HIGH_MSG = INVALID_INPUT_BEGIN + "must be less than " + (MAX_NUMBER + 1); String INPUT_NOT_INT_MSG = INVALID_INPUT_BEGIN + "must be an whole number"; String NOT_YN_MSG = INVALID_INPUT_BEGIN + "must be " + NO + " or " + YES; String WINNER_MSG = "you're a winner... # of guesses: "; String LOSER_MSG = "too many guesses entered... the number was "; String QUITTER_MSG = "you're a quitter... the number was "; String MAX_GAMES_PLAYED_MSG = " Maximum number (" + MAX_GAMES + ") of games played."; String ENTER_GUESS_PROMPT = "enter a guess between " + MIN_NUMBER + " and " + MAX_NUMBER + " (" + QUIT_VALUE + " to quit): "; String PLAY_AGAIN_PROMPT = " Do you want to play again (" + NO + " or " + YES + ")? "; String BOLD_BEGIN = "*** "; String BOLD_END = " ***"; String PLAY_MSG = " playing the CSC205" + " guessing game." + BOLD_END; String WELCOME_MSG = BOLD_BEGIN + "Hello! Have fun" + PLAY_MSG; String THANKS_MSG = BOLD_BEGIN + "Thanks for" + PLAY_MSG; String GUESSES_DUMP = "...the guesses were: "; String WINNER_RESULT = "Won"; String QUITTER_RESULT = "Quit"; String LOSER_RESULT = "Lost"; // printf() format strings... String GAME_STATS_FMT = "games played: %d; won: %d; lost: %d; quit: %d;" + " winning pct.: %.2f%% "; String GAME_DUMP_FMT = "game %d: %s; the number was: %d; " + "#guesses: %d; backdoored: %s "; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

What are the benefits of using computers in marketing?

Answered: 1 week ago

Question

How does or how might the key public affect your organization?

Answered: 1 week ago