Question

Required in JAVA language Write a program that enables a user to play number guessing games....


Required in JAVA language

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
...guesses in ascending order: 1,2,3,4,5,

game 2: Quit; the number was: 49; #guesses: 3; backdoored: false
...guesses in ascending order: 1,2,3,

game 3: Lost; the number was: 182; #guesses: 10; backdoored: true
...guesses in ascending order: 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 CSC205AA 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 CSC205AA 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
...guesses in ascending order: 1,2,3,4,5,

game 2: Quit; the number was: 49; #guesses: 3; backdoored: false
...guesses in ascending order: 1,2,3,

game 3: Lost; the number was: 182; #guesses: 10; backdoored: true
...guesses in ascending order: 1,5,144,150,181,183,191,199,200,201,
Example Games (second round to test MAX_GAMES played)

*** Hello! Have fun playing the CSC205AA 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 CSC205AA 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 CSC205AA 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 CSC205AA 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
...guesses in ascending order: 10,20,30,40,50,70,

game 2: Quit; the number was: 60; #guesses: 2; backdoored: false
...guesses in ascending order: 10,20,

game 3: Lost; the number was: 60; #guesses: 10; backdoored: false
...guesses in ascending order: 1,2,3,4,5,6,7,8,9,10,

game 4: Won; the number was: 60; #guesses: 4; backdoored: false
...guesses in ascending order: 1,2,3,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

GuessingGame.java

import java.util.Scanner;
import java.util.Random;
public class GuessingGame {

    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 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;
  
    static Random rng = new Random(System.currentTimeMillis());
    static Scanner stdin = new Scanner(System.in);
    static Scanner scan = new Scanner(System.in);
          
    static int guess;
    static int numberOfGames = 0;
    static int numberOfWins = 0;
    static int numberOfLoses = 0;
    static boolean endStart = true; // Check if this boolean is used
  
        public static void main(String[] args) {
      
            PlayGame();
            PostGame();
            EndGame();
        }
      
        public static boolean check(int item, int[] chekarray){
            for (int i = 0; i < chekarray.length - 1; i++){
                if (chekarray[i] == item)
                    return true;
          
       }
       return false;
        }
      
        public static void PlayGame() {
      
            int guessCounter = 0;
            int n = MIN_NUMBER + rng.nextInt(MAX_NUMBER);
            int[] history = new int[10];
            boolean found = false;
          
            System.out.println(WELCOME_MSG);
            boolean test = true;
            do{
                found = false;
                System.out.println();
                System.out.print(ENTER_GUESS_PROMPT);
                guess = stdin.nextInt();
              
                if (check(guess, history)){
                    found = true;
       }

                history[guessCounter] = guess;
                guessCounter = guessCounter + 1;
              
                if(guess < MIN_NUMBER && guess != BACKDOOR_VALUE &&
                        guess != QUIT_VALUE){
                    if (found) {
                        System.out.print(NOPE_NOPE_MSG + "\n");
           guessCounter--;
                    } else {
           System.out.println(INVALID_INPUT_LESS_MIN_MSG + "\n");
                        guessCounter--;
                    }
                }
              
                if(guess > MAX_NUMBER){
                    if (found) {
           System.out.print(NOPE_NOPE_MSG + "\n");
                            guessCounter--;
                    } else {
           System.out.println(INVALID_INPUT_GREATER_MAX_MSG +
                                "\n");
                        guessCounter--;
                    }
       }
              
                // Giving the user the answer
                if(guess == BACKDOOR_VALUE){
                    System.out.println(n);
                        guessCounter--;
                }
              
                // Quiting the round and giving the chance to end the game
                if(guess == QUIT_VALUE){
                    System.out.println((QUITTER_MSG + n) + "\n" +
                            PLAY_AGAIN_PROMPT);
                    numberOfLoses++;
                    String val = scan.next();
                        if(val.equalsIgnoreCase("y")){
                            numberOfGames++;  
                                PlayGame();
                        }
                      
                        if(val.equalsIgnoreCase("n")){
                            System.out.println(THANKS_MSG);
                            test = false;
                            PostGame();
                            break;
                        }
                      
                      
                }
              
                // Correct guess on the last try
                if(guessCounter == MAX_GUESSES && guess == n){
                    numberOfGames++;
                    System.out.println(WINNER_MSG + guessCounter);
                        numberOfWins++;
                        System.out.println(PLAY_AGAIN_PROMPT);
                        String val = scan.next();
                        if(val.equalsIgnoreCase("y"))
                                PlayGame();  
                        if(val.equalsIgnoreCase("n")){
                            System.out.println(THANKS_MSG);
                            test = false;
                            PostGame();
                            break;
                        }
                }
              
                // Max games
                if(numberOfGames == MAX_GAMES){
                    System.out.println(MAX_GAMES_PLAYED_MSG);
                    test = false;
                    PostGame();
                    break;
                }
              
                // Max guesses
                if(guessCounter == MAX_GUESSES){
                    numberOfGames++;
                    System.out.println(LOSER_MSG+ n + "\n" +PLAY_AGAIN_PROMPT);
                    numberOfLoses++;
                    String val = scan.next();
                        if(val.equalsIgnoreCase("y"))
                                PlayGame();  
                        if(val.equalsIgnoreCase("n")){
                            System.out.println(THANKS_MSG);
                            test = false;
                            PostGame();
                            break;
                        }
                }
              
                // Guessing in the range of the bonds
                    if(guess > MIN_NUMBER || guess < MAX_NUMBER){
                        if (found) {
                            System.out.print(NOPE_NOPE_MSG + "\n");  
                                guessCounter -= 1;
                        } else {
                            System.out.print(NOPE_MSG);
                        }
                        if(guess == n){
                            numberOfGames++;
                            System.out.println(WINNER_MSG + guessCounter);
                            numberOfWins++;
                            System.out.println(PLAY_AGAIN_PROMPT);
                            String val = scan.next();
                            if(val.equalsIgnoreCase("y"))
                                    PlayGame();
                            if(val.equalsIgnoreCase("n")){
                                System.out.println(THANKS_MSG);
                                test = false;
                                PostGame();
                                break;
                            }
                        }  
                    }
                  
                // Giving hints after 5 tries until the max # of guesses
                if(guessCounter == HINT_THRESHOLD ||
                    guessCounter == (HINT_THRESHOLD + 1) ||
                    guessCounter == (HINT_THRESHOLD + 2) ||
                    guessCounter == (HINT_THRESHOLD + 3) ||
                    guessCounter == (HINT_THRESHOLD + 4) ||
                    guessCounter == (HINT_THRESHOLD + 5)){
                    if(guess != n && guess > n){
                        System.out.println("lower");
                    }
                    if(guess != n && guess < n){
                        System.out.println("higher");
                    }
                }
                  
            }
            while(guess > MIN_NUMBER || guess < MAX_NUMBER);
        }
      
        // Post game information
        public static void PostGame() {
            int winningPct = 0;
            System.out.print("Played: " + numberOfGames + ";" +
                    " Won: " + numberOfWins + ";" +
                    " Lost: " + numberOfLoses + ";");
            if(numberOfGames != 0){
                System.out.println(" winningPct: " +
                        (winningPct = numberOfWins/numberOfGames));
                EndGame();
            }
            else{
            winningPct =0;}
            EndGame();
        }
      
        // Ending the game
        public static void EndGame(){
          
        }

}


Add a comment
Know the answer?
Add Answer to:
Required in JAVA language Write a program that enables a user to play number guessing games....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Specification Write a program that allows the user to play number guessing games. Playing a Guessing...

    Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • How can I do this program complete with input validation so that the computer prompts the...

    How can I do this program complete with input validation so that the computer prompts the user to enter a number 0 through 100 if he/she guesses anything lower than 0, a floating point number, or other characters? And later, prompts the user to enter yes or no if he/she enters anything else when the program prompts the user to play again? Write a program that plays the Hi-Lo guessing game with numbers. The program should pick a random number...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

  • Hello, Could you please explain how I would complete this program with input validation to ensure...

    Hello, Could you please explain how I would complete this program with input validation to ensure that an error message will not appear if the user enters something other than an integer that is not 1-100 and later if they enter anything other than yes and no? Here is the program: Write a program that plays the Hi-Lo guessing game with numbers. The program should pick a random number between 1 and 100 (inclusive), then repeatedly promt the user to...

  • Please write a C# program that where a player will play a guessing game with the...

    Please write a C# program that where a player will play a guessing game with the range of 1-100. Each time the play guesses a number, and it is not correct the program should indicate if the number is more or less than what the player guessed. The play should be able to guess until the correct number has been guessed. If an invalid number is entered, such as: 1.24 (real number), or asd (string). The program should tell user...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT