Public Group

# Number Guess Code Review

This topic is 1853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I have been following this book about C++ and one of the challenges was changing a game where you guess the computer's number to a game where the computer guesses your number.

With a bit of help from Google, I managed to complete the game but have come across one issue. If someone wants to, they can crash the game by giving the computer no more numbers to choose from.

1. Do you have any solutions on how I can stop the game from being broken?
2. Is there a way the code can be improved?

Thanks!

// Guess My Number
// The classic number guessing game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator

int guess = rand() % 100 + 1; //random number between 1 and 100
int tries = 0;
int highestNumber = 100;
int lowestNumber = 1;
char position;
bool success;

cout << "\tWelcome to Guess My Number";
cout << "\n\nThink of a number between 1 and 100.";
cout << "\nThe computer will try to guess your number.";

cout << "\n\nAre you ready? (y/n): ";

do
{
cout << "\n\nMy Guess: " << guess << endl;
cout << "\nHigh, Low or Success? (h/l/s): ";
cin >> position;

if (position == 'h')
{
highestNumber = guess - 1;
guess = rand() % (highestNumber - lowestNumber + 1) + lowestNumber;
success = false;
}

else if (position == 'l')
{
lowestNumber = guess + 1;
guess = rand() % (highestNumber - lowestNumber + 1) + lowestNumber;
success = false;
}

else if (position == 's')
{
cout << "\nWoo! I won!";
success = true;
}

} while (success != true);

return 0;
}

##### Share on other sites

if highestNumber == lowestNumber and they say the guess is wrong they have cheated so tell them that.

EDIT: I don't know if it can happen in your code but if lowest > highest or highest < lowest that is another cheat on the user's part.

##### Share on other sites

Thanks Paradigm Shifter, I will input that now and give it a whirl.

EDIT: Where should I input the code? I tried it in the mix with the 'else if' but it didn't work.

Edited by CallumFR

##### Share on other sites

Some brief observations:

1. Your ready-variable is not used; the games starts even if the user is no ready. Not so much an issue with the design of the program from a coding point of view, but it is a surprise for the user.
2. The success-variable is not necessary. It is only true when position is 's', so use that as a break condition.
3. A quit option would be nice to break the game. Perhaps not so much in this particular case since the game is short enough to end within just a few moves on average, but in general you should consider it.
4. You may want to move the win condition to after the loop instead. This can be argued though, since it depends on what you consider game loop logic. The win condition is not part of the loop but as a part of the exit condition, but on the other hand it is also a part of the  set of valid inputs during the game.
5. A final else-statement taking care of invalid input would be nice so the user knows the input was invalid instead of wondering why the computer guessed the same number.

##### Share on other sites

I tried following your coding style wherever I made changes. I'm guessing you haven't learned about functions yet, but this at least cuts down on some repeated code (notably, the let's guess a new number part).

// Guess My Number
// The classic number guessing game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(static_cast<unsigned int>(time(0)));

int guess = rand() % 100 + 1;
int tries = 1; //Initialized to 1 instead of 0, guessing it in first = 1 guess
int highestNumber = 100;
int lowestNumber = 1;
char ready; //This isn't really used currently.
char position;
bool success = false; //Initialize and don't worry about it if incorrect guesses

cout << "\tWelcome to Guess My Number";
cout << "\n\nThink of a number between 1 and 100.";
cout << "\nThe computer will try to guess your number.";

cout << "\n\nAre you ready? (y/n): ";
cin >> ready; //This is currently just ignored.

do
{
cout << "\n\nMy Guess: " << guess << endl;
cout << "\nHigh, Low or Success? (h/l/s): ";
cin >> position;

if (position == 's') //Checking success first. No need to do other stuff if we got it right
{
cout << "\nWoo! I won in " << tries << " attempts!"; //Added the tries to output
success = true;
}
else //We didn't get it right
{
if (position == 'h' || position == 'l') //Did we get something too low or something too high?
{
if (position == 'h') //Too high, adjust highest
{
highestNumber = guess - 1;
}
{
lowestNumber = guess + 1;
}

if (lowestNumber > highestNumber) //Check for lies
{
cout << "\nYou lied to me! I'm done playing...";
success = true;
}
else //No lies detected, let's guess again
{
guess = rand() % (highestNumber - lowestNumber + 1) + lowestNumber;
tries++;
}
}
else //Character that isn't handled.
{
cout << "\nInvalid character input.";
}
}
} while (success != true);

return 0;
}

Edited by CoreLactose

##### Share on other sites

Some brief observations:

1. Your ready-variable is not used; the games starts even if the user is no ready. Not so much an issue with the design of the program from a coding point of view, but it is a surprise for the user.
2. The success-variable is not necessary. It is only true when position is 's', so use that as a break condition.
3. A quit option would be nice to break the game. Perhaps not so much in this particular case since the game is short enough to end within just a few moves on average, but in general you should consider it.
4. You may want to move the win condition to after the loop instead. This can be argued though, since it depends on what you consider game loop logic. The win condition is not part of the loop but as a part of the exit condition, but on the other hand it is also a part of the  set of valid inputs during the game.
5. A final else-statement taking care of invalid input would be nice so the user knows the input was invalid instead of wondering why the computer guessed the same number.

Thanks for the tips. Just read through the code when I saw this and spotted quite a few things I didn't utilize at all.

And after reading the new code CoreLactose suggested, I can see what you mean by how everything should be moved around.

Edited by CallumFR

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634067
• Total Posts
3015320
×