NIM sticks

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.

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
64
  #include <iostream>

using namespace 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 
Last edited on
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!
Topic archived. No new replies allowed.