This is a homework assignment that I am stuck on and can't get my professor to reply to any of my questions. The assignment is to build a version of NIM. Everything worked great until i tried to make the computer display the actual sticks instead of numbers. I now it needs to be a for loop but cant get it to work. Any help would be greatly appreciated.
#include <iostream>
usingnamespace std;
int main()
{
//initial setup with sticks being set to eleven
int sticks, sticks_1, sticks_2, player_1, player_2;
sticks = 11;
cout << "Welcome to Nim!" << endl;
cout << "Players will take turns removing 1, 2, or 3 sticks from the" << endl;
cout << "initial 11. The player removing the last stick wins! ||||||||||| <---sticks" << endl;
//loops set untill someone makes count zero
while (sticks!=0)
{
cout << "The game looks like this: " << sticks << endl;
cout << "Player 1 it's your turn, select 1, 2 or 3 sticks to remove." << endl;
cin >> player_1;
while (player_1 > 3 || player_1 < 1 || player_1 > sticks)
{
cout << "!You did not enter a correct answer!" << endl;
cout << "If there is three or less you should win!" << endl;
cout << "Player 1 please choose 1, 2 or 3 sticks to remove." << endl;
cin >> player_1;
}
sticks = sticks - player_1;
if (sticks == 0)
{
cout << "Player 1 wins!" << endl;
break;
}
cout << "The game looks like this: " << sticks << endl;
cout << "Player 2 it's your turn, select 1, 2 or 3 sticks to remove." << endl;
cin >> player_2;
while (player_2 > 3 || player_2 < 1 || player_2 > sticks)
{
cout << "!You did not enter a correct answer!" << endl;
cout << "If there is three or less you should win!" << endl;
cout << "Player 2 please choose 1, 2 or 3 sticks to remove." << endl;
cin >> player_2;
}
sticks = sticks - player_2;
if (sticks == 0)
{
cout << "Player 2 wins!" << endl;
break;
}
}
}
The most basic (if lacking elegance) version would look like this:
1 2 3 4
cout << "The game looks like this: " << sticks << endl; // change this line into these 3:
cout << "The game looks like this: " ;
for (int i = 0; i < sticks; i ++) { cout << "|";} // draw one stick as many times as needed
cout << endl; // only after last stick is drawn, end line
Thank you! I was so close, but don't think I would of came up with that by myself.
cout << "The game looks like this: " ;
for (int i = 0; i < sticks; i ++)
{
cout << "|";
}
cout << endl;
worked great!