Question

7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number”...

7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number” as follows: Your app chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The app displays the prompt "Guess a number between 1 and 1000: ". The player inputs a first guess. If the player’s guess is incorrect, your app should display Too high. Try again. or Too low. Try again. to help the player “zero in” on the correct answer. The app should prompt the user for the next guess. When the user enters the correct answer, display Congratulations. You guessed the number! and allow the user to choose whether to play again. [Note: The guessing technique employed in this problem is using a similar strategy to a binary search, which is discussed in using c#

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

using System;
class Program {

    static void Main(string[] args) {
        //creating a random number generator
        Random rand = new Random();
        //generating a random number between 1 and 1000 and storing
        int secretNumber=(rand.Next()%1000)+1;
        //initializing userGuess to 0
        int userGuess=0;
        //looping as long as user guess is not same as secret number
        while(userGuess!=secretNumber){
            //asking and reading guess
            Console.Write("\nGuess a number between 1 and 1000: ");
            userGuess=int.Parse(Console.ReadLine());
            //checking if guess is correct
            if(userGuess==secretNumber){
                //printing success message
                Console.WriteLine("Congratulations. You guessed the number!");
                //asking if player wants to play again
                Console.Write("\nDo you want to play again? (y/n): ");
                string ch=Console.ReadLine().ToLower();
                if(ch.Equals("y")){
                    //generating another secret number and starting userGuess with value 0
                    secretNumber=(rand.Next()%1000)+1;
                    userGuess=0;
                }
            }else if(userGuess<secretNumber){
                //low
                Console.WriteLine("Too low. Try again.");
            }else{
                //high
                Console.WriteLine("Too high. Try again.");
            }
        }
        
        //waiting for a key press to quit, remove if you dont need.
        Console.ReadKey();
    }
}

/*OUTPUT*/

Guess a number between 1 and 1000: 500

Too high. Try again.

Guess a number between 1 and 1000: 250

Too low. Try again.

Guess a number between 1 and 1000: 375

Too high. Try again.

Guess a number between 1 and 1000: 300

Too low. Try again.

Guess a number between 1 and 1000: 325

Too high. Try again.

Guess a number between 1 and 1000: 310

Too high. Try again.

Guess a number between 1 and 1000: 302

Too low. Try again.

Guess a number between 1 and 1000: 305

Congratulations. You guessed the number!

Do you want to play again? (y/n): y

Guess a number between 1 and 1000: 500

Too high. Try again.

Guess a number between 1 and 1000: 300

Too low. Try again.

Guess a number between 1 and 1000: 350

Too low. Try again.

Guess a number between 1 and 1000: 400

Too low. Try again.

Guess a number between 1 and 1000: 450

Too low. Try again.

Guess a number between 1 and 1000: 490

Too high. Try again.

Guess a number between 1 and 1000: 480

Too high. Try again.

Guess a number between 1 and 1000: 475

Too high. Try again.

Guess a number between 1 and 1000: 460

Too low. Try again.

Guess a number between 1 and 1000: 465

Too high. Try again.

Guess a number between 1 and 1000: 462

Too high. Try again.

Guess a number between 1 and 1000: 461

Congratulations. You guessed the number!

Do you want to play again? (y/n): n

Add a comment
Know the answer?
Add Answer to:
7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number”...
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
  • 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...

  • Number Guessing Game Games Write program in C++. For this game, the computer will select a...

    Number Guessing Game Games Write program in C++. For this game, the computer will select a random number between 1 and 100 (inclusive). The computer will then ask the (human) player to guess the number the computer has selected. After the player’s guess is input to the computer, the computer will output one of three responses, depending on the relationship of the number the player guessed to the number the computer selected: “Your guess was too low.” “Your guess was...

  • Swift program: Develop an app that is a Number Guessing Game, (ex: one that allows a...

    Swift program: Develop an app that is a Number Guessing Game, (ex: one that allows a player to guess a number between a high and a low number, say between 1 and 10) with the system guiding the player by indicating whether the number is too high or too low after an incorrect guess. This process is repeated until the player correctly guesses the number or simply quit your app. For simplicity purposes, assume the magic number to be guessed...

  • Write a program that prompts the user for an integer that the player (maybe the user,...

    Write a program that prompts the user for an integer that the player (maybe the user, maybe someone else) will try to guess. If the player's guess is higher than the target number, the program should display "too high" If the user's guess is lower than the target number, the program should display "too low" The program should use a loop that repeats until the user correctly guesses the number. Then the program should print how many guesses it took....

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • 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...

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • python Exercise: ex9.py Write a program that plays a number and you guess the number. The...

    python Exercise: ex9.py Write a program that plays a number and you guess the number. The program helps you with a response 1t the number you guessed is higher or lower than the mystery number. To generate a random number, we need to import some routines. guessing game. It generates a random mystery import random mysteryNumber random.randint (1, 100) The random.randint (1, 100) creates a random integer between 1 and 100. Here is a sample output: Let's play the guessing...

  • Write a guessing game where the user has to guess a secret number. After every guess...

    Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively. Using PYTHON.

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