So I'm trying to set up my game of NIM so that once the game is over, it asks if you'd like to play again with either a 1 or a 0. I have an if statement set up to do so, but right now, even if I do enter a 1 or a 0, it just returns the "invalid statement" option that happens earlier in my program. Here's a screenshot of what's happening.
/* The game of NIM */
#include <iostream>
#include <cstdlib>
usingnamespace std;
int menu();
int game();
int main()
{
menu();
}
int menu()
{
int choice;
cout << "Would you like to play NIM? Enter 1 to play, or enter 0 to exit.\n";
cin >> choice;
if (choice == 1)
{
game();
}
if (choice == 0)
{
return 0;
}
else
{
cout << "Invalid entry. Please enter either a 1 or a 0.\n";
menu();
}
}
int game()
{
cout << "Welcome to the game of NIM!\n";
cout << "---------------------------\n";
cout << "You will take turns against a computer,\nremoving anywhere from 1-3 objects from the game.\n";
cout << "Whoever ends up removing the last object from the game loses,\nand the other player wins!\n\n";
int num_objects = 23;
int current_player = 1;
int move;
int minimum_move = 1;
int maximum_move = 4;
int o = 0;
do
{
if (current_player == 1)
{
cout << "Player 1, enter your move (1-4): ";
cin >> move;
while (move < minimum_move || move > maximum_move || move > num_objects)
{
cout << "Illegal move. \nEnter a new move: ";
cin >> move;
}
}
else
{
switch (num_objects)
{
case 1: move = 1;
break;
case 2: move = 1;
break;
case 3: move = 2;
break;
case 4: move = 3;
break;
case 5: move = 4;
break;
default: move = 1 + rand() % 4;
break;
}
/*do
{
move = 1 + rand() % 3;
}
*/
while (move < minimum_move || move > maximum_move || move > num_objects);
cout << "Computer removed " << move << endl;
}
num_objects = num_objects - move;
for (o = 0; o < num_objects; ++o)
{
cout << "*";
}
cout << "\n" << num_objects << " objects remaining.\n";
if (num_objects > 15 && num_objects < 19)
{
cout << "Things are getting pretty intense in here!\n";
}
if (num_objects < 7 && num_objects > 3)
{
cout << "This is a close game!\n";
}
current_player = (current_player + 1) % 2;
}
while (num_objects > 0);
if (current_player == 1)
{
cout << "Player 1 wins! Good job! :D\n";
}
else
{
cout << "The computer wins! Good try!\n";
}
cin.ignore();
cout << "\nIf you'd like to play again, enter 1. If you would like to exit, enter 0.\n";
int play_again;
cin >> play_again;
if (play_again == 1)
{
game();
}
if (play_again == 0)
{
return 0;
}
}
the other way to fix is to put else in front of line 25.
Your code as-is, if the user enters "1" , enters the game() function, and then evaluates the conditional on line 25. What you really want is for it to exclusively enter one of the three conditional sections, and so you want a
yes, return 0 is used to close the program, but that is what you wanted it to do, no?
Notice how in the menu function, you made a call to game function.
after you complete the game function, the program goes back to the menu function where you left off. But you meant for it to close out at that point. Thus, the return 0 works.