Question

In: Computer Science

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.       Excellent! You guessed the number!

          If the number of guesses is 10 or fewer, print “Either you know the secret or you got lucky!” If the player guesses the number in 10 tries, then print “Ahah! You know the secret!” If the player makes more than 10 guesses, then print, “You should be able to do better!”

          Would you like to play again (y or n)?

2.       Too low. Try again.

3.       Too high. Try again.

If the player’s guess is incorrect, your program should loop until the player finally gets the number right. Your program should keep telling the player Too high or Too low to help the player “zero in” on the correct answer. You must count the number of guesses the player makes

Your player must play this game at least 3 times


c++ . i have to use 2 function and don't use array

Solutions

Expert Solution

Code is explained below with the help of comments:

#include <iostream>
#include<time.h>

using namespace std;

//first function to generate a random number in the given range
int generate_num(int lower,int upper){
    return rand()%upper+lower;
}

//second function to compare the guesses number with the generated number
void checkGuess(int guess, int num){
    if(guess<num){
        cout<<"Too low. Try again"<<endl;
    }
    else{
        cout<<"Too high. Try again."<<endl;
    }
}


int main()
{
    // initialize random seed to generate different random numbers every time
    srand (time(NULL));
    
    //variable to store the number of times games has been played
    int gameCount=0;
    //infinite while loop
    while(true){
        //increase the game count by 1 every time the game is played
        gameCount+=1;
        cout<<"I have a number between 1 and 1000.\nCan you guess my number ? "<<endl;
        cout<<"Type in your first guess : "<<endl;
        //generate a random number between 1 and 1000
        int num = generate_num(1,1000); 
        //take input for user's guess
        int guess;
        
        //counter variable to store the number of guesses made by the user
        int count=0;
        while(true){
            //take input from user
            cin>>guess;
            //increase the count by 1
            count+=1;
            //compare the guessed and the generated number
            checkGuess(guess,num);
            //if the number has been guesses correctly then break the loop
            if(guess==num){
                break;
            }
        }
        //uncomment the following line to print the number of guesses
        //cout<<count<<endl;
        cout<<"Excellent! You guessed the number!"<<endl;
        //display a message according to the number of guesses made to reach the final number
        if(count<10){
            cout<<"Either you know the secret or you got lucky!"<<endl;
        }
        else if(count==10){
            cout<<"Ahah! You know the secret!"<<endl;
        }
        else{
            cout<<"You should be able to do better!"<<endl;
        }
        //take input for user's choice
        cout<<"Would you like to play again (y or n)?"<<endl;
        
        char choice;
        cin>>choice;
        //the user must play the game atleast 3 times, therefore start the game again
        //even if the choice is no and count is  less than 3
        if(choice=='n' && gameCount<3){
            cout<<"You must play the game at least 3 times. Starting the game again."<<endl;
            continue;
        }
        //if the user has already played the game 3 times then he can quit
        else if(choice=='n' && gameCount>=3){
            cout<<"Goodbye!"<<endl;
            break;
        }
        //start the game again if choice is yes
        else{
            continue;
        }
    }
   
    return 0;
}

Explaination:

Sample Output


Related Solutions

PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
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...
(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses...
(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The script displays the prompt Guess a number between 1 and 1000 next to a text field. The player types a first guess into the text field and clicks a button to submit the guess to the script. If the player's guess is incorrect, your program should display...
Two persons (A & B) play guess the number game. First person choses an integer in...
Two persons (A & B) play guess the number game. First person choses an integer in the range [1,10] and second person has to guess it by asking questions that the first person can only answer in yes or no. a) Person B asks questions in the sequence, “is it x?” where x=1, 2, …, 10 e.g. first question would be, is it number 1. If no, 2nd question would be, is it number 2, etc. Find the expected number...
Write a program in Basic to play the game of Nim with acom­puter.
Write a program in Basic to play the game of Nim with acom­puter.
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
Write a program that allows 2 players to play the marble game using combinations of loop...
Write a program that allows 2 players to play the marble game using combinations of loop and conditional statements. In the marble game, you ask the user to enter how many marbles to start with. Then, the game begins. The first player must take 1, 2 or 3 marbles. Then the second player goes and must take 1, 2 or 3 marbles. The winner is the player who takes the last marble. Allow two users to play this game and...
Write a game where a user has to guess a number from 1 – 6, inclusive....
Write a game where a user has to guess a number from 1 – 6, inclusive. Your program will generate a random number once (pick a number), and will then prompts the user to guess the number for up to 3 times. If the user enters 3 wrong guesses, the program should be terminated along with the losing message. Once the user has successfully guessed the number, tell the user they win, and tell them how many guesses it took...
Write a program where a user of this program will play a game in which he/she...
Write a program where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT