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

Determine cheating in reverse "Guess My Number" game

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

Problem

I'm just starting to learn C++ (not my first language), and started with working on a program to simulate the classic "Guess My Number" game, and am mostly looking for feedback on optimization and/or general tips. I wanted to write it to allow the player to either guess like normal, but also allow the player to choose and have the computer guess. The most interesting part about this, in my opinion anyway, is determining if the player is cheating, and this is also what I'm most interested in regarding feedback (but I'd welcome feedback on anything).

Another side-question is: should I use the enum's name when referring to the enum values, such as resultOption::CORRECT, or is just using CORRECT alright? I know I should never use using namespace std for clarity's sake.

```
#include
#include
#include

enum mainOption { GUESSER = 1, CHOOSER_DUMB, CHOOSER_SMART, EXIT };

const int MIN_GUESS = 1;
const int MAX_GUESS = 100;
const std::string PLAY_AGAIN_MSG = "\nThat was fun! Let's play again!\n";

int mainMenu();
void playGuesser();
void playChooser(bool smart);

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

int option;
do {
option = mainMenu();

switch (option) {
case GUESSER:
playGuesser();
break;
case CHOOSER_DUMB:
playChooser(false);
break;
case CHOOSER_SMART:
playChooser(true);
break;
case EXIT:
std::cout > option;

if (option EXIT) {
std::cout EXIT);

return option;
}

void playGuesser() {
int number = (std::rand() % (MAX_GUESS - MIN_GUESS + 1)) + MIN_GUESS;
int guess, guesses = 0;

std::cout > guess;

if (guess MAX_GUESS) {
std::cout number) {
std::cout > result;

if (result CORRECT) {
std::cout CORRECT);

if (result == CORRECT) {
std::cout << "Aw

Solution

It looks like quite a bit of refactoring can be done (especially with all of the conditionals in the larger functions), but I'll start with some other points.

-
The random number of blank lines at the top is a little annoying. You really only need one between the different sections. You can also move main() at the very bottom to eliminate the need for the function prototypes.

-
Great work on having the seed generation done in main() only! Many people seem to do that incorrectly.

Now, if you have access to C++11, you should consider using the ` library instead for random number generation. There are many different ways of using it, which would depend on your application.

-
This is probably a little picky, but you can make your constants
constexpr instead (also available only in C++11). This will make them compile-time defined.

-
I would try to keep all the menu work in separate functions and just have them called from
main(). It's probably not bad to keep them as is, but I like to try to keep main() as simple as possible.

-
One might assume that a function called
mainMenu() would be void and just display the menu options. Since your function is returning a value, you can rename it to something like getMainMenuOption()`.

Context

StackExchange Code Review Q#116744, answer score: 5

Revisions (0)

No revisions yet.