Question

In: Computer Science

Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....

  1. Number guessing Game

Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function

srand(time(NULL));

int r = rand() % 100 + 1;

that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if the entered number is larger or smaller than its number. The user then keeps guessing till he/she finds the number.

If the user doesn’t find the number after 10 guesses, a proper game over message will be shown and the actual guess is revealed. If the user makes a correct guess in its allowed 10 guesses, then a proper message will be shown and the number of guesses the user made to get the correct answer is also printed.

After each correct guess or game over, the user decides to play again or quit and based on the user choice, the computer will make another guess and goes on or prints a proper goodbye message and quits A sample run is as follows (user inputs are in red):

Welcome to the Number Guess Game!
I choose a number between 1 and 100 and you have only 10 chanced to guess it!

OK, I made my mind!

What is your guess? > 5

My number is larger than 5!

9 guesses left.

What is your guess? > 75

My number is smaller than 75!

8 guesses left.

What is your guess? > 40

My number is smaller than 40!

7 guesses left.

What is your guess? > 23

My number is larger than 23!

6 guesses left.

What is your guess? > 30

My number is larger than 30!

5 guesses left.

What is your guess? > 35

You did it! My number is 35!
You found it with just 6 guesses.

Do you want to play again? (Y/N) > Y

OK, I made my mind!

What is your guess? > 15

My number is larger than 15!

9 guesses left.

And if the user makes 10 guesses without a success

What is your guess? > 405

Please enter a number between 1 and 100

What is your guess? > 45

My number is larger than 45!

1 guess left.

What is your guess? > 47

SORRY! You couldn’t find it with 10 guesses!

My number was 46. Maybe next time!

Do you want to play again? (Y/N) > N

Thanks for playing! See you later.

Solutions

Expert Solution


#include <stdio.h>
#include <time.h>
int main()
{
    srand(time(NULL));
    printf("Welcome to the Number Guess Game!\n");
    printf("I choose a number between 1 and 100 and you have only 10 chances to guess it!\n");
    char choice;
   choice = 'Y';
    while(choice == 'Y')
    {
        int num_of_guess = 0;
        int number = rand() % 100 + 1;
        int guess;
        printf("OK! I made my mind!\n");
        do
        {
            printf("What is your guess? > ");
            scanf("%d",&guess);
            if(guess>100)
            {
                printf("Please enter a number between 1 and 100\n");
            }
            else
            {
                num_of_guess++;
                if(num_of_guess>=10)
                {
                    printf("SORRY! You couldn't find it with 10 guesses!\n");
                    printf("My number was %d. Maybe next time!\n",number);
                }
                else
                {
                    if(guess==number)
                    {
                        printf("You did it! My number is %d!\n",number);
                        printf("You found it with just %d guesses.\n",num_of_guess);
                    }
                    else if(guess<number)
                    {
                        printf("My number is larger than %d!\n",guess);
                        printf("%d guesses left.\n",10-num_of_guess);
                    }
                    else
                    {
                        printf("My number is smaller than %d!\n",guess);
                        printf("%d guesses left.\n",10-num_of_guess);
                    }
                }
            }
        }while(num_of_guess<10 && guess!=number);
      
        printf("Do you want to play again? (Y/N) > ");
        choice = getchar();
    }
    printf("Thanks for playing! See you later.\n");
    return 0;
}


Related Solutions

C++ The program implements the Number Guessing Game. However, in that program, the user is given...
C++ The program implements the Number Guessing Game. However, in that program, the user is given as many tries as needed to guess the correct number. Rewrite the program so that the user has no more than five tries to guess the correct number. Your program should print an appropriate message, such as “You win!” or “You lose!”.
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
Write a program with at least 2 functions that play the game of “guess the number”...
Write a program with at least 2 functions that play the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following: I have a number between 1 and 1000. Can you guess my number? Please type in your first guess. The player then types the first guess. The program then responds with one of the following: 1.      ...
1. [100] Create a C program for a number guessing game. Here are the requirements: a....
1. [100] Create a C program for a number guessing game. Here are the requirements: a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max. b. If the user guesses...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT