HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Guess My Number - computer guesses the user's number

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
guessesnumbertheusercomputerguess

Problem

I'm learning C++ right now and one of the activity/tutorial things was to re-write the "High/Low Guess My Number" game - with a twist. I had to re-write it so that the computer had to guess the number.

Here's the exercise prompt:


Write a new version of the Guess My Number program in which the player and computer switch roles. That is, the player picks a number and the computer must guess what it is

And, here's what I wrote:

```
// Guess My Number - Computer vs Player

#include
#include
#include

using namespace std;

int main()
{
srand(static_cast(time(0)));

int tries = 0;

// short stuff; // Waiting for solution

bool error = false;

enum statusTypes {NADA, HIGH, LOW, CORRECT};
int status = NADA;

int min = 0;
int max = 101;

int guess = rand() % 100 + 1;
//int previousGuess; // Only for duplicate guess checking
// ^ Not used currently, 1.1 release will fix duplicate PC guesses
int toGuess;

cout > toGuess;

cin.get();

while (status != CORRECT)
{
++tries;

cout toGuess)
{
status = HIGH;
//cout max || guess max || guess < min);

}

else if (status == CORRECT)
{
cout << "Computer Guessed It!\n";
cout << "The guess was " << guess << "\n";
cout << "And it took " << tries << " tries!\n";
cout << "Thanks for playing!" << endl;
}

else
{
error = true;
cout << "Uh, something went wrong.\n";
cout << "Here's the current values: \n";
cout << "Status: " << status << endl;
cout << "toGuess: " << toGuess << endl;
cout << "guess: " << guess << endl;
cout << "tries: " << tries << endl;
}

/*cout << "\n3rd Debug Statements:\n" << endl;
cout << "Here's the current values: \n";
cout << "Status: " << status << endl;
cout << "toGuess: " << toGu

Solution

First of all, I'd like to commend you for calling srand() in main() only. It's quite common for many to call it elsewhere instead, which messes up the randomization.

Overall, this looks pretty clean for a beginner. Indentation and whitespace is consistent and the code flows okay. Nonetheless, there are still some points to address, and I'll just provide some basics.

-
Once you learn about creating functions, I highly recommend you incorporating this program and future ones. Having everything in main() makes code harder to read and maintain, but it's not as severe with this short program. I'd consider that your next step in your learning.

For instance, you can have a function for all the conditional statements with status. This will remove duplication, thus shortening your code and having that functionality in one place.

Here's what one conditional may look like with such a function call:

if (status == HIGH)
{
    max = guess;

    guess = randNumber(guess, min, max);
}


You'll just need to define this function that will assign to guess.

-
Try not to have so much commented-out code, at least on your Code Review submission. It makes it harder to read and review, though I understand that it's for debugging. That's not important for others, so you can leave out this information at that time.

-
I assume NADA refers to an unassigned status type. In either case, you can rename it to sound more relevant to its use.

As for the enum itself, you can just name it Status. It's a type, so it can be capitalized.

Code Snippets

if (status == HIGH)
{
    max = guess;

    guess = randNumber(guess, min, max);
}

Context

StackExchange Code Review Q#73706, answer score: 12

Revisions (0)

No revisions yet.