Loop Won't Loop

OK, I'm still working on the tic tac toe problem. I'm close, but still no cigar. I've created a loop with the exit being the test to see if one of the players has one. I haven't written the exit so it should just keep cycling. It stops after player2's turn. Take a look, please:

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// The players take turns
while (win == 0)
{
    // Loop for player1's turn
    while (player1 == 0)
    {
        // Get player1's choice
        cout << "Player1, make your choice 1- 9: " << endl;
        cin >> choice;
        cout << endl << endl;
        // Test to see if space is already taken.
        // If not, assign 'X'
        switch (choice)
        {
            case 1:
            {
                if (board[0] == 'a')
                {
                    board[0] = 'X';
                    player1 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player1 = 0;
                }
            }
            case 2:
            {
                if (board[1] == 'a')
                {
                    board[1] = 'X';
                    player1 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player1 = 0;
                }
            }
            case 3:
            {
                if (board[2] == 'a')
                {
                    board[2] = 'X';
                    player1 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player1 = 0;
                }
            }
            case 4:
            {
                if (board[3] == 'a')
                {
                    board[3] = 'X';
                    player1 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player1 = 0;
                }
            }
            case 5:
            {
                if (board[4] == 'a')
                {
                    board[4] = 'X';
                    player1 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player1 = 0;
                }
            }
            case 6:
            {
                if (board[5] == 'a')
                {
                    board[5] = 'X';
                    player1 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player1 = 0;
                }
            }
            case 7:
            {
                if (board[6] == 'a')
                {
                    board[6] = 'X';
                    player1 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player1 = 0;
                }
            }
            case 8:
            {
                if (board[7] == 'a')
                {
                    board[7] = 'X';
                    player1 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player1 = 0;
                }
            }
            case 9:
            {
                if (board[8] == 'a')
                {
                    board[8] = 'X';
                    player1 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player1 = 0;
                }
            }

        }

        cout << board[0] << board[1] << board[2] << endl
             << board[3] << board[4] << board[5] << endl
             << board[6] << board[7] << board[8] << endl << endl;
    }

    // Loop for player2's turn
    while (player2 == 0)
    {
        // Get player2's choice
        cout << "Player2, make your choice 1- 9: " << endl;
        cin >> choice;
        cout << endl << endl;
        // Test to see if space is already taken.
        // If not, assign 'O'
        switch (choice)
        {
           case 1:
           {
               if (board[0] == 'a')
                    {
                        board[0] = 'O';
                        player2 = 1;
                        break;
                    }
                    else
                    {
                        cout << "That space is already taken";
                        player2 = 0;
                    }
            }
            case 2:
            {
                if (board[1] == 'a')
                {
                    board[1] = 'O';
                    player2 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player2 = 0;
                }
            }
            case 3:
            {
                if (board[2] == 'a')
                {
                    board[2] = 'O';
                    player2 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player2 = 0;
                }
            }
            case 4:
            {
                if (board[3] == 'a')
                {
                    board[3] = 'O';
                    player2 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player2 = 0;
                }
            }
            case 5:
            {
                if (board[4] == 'a')
                {
                    board[4] = 'O';
                    player2 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player2 = 0;
                }
            }
            case 6:
            {
                if (board[5] == 'a')
                {
                    board[5] = 'O';
                    player2 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player2 = 0;
                }
            }
            case 7:
            {
                if (board[6] == 'a')
                {
                    board[6] = 'O';
                    player2 = 1;
                    break;
                }
                else
                {
                    cout << "That space is already taken";
                    player2 = 0;
                }
            }
            case 8:
            {
                if (board[7] == 'a')
                    {
                        board[7] = 'O';
                        player2 = 1;
                        break;
                    }
                    else
                    {
                        cout << "That space is already taken";
                        player2 = 0;
                    }
                }
                case 9:
                {
                    if (board[8] == 'a')
                    {
                        board[8] = 'O';
                        player2 = 1;
                        break;
                    }
                    else
                    {
                        cout << "That space is already taken";
                        player2 = 0;
                    }
                }
            }
            cout << board[0] << board[1] << board[2] << endl
                 << board[3] << board[4] << board[5] << endl
                 << board[6] << board[7] << board[8] << endl << endl;
        }
    }
Firstly, you have some obvious code duplication for player1 and player2's turns. You should make a function that takes in the current player and does their turn.

Your loop stops after player2's turn because at that point both player1 and player2 are equal to 1. So, both loops while (player1 == 0) and while (player2 == 0) are getting skipped over and thus your main loop is infinite looping. You need to reset player1 and player2 to 0 after their respective turn.

Also, your case statements are incorrect. If the player chooses a space that's already taken, it will run the next case (since the else conditions don't have a break statement). By the way, the break statements in a case make the execution break from the switch statement, not the surrounding loop.

Your case statements should look like this (using case 1 of player1 as an example):

1
2
3
4
5
6
7
8
9
10
11
12
case 1:
   if (board[0] == 'a')
   {
      board[0] = 'O';
      player2 = 1;
   }
   else
   {
      cout << "That space is already taken";
      player2 = 0;
   }
   break;


And you may consider having a default case to handle if the player selects a number besides 1-9.

Why are you looping each player's turn? Players only take one turn at a time...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Player {
   string name;
   char symbol;

 public:
   Player(string, char);
   ??? makeMove(int, ???);
};
//...

int main() {
   //...
   Player p1("Player 1", 'X'),
          p2("Player 2", 'O');

   while( !gameOver ) {
      p1.makeMove();
      p2.makeMove();
   }
   //...
}
Or you can try using a for loop. Since there can't be over 9 turns in a tic-tac-toe game, the following code should make the turns alternate:
1
2
3
4
5
6
7
8
9
10
11
12
for(int i = 0;i<9;i++)
{
   if (i%2 == 0)
   {
         //For player 1
   }

   else
   {
         //For player 2
   }
}


I too am making a Tic-Tac-Toe game, but at stuck at making an computer player (two player works perfectly).
If you are interested you can see the prototype of my code in this page:
http://www.cplusplus.com/forum/beginner/44450/#msg240735
Last edited on
Topic archived. No new replies allowed.