In: Computer Science
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
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