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

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

  • In Java You’re going to make a Guess the Number game. The computer will "think" of...

    In Java You’re going to make a Guess the Number game. The computer will "think" of a secret number from 1 to 20 and ask the user to guess it. After each guess, the computer will tell the user whether the number is too high or too low. The user wins if they can guess the number within six tries. The program should look like this in the console, player input is in bold: Hello! What is your name? Abaddon...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

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
ADVERTISEMENT