Jump to content

  • Log In with Google      Sign In   
  • Create Account


Number Guess Code Review


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 CallumFR   Members   -  Reputation: 104

Like
0Likes
Like

Posted 21 December 2013 - 05:05 AM

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 ready;
	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): ";
	cin >> ready;

	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;
}


Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5093

Like
1Likes
Like

Posted 21 December 2013 - 05:10 AM

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.


Edited by Paradigm Shifter, 21 December 2013 - 05:13 AM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 CallumFR   Members   -  Reputation: 104

Like
0Likes
Like

Posted 21 December 2013 - 05:14 AM

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, 21 December 2013 - 05:22 AM.


#4 Brother Bob   Moderators   -  Reputation: 7744

Like
1Likes
Like

Posted 21 December 2013 - 05:23 AM

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.


#5 Lactose!   GDNet+   -  Reputation: 2294

Like
1Likes
Like

Posted 21 December 2013 - 05:46 AM

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).

I removed all your comments, to more easily show where I made comments myself.

// 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;
				}
				else //Too low, adjust lowest
				{
					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, 21 December 2013 - 05:49 AM.


#6 CallumFR   Members   -  Reputation: 104

Like
0Likes
Like

Posted 21 December 2013 - 06:09 AM

 

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, 21 December 2013 - 06:13 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS