Hello, I'm currently in C++ 101 at school and I need a peer review for my assignment by midnight tonight. I am taking this class online and there are no students available or they aren't responding on the class forums. Just a comment on any errors, unnecessary code or suggestions would be very appreciated!
The assignment ask me to make a game called "Nim"...
[size=90]You will write a program that implements the following version of the game of Nim.
Nim is a mathematical strategy game, which dates back to 16th century China. There are many
variations on this game, but the version we’ll be playing starts with a row of 11 sticks:
|||||||||||
On their turn a player may remove 1, 2, or 3 sticks from the pile. Players alternate taking turns,
and the play removing the last stick wins. Your game should feature two players alternating
turns. First, your program should print out a welcome statement and brief instructions. Then, you
should display the current game state and prompt player 1 for the number of sticks they should
remove:
After making sure they entered a valid response, you should remove the appropriate number of
sticks. If there are now zero sticks remaining, you should declare that player the winner and
end the program. Otherwise, the other player should take a turn.[/size]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
using namespace std;
int playerinput;
const int MAXSTICKS = 11;///max number of sticks
int sticks = MAXSTICKS; ///number of sticks left
int whoplays = 1; ///1 is player ones turn, 2 is player twos turn
bool gamerunning = true;
int main()
{
cout << "Welcome to Nim!" << endl;
cout << "Players will take turns removing 1, 2, or 3 sticks from the initial 11.\n";
cout << "The player removing the last stick wins! Player 1, it's your turn!" << endl;
while ((gamerunning == true))
{
do
{
for (int i = sticks; i>0; i--)
{
cout << "|";
}
if (whoplays == 1)
{
cout << "\nPlayer 1, it's your turn!" << endl;
}
else
{
cout << "\nPlayer 2, it's your turn!" << endl;
}
cout << "How many sticks would you like to remove? (1, 2, or 3)..." << endl;
cin >> playerinput;
while (playerinput%1 !=0 || playerinput <= 0 || playerinput > 3 || playerinput > sticks)
{
cout << "INVALID INPUT \n Try Again..." << endl;
for (int i = sticks; i>0; i--)
{
cout << "|";
}
cout << "\n";
cin >> playerinput;
}
sticks = (sticks-playerinput);
if (sticks<1)
{
cout << "Congratulations Player " << whoplays<<", you win!\n\n\n";
gamerunning = false;
}
if (whoplays == 1)
{
whoplays = 2;
}
else
{
whoplays= 1;
}
}
while (sticks >0);
}
return 0;
}
|