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

Console guess game

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

Problem

This is the console program that I've written in C++. It's a game where you have to guess a randomly picked number within the range 0-200. I just took a look at syntax and how to get started with C++ and VS and I was able to write this game almost immediately, but tweaking and improving it took me some time. I have a lot of experience with PAWN.

Anyway, does my code look alright? Please let me know if there are any bad practices that you notice or anything wrong.

`#include // cin(), cout(), printf()
#include // time()
#include // Sleep()

/ Variables /
int number;
int tries;
int max_tries;

/ Functions /
void game_init(int try_count);
void game_process();
void game_decide();
int game_check(char input);

int main()
{
srand((unsigned int)time(NULL));

game_init(8);
game_process();
}

void game_init(int try_count)
{
number = rand() % 200;
tries = 1;
max_tries = try_count;
printf("You have %d tries to guess a random number we've picked for you. (withing a range 0-200)\n", max_tries);
}
void game_process()
{
do
{
printf("\nTry %d/%d: ", tries, max_tries);

int input;
scanf_s("%d", &input);

if (game_check(input) == 1)
{
std::cin.get();
break;
}

if (tries == 9)
{
printf("\n\n\nUnfortunately, you weren't able to guess the number.\nIt was %d!", number);
printf("\n\nI wish you more luck the next time. Wanna try again? Type \"yes\" or \"no\".");
printf("\nDo you want to try again? ");

game_decide();
}
}
while (tries number)
{
printf("\nThe number %d you've entered is higher than the number we're looking for.", input);

tries ++;
std::cin.ignore();
}

return 0;
}

void game_decide()
{
char decision[3];
std::cin >> decision;

if (!strcmp(decision, "yes"))
{
std::cout

Solution

-
There's no need for printf() and scanf() in C++. Instead, use:

  • std::cout



  • std::cin >> aVariable; for inputting



-
In C++, use
instead of . The latter is a C library.

-
Never use global variables, unless they're constants. Instead, pass
number and tries to their relevant functions.

-
If
max_tries is supposed to be a constant, initialize it below the #includes as such:

const int max_tries = 8;


-
Since you're using
std::rand(), which returns non-negative numbers, you could change number's type to unsigned int.

-
You should move
game_init()'s contents to main() instead for clarity and easier program termination (via return X).

-
game_check() should return a bool since it's conditional. For bools, 0 corresponds to false and 1 corresponds to true.

It shouldn't need to display anything since that's not its purpose. Instead, move those outputs to
game_process(), remove the std::cin.ignore()s, and return the appropriate bools.

Remove the
return 0; it should now return a value of true or false instead.

The parameter for its prototype and definition should be an
int, which you're already passing into it from game_process().

-
In
game_decide(): instead of using a char array to hold decision, just make it a single char variable. A "y" or "Y" could mean yes, and an "n" or "N" could mean no. Then, your conditional statements could look like this:

if (decision == 'y' || decision == 'Y')
// do something
else if (decision == 'n' || decision == 'N')
// do something else
else
// try again


You're doing a recursive call, which is absolutely unnecessary for this situation. Instead, use a
while or do-while loop to handle this.

It should return a
bool instead (for the same reason as game_check()).

Clear everything from the "yes" and "no" blocks and instead have them respectively return
true and false. Then, back at game_process(), allow the loop to end if game_decide() returns false (meaning that the user no longer wants to play). Thus, the program will fall back to main() and terminate nicely. You would then no longer need #include `.

Code Snippets

const int max_tries = 8;
if (decision == 'y' || decision == 'Y')
// do something
else if (decision == 'n' || decision == 'N')
// do something else
else
// try again

Context

StackExchange Code Review Q#27613, answer score: 16

Revisions (0)

No revisions yet.