Developing Guess game in C++ step by step
Objectives: learn loops, input/output, if statement, random numbersIn this article we will develop Guess game step by step. The rules of the game is quite simple:
- Computer proposes number in range 1..1000 and a player should guess it.
- At every step of the game, player tells to the computer his assumption about a number and computer tells, if player guessed it right. Otherwise, computer tells the player if his number is less or more, than the proposed number and player tries again.
Main function
First of all we should write a correct main function:
int main() {
return 0;
}
Propose a number
Then the computer should propose a number. We will do it, using rand() function. It returns a number in range from 0 to RAND_MAX (which is quite big). In order to put the number in 1..1000 range, we can use remainder operator:
#include <cstdlib>
int main() {
int number;
number = rand() % 1000 + 1;
return 0;
}
cstdlib library is required in order to use rand() function.
Ask player for his guess
Next, we should ask player for his guess.
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
int number;
number = rand() % 1000 + 1;
int guess;
cout << "Enter your estimate: ";
cin >> guess;
return 0;
}
iostream library allows us to use input/output functions.
Compare player's estimate with the proposed number
Compare player's estimate with the secret number and tell him the result.
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
int number;
number = rand() % 1000 + 1;
int guess;
cout << "Enter your estimate: ";
cin >> guess;
if (guess < number)
cout << "Your estimate is less, than the secret number" << endl;
else if (guess > number)
cout << "Your estimate is more, than the secret number" << endl;
else
cout << "Your guess is right!" << endl;
system("PAUSE");
return 0;
}
The line: system("PAUSE"); prevents console window from closing.
Keep asking
If player didn't guess the secret number right, the program should keep asking him:
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
int number;
number = rand() % 1000 + 1;
int guess;
do {
cout << "Enter your estimate: ";
cin >> guess;
if (guess < number)
cout << "Your estimate is less, than the secret number" << endl;
else if (guess > number)
cout << "Your estimate is more, than the secret number" << endl;
else
cout << "Your guess is right!" << endl;
} while (guess != number);
system("PAUSE");
return 0;
}
Now the game is complete. Last step to make it shine, is to change random seed. Actually, rand() function always returns the same number and it makes the game boring. To change the seed, we should call srand function.
#include <cstdlib>
#include <time.h>
#include <iostream>
using namespace std;
int main() {
srand(time(0));
int number;
number = rand() % 1000 + 1;
int guess;
do {
cout << "Enter your estimate: ";
cin >> guess;
if (guess < number)
cout << "Your estimate is less, than the secret number" << endl;
else if (guess > number)
cout << "Your estimate is more, than the secret number" << endl;
else
cout << "Your guess is right!" << endl;
} while (guess != number);
system("PAUSE");
return 0;
}
Final source code
#include <cstdlib>
#include <time.h>
#include <iostream>
using namespace std;
int main() {
srand(time(0));
int number;
number = rand() % 1000 + 1;
int guess;
do {
cout << "Enter your estimate: ";
cin >> guess;
if (guess < number)
cout << "Your estimate is less, than the secret number" << endl;
else if (guess > number)
cout << "Your estimate is more, than the secret number" << endl;
else
cout << "Your guess is right!" << endl;
} while (guess != number);
system("PAUSE");
return 0;
}
Contribute to AlgoList
Liked this tutorial? Please, consider making a donation. Contribute to help us keep sharing free knowledge and write new tutorials.
Every dollar helps!