-
My first c++ game
it's a simple game, you've got to guess whats the number thats been randomly generated. I cant help having the feeling that this code could be cleaner, I'd like suggestions from more experienced programmers.
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void game();
string turnString(int n);
string clue(int n, int randNumber);
string comments(int turn);
int playAgain;
int main()
{
do
{
game();
cout<<"\n\n\nPLAY AGAIN? (TYPE 1 FOR YES): ";
cin>>playAgain;
cout<<"\n\n\n";
}
while(playAgain == 1);
}
void game()
{
srand(time(0));
int randNum = (rand() % 15) + 1;
int chances = 5;
int turn = 1;
int input;
cout<<"Random number has been generated (1 to 15),\nyou've got 5 chances to guess it. GOOD LUCK!"<<endl;
cin.get();
while(turn <= 5 && input != randNum)
{
cout<<"\nGuess Number "<<turn<<": ";
cin>>input;
cout<<clue(input,randNum)<<endl;
turn++;
cin.get();
}
turn--;
if(input == randNum)
{
cout<<"\n\nCongratulations! You've guessed it!"<<endl;
cin.get();
cout<<"You've done it on the "<<turnString(turn)<<" turn.";
cin.get();
cout<<"\nComments: "<<comments(turn);
cin.get();
}
else
{
cout<<"\n\nYou've run outta chances!\nThe random number was "<<randNum<<".\nTRY AGAIN, SUCKER!";
cin.get();
cin.get();
}
}
string turnString(int n)
{
string stringReturn;
switch(n)
{
case 1:
stringReturn = "first";
break;
case 2:
stringReturn = "second";
break;
case 3:
stringReturn = "third";
break;
case 4:
stringReturn = "fourth";
break;
case 5:
stringReturn = "fifth";
break;
}
return stringReturn;
}
string clue(int n,int randNumber)
{
string stringReturn;
if(randNumber != n && randNumber > n)
{
stringReturn = "Try higher.";
}
if(randNumber != n && randNumber < n)
{
stringReturn = "Try lower.";
}
return stringReturn;
}
string comments(int turn)
{
string comments[6];
comments[1]="FLAWLESS VICTORY!";
comments[2]="Good job!";
comments[3]="Hmmm... not bad.";
comments[4]="You could've done better.";
comments[5]="You saved your ass this time, pal.";
return comments[turn];
}
-
You made some attempt at indentation, and that is good, but it can still be improved, particularly for the main function. Note that the expression (randNumber != n && randNumber > n) can be simplified to (randNumber > n).
Instead of always defining the array of strings for each call of your comments function, you could define a static array of const strings instead.
-
main function? Surely you mean the game function since main is already properly indented.
-
Hi, i've made a few changes in my code, i've made it possible to play on different difficulty levels, but now i have a problem, here's my code:
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int level;
bool isLevelSet;
void game();
string turnString(int n);
string clue(int n, int randNumber);
string comments(int turn);
int playAgain;
int main()
{
do
{
game();
cout<<"\n\n\nPLAY AGAIN? (TYPE 1 FOR YES): ";
cin>>playAgain;
cout<<"\n\n\n";
}
while(playAgain == 1);
}
void game()
{
if(isLevelSet == false){srand(time(0));};
int randNum;
int range[3];
int chances = 5;
int turn = 1;
int input;
cout<<"Random number will be generated.\nYou've got 5 chances to guess it."<<endl;
if(isLevelSet == false)
{
cout<<"\n\nSELECT DIFFICULTY LEVEL: \n";
cout<<"1. EASY (10 Numbers)\n2. NORMAL (15 Numbers)\n3. HARD (20 Numbers)\n";
cout<<"Command: ";
cin>>level;
range[1] = 10;
range[2] = 15;
range[3] = 20;
randNum = (rand() % range[level]) + 1;
//cout<<"Range is "<<range[level];
isLevelSet = true;
}
while(turn <= 5 && input != randNum)
{
cout<<"\nGuess Number "<<turn<<": ";
cin>>input;
cout<<clue(input,randNum)<<endl;
turn++;
cin.get();
}
turn--;
if(input == randNum)
{
cout<<"\n\nCongratulations! You've guessed it!"<<endl;
cin.get();
cout<<"You've done it on the "<<turnString(turn)<<" turn.";
cin.get();
cout<<"\nComments: "<<comments(turn);
cin.get();
}
else
{
cout<<"\n\nYou've run outta chances!\nThe random number was "<<randNum<<".\nTRY AGAIN, SUCKER!";
cin.get();
cin.get();
}
}
string turnString(int n)
{
string stringReturn;
switch(n)
{
case 1:
stringReturn = "first";
break;
case 2:
stringReturn = "second";
break;
case 3:
stringReturn = "third";
break;
case 4:
stringReturn = "fourth";
break;
case 5:
stringReturn = "fifth";
break;
}
return stringReturn;
}
string clue(int n,int randNumber)
{
string stringReturn;
if(randNumber != n && randNumber > n)
{
stringReturn = "Try higher.";
}
if(randNumber != n && randNumber < n)
{
stringReturn = "Try lower.";
}
return stringReturn;
}
string comments(int turn)
{
string comments[6];
comments[1]="FLAWLESS VICTORY!";
comments[2]="Good job!";
comments[3]="Hmmm... not bad.";
comments[4]="You could've done better.";
comments[5]="You saved your ass this time, pal.";
return comments[turn];
}
the problem is, after you select the level, it sets the range int so it defines how far the random number can go, this is okay up until your first game, now, when you play it for the second time, the range goes immensely far.
-
Again, indentation. Your code is unreadable.
-
No code within a function will touch the left margin.
Code:
int main()
{
do_something();
this_guy = that_guy;
}
Is much better read as
Code:
int main()
{
do_something();
this_guy = that_guy;
}
I used three spaces.
-
Then here's the indented code:
Code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int level;
bool isLevelSet;
void game();
string turnString(int n);
string clue(int n, int randNumber);
string comments(int turn);
int playAgain;
int main()
{
do
{
game();
cout<<"\n\n\nPLAY AGAIN? (TYPE 1 FOR YES): ";
cin>>playAgain;
cout<<"\n\n\n";
}
while(playAgain == 1);
}
void game()
{
if(isLevelSet == false){srand(time(0));};
int randNum;
int range[3];
int chances = 5;
int turn = 1;
int input;
cout<<"Random number will be generated.\nYou've got 5 chances to guess it."<<endl;
if(isLevelSet == false)
{
cout<<"\n\nSELECT DIFFICULTY LEVEL: \n";
cout<<"1. EASY (10 Numbers)\n2. NORMAL (15 Numbers)\n3. HARD (20 Numbers)\n";
cout<<"Command: ";
cin>>level;
range[1] = 10;
range[2] = 15;
range[3] = 20;
randNum = (rand() % range[level]) + 1;
isLevelSet = true;
}
while(turn <= 5 && input != randNum)
{
cout<<"\nGuess Number "<<turn<<": ";
cin>>input;
cout<<clue(input,randNum)<<endl;
turn++;
cin.get();
}
turn--;
if(input == randNum)
{
cout<<"\n\nCongratulations! You've guessed it!"<<endl;
cin.get();
cout<<"You've done it on the "<<turnString(turn)<<" turn.";
cin.get();
cout<<"\nComments: "<<comments(turn);
cin.get();
}
else
{
cout<<"\n\nYou've run outta chances!\nThe random number was "<<randNum<<".\nTRY AGAIN, SUCKER!";
cin.get();
cin.get();
}
}
string turnString(int n)
{
string stringReturn;
switch(n)
{
case 1:
stringReturn = "first";
break;
case 2:
stringReturn = "second";
break;
case 3:
stringReturn = "third";
break;
case 4:
stringReturn = "fourth";
break;
case 5:
stringReturn = "fifth";
break;
}
return stringReturn;
}
string clue(int n,int randNumber)
{
string stringReturn;
if(randNumber != n && randNumber > n)
{
stringReturn = "Try higher.";
}
if(randNumber != n && randNumber < n)
{
stringReturn = "Try lower.";
}
return stringReturn;
}
string comments(int turn)
{
string comments[6];
comments[1]="FLAWLESS VICTORY!";
comments[2]="Good job!";
comments[3]="Hmmm... not bad.";
comments[4]="You could've done better.";
comments[5]="You saved your ass this time, pal.";
return comments[turn];
}
I cant help having the feeling that this code could be cleaner, I'd like suggestions from more experienced programmers.
Another problem is, after you select the level, it sets the range int so it defines how far the random number can go, this is okay up until your first game, now, when you play it for the second time, the range goes immensely far.
-
Here's how I would do it:
1) Pick a random number. Clamp, if desired (but not necessary).
2) Calculate log2(N) + 1. This is the maximum number of guesses needed to find any given N.
3) Divide #2 by the difficulty level.
4) Scale #3 by some factor (a bias) to increase the game's overall "level of ease", if desired.
In code, something along these lines:
Code:
size_t maximum_guesses_allowed( size_t value, size_t level, size_t bias = 0 )
{
return std::max
(
size_t( ( log( value ) / log( 2 ) + 1 ) / ( level + 1 ) * ( bias + 1 ) ),
size_t( 1 )
);
}
Note that the above assumes a 0-based difficulty rating.