I am practicing c++ out of a book and it had me create the game NIM. One exercise tells me to change the program so that it prints the number of objects removed by the player, so I added line 27, but it will not print line 27. What did I do wrong with the cout?
/* The game of NIM */
#include<iostream> // include two libraries
#include<cstdlib>
usingnamespace std;
int main() // main starts the actual program
{
//--------------------variable declarations-------------------------
int num_objects = 23;
int current_player = 1;
int move;
//-------------------- Beginning of the game loop -------------------
cout << "How to play NIM: " << endl;
cout << "The traditional game of Nim is played with a number of coins arranged in heaps: the number of coins and heaps is up to you. " << endl;
cout << " There are two players. When it's a player's move he or she can take any number of coins from a single heap. They have to take at least" << endl;
cout << " one coin, though, and they can't take coins from more than one heap. The winner is the player who makes the last move, so there are no" << endl;
cout << "coins left after that move. (Some people play the game the other way around, with the last person to make a move losing the game, but we'll" << endl;
cout << " ignore that version for the moment." << endl;
cout << " It's clear that there is no luck involved here. You can work out the best move to make by cleverly predicting the sequence of moves that would follow it." << endl;
do {
if (current_player == 1) { // conditional: if
cout << "Player 1, enter your move (1-3): "; // output
cin >> move; // input
while (move < 1 || move > 3 || move > num_objects) {
cout << "Illegal move. \nEnter a new move: ";
cin >> move;
cout << "Player 1 removed " << move << endl;
}
} else { // else part of conditional
do { // make sure move is legal
move = 1 + rand() % 3; // random computer move
} while (move < 1 || move > 3 || move > num_objects);
cout << "Computer removed " << move << endl;
}
num_objects = num_objects - move; // implement the move
cout << num_objects << " objects remaining. \n";
current_player = (current_player + 1) % 2; // switch player
} while (num_objects > 0);
//--------------end of the game loop-----------------
cout << "Nice going player " << current_player << ", you win!\n";
cin.ignore();
cout << "\nPress enter to quit.\n";
cin.ignore();
return 0;
}
First it checks if the move was illegal - while (move < 1 || move > 3 || move > num_objects)
If it is, say I enter 7, which is an illegal move, then it will enter the loop. Then it will ask the user to enter another re-enter the move. If the user enters something illegal again, it will then print out line 27, which is "Player 1 removed" etc. and then re-run the loop. If the user enters something legal, it will STILL print out line 27.
You probably want it to only output how line 27 when the user makes a legal move no?
No I do not want it to tell how many the player moved unless it is a legal move. So am I going to need another if statement right after the "cin >> move;" inside my first if statement that will tell it to only say the amount that the player removed if the move is within 1 and 3?