Guess My Number

By Sonny on Apr 24, 2010

old game I wrote. This was probably the first or second game I wrote using random numbers and enumerated types. The computer picks a number, and you try to guess it. Very basic and simple, but it was a beginner project. Now, looking back, I could think of a million ways to rewrite this game, with 100 more features and far more memory efficient. This is just an example code, all in all. So, if you find bugs, don't post them, as I have no intention of fixing them. I'll post better games, and codes soon enough. More games will be posted at my forum.

#include <iostream>
#include <cstdlib>
#include <time.h>

using std::cout;
using std::cin;

int main()
{
enum difficulty {EASY = 10, NORMAL = 100, HARD = 1000};
difficulty diff = NORMAL;

int uDiff, pGuess, pGuesses = 0;

cout << "Welcome to Guess My Number!\n";
cout << "Enter 0 at any time to quit\n";

cout << "Enter the difficulty (1-3): ";
cin >> uDiff;
if (uDiff == 0) { cout << "Thanks for playing! Goodbye."; return 0; }
switch (uDiff)
{
case 1:
diff = EASY;
break;
case 2:
diff = NORMAL;
break;
case 3:
diff = HARD;
break;
default:
do {
cout << "Invalid difficulty!\n";
cout << "Enter the difficulty (1-3): ";
cin >> uDiff;
}
while (uDiff > 4);

}
srand(time(0));
unsigned int cNum = (rand() % diff) + 1;
cout << "Enter your guess: ";
cin >> pGuess;
while (pGuess != cNum)
{
if (pGuess == 0)
{
cout << "Thanks for playing! Goodbye.";
return 0;
}
if (pGuess < cNum)
{
cout << "Too low! Try again.\n";
}
if (pGuess > cNum)
{
cout << "Too high! Try again.\n";
}
cout << "Enter your guess: ";
cin >> pGuess;
}
pGuesses++;
cout << "\nYou guessed correctly after " << pGuesses << " attempts.\n";
cout << "The number was: " << cNum << "\n";
cin.get();
return 0;
}

Sonny   -  Apr 25, 2010

Yeah, it looks good. I'm looking into it. Thanks. Qt is good as well, and that's what I've actually started in. So in some time, I'll expand onto bigger games, as well. Plus the language bindings are great, especially with Python. So, we'll see.

Hawkee   -  Apr 25, 2010

Great, maybe you can look into wxWidgets. There's also Qt, but that's quite pricey.

Sonny   -  Apr 25, 2010

Yeah, it'll be cross platform. And, I don't even code on windows. One thing I learned is not to code platform specific unless necessary.

Hawkee   -  Apr 24, 2010

Something to keep in mind when doing a GUI is if you make it dependent on Windows it won't run on Mac. I'm not sure if there is a cross-platform library available.

Sonny   -  Apr 24, 2010

I might redo it with a nice, simple gui and a more advanced rand generator, as well as the ability to have the computer guess the number, instead of you.

Hawkee   -  Apr 24, 2010

One suggestion, maybe ask for the maximum number rather than give 3 options. I was playing around with it and did a max of 100,000. Made for more of a challenge.

Sonny   -  Apr 24, 2010

I might addon to it, since now I'm doing intermediate visual apps.

Hawkee   -  Apr 24, 2010

Cool, I got it to compile on OS X and here were my results:

Welcome to Guess My Number!
Enter 0 at any time to quit
Enter the difficulty (1-3): 3
Too low! Try again.
Too low! Try again.
Too high! Try again.
Too high! Try again.
Too low! Try again.
Too high! Try again.
Too high! Try again.
Too low! Try again.
Too high! Try again.