In: Computer Science
C++ Programming level 1
using step number 2 is a must
Guess the Number
Introduction In this assignment you will create a program that will simulate a number guessing game.
Skills: random, while-loop, Boolean flags
Algorithm to win
The guess the number game has an algorithm to lead to the winning value. The way it works is say you have the set of numbers 1 to 100. You always begin by choosing the half-way point, then a hint will be provided if you do not guess correctly. Based on that hint, you shift either the beginning or end point. For instance, for 1 to 100, select 50. Let’s say the system says “higher,” then you set your potential range from 51 to 100. Next, we would choose the half-way point between these two values, so 75. Let’s say that now we are told, “lower.” Our new range becomes 51 to 74. The half-way point would be 62, let’s say that this is our number the game would end.
main.cpp
Your program should follow these steps and generate similar output. Do not change the design and follow naming conventions:
1) Create two constants with appropriate name: smallest value set to 1, largest value set to 100.
2) Create the mt19937 engine and initialize it with the random device as shown in the Boolean lectures.
3) Create a uniform int distribution of type integer and set it to generate values between the constants for smallest and largest values.
4) Create a boolean flag variable called continue game, set it to true.
5) Create the main while loop of the game, and leave the condition as the continue game variable
6) Inside the loop, generate a random number and store it in a variable
7) Declare and initialize a variable for number of guesses, set it to 0
8) Prompt the user for a number as show in the example, you must use the constants to display the output for the numbers, do not use literals in the string
9) Get the initial guess from the user and store it in a variable called guess.
10) Create a Boolean value, is guess correct, and set it to determine if the guess is correct
11) Create another while loop that will loop while the guess is not correct
12) Inside this loop increment the number of guesses (choose the appropriate increment type)
13) Create a string variable, hint, and use the Ternary operator to either store “Lower” if the guess is higher than the random number, or “Higher” otherwise.
14) Display the Hint, and prompt the user again to enter another guess
15) Update the is correct guess variable.
16) Outside the innermost loop, display the Number of guesses it took
17) Prompt the user if they wish to play again and update the continue game Boolean flag.
18) Outside the outermost loop, display a message Thanking the player.
19) Look at the output
Test Run: Your code should look exactly as the one below:
Guess a number between 1 and 100: 50
Hint: Higher
Guess a number between 1 and 100: 75
Hint: Higher
Guess a number between 1 and 100: 82
Hint: Higher
Guess a number between 1 and 100: 90
Hint: Higher
Guess a number between 1 and 100: 95
Hint: Higher
Guess a number between 1 and 100: 99
Hint: Lower
Guess a number between 1 and 100: 97
Number of guesses: 6
Do you want to play again (y/n) ? n
Thank you for playing.
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
int startNum = 1,endNum = 100;//declaration of start,end of the range.
bool play = true;//intially play is true.because user needs to play game at least one time.
char option;//will takes and store wheather game will continue or not.
while(play){
int num = -1,guess = -1,chances = 0;
num = rand()%endNum + 1;//randomly getting the number between 1 to 100.
//loop runs until user guess the number.
do{
cout<<"Guess the number between 1 and 100:";
cin>>guess;
//if guess is wrong then we nedd to display hint and increment the no of chances of the user.
if(guess != num){
cout<<(guess>num?"Higher":"Lower")<<endl;
chances++;
}
}while(guess != num);//come outs from loop if guessed num and num both are same.
//displays the no of chances and asks user to continue or not.
cout<<"Number of guesses:"<<chances<<endl;
cout<<"Do you want to play again(y/n)?";
cin>>option;
//if user option is n.then play becomes false then game will ends
if(option == 'n'){
play = false;
}
}
cout<<"Thank you for playing."<<endl;
return 0;
}
/*
output
Guess the number between 1 and 100:10
Lower
Guess the number between 1 and 100:50
Higher
Guess the number between 1 and 100:42
Number of guesses:2
Do you want to play again(y/n)?y
Guess the number between 1 and 100:50
Lower
Guess the number between 1 and 100:42
Lower
Guess the number between 1 and 100:68
Number of guesses:2
Do you want to play again(y/n)?y
Guess the number between 1 and 100:64
Higher
Guess the number between 1 and 100:42
Higher
Guess the number between 1 and 100:20
Lower
Guess the number between 1 and 100:30
Lower
Guess the number between 1 and 100:35
Number of guesses:4
Do you want to play again(y/n)?y
Guess the number between 1 and 100:35
Higher
Guess the number between 1 and 100:64
Higher
Guess the number between 1 and 100:22
Higher
Guess the number between 1 and 100:10
Higher
Guess the number between 1 and 100:1
Number of guesses:4
Do you want to play again(y/n)?y
Guess the number between 1 and 100:0
Lower
Guess the number between 1 and 100:20
Lower
Guess the number between 1 and 100:30
Lower
Guess the number between 1 and 100:40
Lower
Guess the number between 1 and 100:50
Lower
Guess the number between 1 and 100:80
Higher
Guess the number between 1 and 100:70
Number of guesses:6
Do you want to play again(y/n)?n
Thank you for playing.
*/