Game in C++: Guess the number drawn
Create a game where the computer draws a number from 1 to 100. Each time you guess a number, it should tell you whether you guessed below the real value, above or if you got it right. At the end, it tells you the number of attempts you had and whether or not you hit the record. Ah, at the end of each round, the program asks if you want to play again or not, displaying the current record.Commented game code in C++
Well, come on, let's go calmly and carefully, today we're going to go over 100 lines of code.
But don't be alarmed, a big program is nothing more than several small programs divided, among other things, between functions that do specific things.
Initially, we declare the record variable, which will store the record number of attempts the user has made in a round. It starts at 0. Soon you will understand why.
Then, we create the clean() function, which will clean the screen with each round.
The gen() function causes the computer to generate a random number, from 1 to 100, and returns it.
The guess() function will be used to receive the user's guess. It receives an integer, the number of attempts the user has already made, to display: "attempt 1", "attempt 2", "attempt 3" ...
The check() function will compare the number you guessed with the number generated by the computer.
If you get it right, it returns 1, if you miss it it returns 0.
The hint() function will receive your guess and the number generated, and will give you the hint, if your guess was higher, lower or equal to that generated by the computer.
The ends() function displays the final game information.
It takes the number of attempts you made, and tells you whether you hit the record or not. Initially, if the record variable is equal to 0, this variable will take the number of attempts you took to get it right in your first game. Then, it evaluates if your attempts were less than the value of 'record', if it is, it says that you beat the record and this is the new value of the record variable. If not, show the record.
Finally, the continues() function asks if you want to play again or not.
Functions explained, let's go to the logic of the game, in main().
Initially, we declared 3 variables, the one that will store the number of attempts in each round, the one that will store the number generated in each round and the one that will store the user's hunches.
In the first DO WHILE, we will control the rounds that will be played. At the end of each iteration, we call the continues() function inside the WHILE, to see if we should start another round or not.
Within each round, we first clean the screen.
Then we reset the number of attempts, after all, it is a new round.
We display the current record, using the 'record' variable. And we say that we generated the number to be guessed.
Now, let's stay in a loop: ask a guess, say if you missed or hit, give the hint ... ask for another guess, tell if you hit or not, give a hint ... ask for another number ... until the person hits.
For this, we will use another DO WHILE loop. This one will run whenever the person misses. Wrong? Wheel again. The check() function controls this.
Within this internal DO WHILE, we first increase the number of attempts.
We take the user's guess, then give the tip (which also warns when it wins).
Finally, it goes to the check() function, which returns 1 if the person gets it right (looping ends) or 0 if they miss (it rotates again).
After this internal looping is over, we call the ends() function, which will tell you how many attempts you hit, whether you hit the record or not.
After that, go to the external while, which will ask if you want to play again or not.
Simple, isn't it?
But don't be alarmed, a big program is nothing more than several small programs divided, among other things, between functions that do specific things.
Initially, we declare the record variable, which will store the record number of attempts the user has made in a round. It starts at 0. Soon you will understand why.
Then, we create the clean() function, which will clean the screen with each round.
The gen() function causes the computer to generate a random number, from 1 to 100, and returns it.
The guess() function will be used to receive the user's guess. It receives an integer, the number of attempts the user has already made, to display: "attempt 1", "attempt 2", "attempt 3" ...
The check() function will compare the number you guessed with the number generated by the computer.
If you get it right, it returns 1, if you miss it it returns 0.
The hint() function will receive your guess and the number generated, and will give you the hint, if your guess was higher, lower or equal to that generated by the computer.
The ends() function displays the final game information.
It takes the number of attempts you made, and tells you whether you hit the record or not. Initially, if the record variable is equal to 0, this variable will take the number of attempts you took to get it right in your first game. Then, it evaluates if your attempts were less than the value of 'record', if it is, it says that you beat the record and this is the new value of the record variable. If not, show the record.
Finally, the continues() function asks if you want to play again or not.
Functions explained, let's go to the logic of the game, in main().
Initially, we declared 3 variables, the one that will store the number of attempts in each round, the one that will store the number generated in each round and the one that will store the user's hunches.
In the first DO WHILE, we will control the rounds that will be played. At the end of each iteration, we call the continues() function inside the WHILE, to see if we should start another round or not.
Within each round, we first clean the screen.
Then we reset the number of attempts, after all, it is a new round.
We display the current record, using the 'record' variable. And we say that we generated the number to be guessed.
Now, let's stay in a loop: ask a guess, say if you missed or hit, give the hint ... ask for another guess, tell if you hit or not, give a hint ... ask for another number ... until the person hits.
For this, we will use another DO WHILE loop. This one will run whenever the person misses. Wrong? Wheel again. The check() function controls this.
Within this internal DO WHILE, we first increase the number of attempts.
We take the user's guess, then give the tip (which also warns when it wins).
Finally, it goes to the check() function, which returns 1 if the person gets it right (looping ends) or 0 if they miss (it rotates again).
After this internal looping is over, we call the ends() function, which will tell you how many attempts you hit, whether you hit the record or not.
After that, go to the external while, which will ask if you want to play again or not.
Simple, isn't it?
Code game in C++
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int record=0; void clean(); int gen(); int guess(int); int check(int, int); void hint(int, int); void ends(int); int continues(); int main() { int attempts, generated, gues; do{ clean(); attempts=0; cout<<"Record until now: "<< record <<" attempts!"<<endl; generated = gen(); cout<<"\Number was drawn! "<<endl; do{ attempts++; gues = guess(attempts); hint(generated, gues); }while(check(generated, gues) != 1); ends(attempts); }while( continues() ); return 0; } void clean() { if(system("CLS")) system("clear"); } int gen() { unsigned seed = time(0); srand(seed); return 1+rand()%100; } int guess(int tryout) { int gues; cout<<"\nTry to guesse the number, from 1 to 100."<<endl; cout<<"Tryout "<<tryout<<":"<<endl; cin >> gues; if(gues>0 && gues<=100) return gues; else cout<<"Only numbers from 1 to 100"<<endl; } int check(int generated, int gues) { if(generated == gues) return 1; else return 0; } void hint(int generated, int gues) { if(gues<generated) cout<<"WRONG! Your guess was LESS than the number drawn!"<<endl; else if(gues>generated) cout<<"WRONG! Your guess was HIGHER than the number drawn!"<<endl; else cout<<"Greeeat, dude!"<<endl; } void ends(int tryout) { cout<<"\nYou got it right in "<<tryout<<" attempt(s)!"<<endl; if(record==0) record = tryout; if(tryout<=record){ cout<<"Congratulations! The record it's yours!"<<endl; record = tryout; } else cout<<"The record still is "<<record<<" attempt(s)"<<endl; } int continues() { int cont=1; cout<<"\nPlay again?"<<endl; cout<<"0. Exit"<<endl; cout<<"1. One more time!"<<endl; cin >> cont; return cont; }
No comments:
Post a Comment