Trying to create a code of the game NIM

Hi! I am new to this, but I've been trying to create a game for NIM, but my code isn't working

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Boma Braide
// December 12, 2018
// Game of NIM


#include <iostream> 
#include <ctime> 
#include <cmath> 

using namespace std; 

  int main(){ 

    bool winner = false; 
    int num, sticks = 22, comp_choice; 
    char whoseTurn, yourTurn = 'Y', compTurn = 'N'; 
    srand(time(NULL)); 

   cout << "welcome to the game of N I M. would you like to go first? Y/N" << endl; 

    cin >> whoseTurn; 

  if (whoseTurn == yourTurn){ 

        cout << "you go first!" << endl; 

    } 

    else if (whoseTurn == compTurn){ 

        cout << "the computer goes first!" << endl; 

    } 

  cout << endl; 

  while (sticks >= 0){ 

    // if the player chooses to go first... 

        if (whoseTurn == yourTurn){ 

            cout << "there are " << sticks << " sticks in the pile.\n" 

                << "\n enter the amount (between 1 and 4) of sticks that you will remove from this pile." << endl; 

            cin >> num; 

  // if number is 1-4, amount of sticks taken/left will be displayed 

            if ((num >= 1) && (num <= 4)){ 

                cout << "you have removed " << num << " sticks from the pile." << endl; 

                sticks -= num; 

                cout << "there are now " << sticks << " sticks left in the pile." << endl; 

            } 

  

            // if number is less than 1 or greater than 4, user will be repeatedly prompted until done so 

            if ((num < 1) || (num > 4)){ 

                cout << "unacceptable answer >:( enter an amount between *1* and *4*" << endl; 

  

                //    cin >> num; 

            } 

  

  

            // computer enters value 

            else { 

                comp_choice = 1 + rand() % (4 - 1) + 1; 

                sticks -= comp_choice; 

                cout << "\n the computer has taken away " << comp_choice << " sticks from the pile.\n" 

                    << "there are now " << sticks << " sticks left in the pile.\n"; 

  

            } 

        } 

  

  

  

    // ----------------------------------------------------------------------------------------------------------- // 

  

            // if COMPUTER goes first 

            else if (whoseTurn == compTurn){ 

                comp_choice = 1 + rand() % (4 - 1) + 1; 

                sticks -= comp_choice; 

                cout << "\n the computer has taken away " << comp_choice << " sticks from the pile.\n" 

                    << "there are now " << sticks << " sticks left in the pile.\n"; 

            } 

            else { 

                do { 

                    cout << "there are " << sticks << " sticks in the pile.\n" 

                        << "\n enter the amount (between 1 and 4) of sticks that you will remove from this pile." << endl; 

                    cin >> num; 

                } while ((num >= 1) && (num <= 4)); 

                cout << "you have removed " << num << " sticks from the pile." << endl; 

                sticks -= num; 

                cout << "there are now " << sticks << " sticks left in the pile." << endl; 

  

                if ((num < 1) || (num > 4)){ 

                    cout << "unacceptable answer >:( enter an amount between *1* and *4*" << endl; 

                } 

                whoseTurn = whoseTurn == compTurn ? yourTurn : compTurn; 

            } 

        } 

  

        system("pause"); 

        return 0; 

    } 


(i haven't yet figured where/how I'll declare the winner once the user or computer reaches 0 so that's a problem too. Your help is much appreciated!)
Last edited on
There were several small errors in your code, especially at the range of your random numbers and how you detect if the poile get empty. Here the fixed code. I have used the random facilities introduced at c++11.
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
65
66
67
68
69
70
71
72
#include <iostream> 
#include <random>

using namespace std; 

int main(){ 
    
    bool playerWon;
    int num, sticks = 22;
    char whoseTurn;
    const char yourTurn = 'Y', compTurn = 'N';
    
    default_random_engine r( random_device{}());
    uniform_int_distribution<> d(1,4);

    cout << "welcome to the game of N I M. would you like to go first? Y/N" << endl; 
    cin >> whoseTurn; 

   if (whoseTurn == yourTurn) cout << "you go first!" << endl; 
   else if (whoseTurn == compTurn) cout << "the computer goes first!" << endl;
   cout << endl; 

   while (sticks > 0){ 

        // if the player chooses to go first... 
        if (whoseTurn == yourTurn){ 
            
            cout << "There are " << sticks << " sticks in the pile.\n"
                << "\n enter the amount (between 1 and 4) of sticks that you will remove from this pile." << endl; 
            cin >> num; 
            
           // if number is 1-4, amount of sticks taken/left will be displayed 
            if ((num >= 1) && (num <= 4)){ 
                if (num >= sticks) {
                    cout << "You have won!\n";
                    playerWon = true;
                    break;
                }
               cout << "you have removed " << num << " sticks from the pile." << endl; 
               sticks -= num; 
               cout << "there are now " << sticks << " sticks left in the pile." << endl; 
               whoseTurn = compTurn;
               continue;

            } 
            // if number is less than 1 or greater than 4, user will be repeatedly prompted until done so 
            if ((num < 1) || (num > 4)){ 

                cout << "unacceptable answer >:( enter an amount between *1* and *4*" << endl; 
                //    cin >> num; 
                continue;

            } 
        }
            
         // computer enters value 
        else { 
            if ( sticks <= 4){
                  cout << "\nThe computer took " << sticks << " sticks, it won\n";
                  playerWon = false;
                  break;
            }

            int comp_choice = d(r); // gtting a random number
            sticks -= comp_choice; 
            cout << "\n the computer has taken away " << comp_choice << " sticks from the pile.\n";
              
            whoseTurn = yourTurn;
        } 
    } 
    cout <<  (playerWon ? "Congratulations, you won the game." : "Sorry, you lost.") << '\n';
}
I think I understand what I did wrong. Thank you so much!
Topic archived. No new replies allowed.