TicTacToe vs computer

Im trying to code a Tictactoe game against the computer, my game works perfectly expect I can't get the computer to automatically place the 'O' marker. However it will generate and output the number on the computers turn. Any help would be much appreciated, I have attached the code below.

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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

class tictactoe {

public:

    int playerWins;
    int Draw;

    void draw_board(char board[])
    {

    cout << "\n\n\tTic Tac Toe\n\n";

    cout << "Player (X)  -  Computer (O)" << endl << endl;
    cout << endl;

    cout << "     |     |     " << endl;
    cout << "  " << board[1] << "  |  " << board[2] << "  |  " << board[3] << endl;

    cout << "_____|_____|_____" << endl;
    cout << "     |     |     " << endl;

    cout << "  " << board[4] << "  |  " << board[5] << "  |  " << board[6] << endl;

    cout << "_____|_____|_____" << endl;
    cout << "     |     |     " << endl;

    cout << "  " << board[7] << "  |  " << board[8] << "  |  " << board[9] << endl;

    cout << "     |     |     " << endl << endl;
    }
    void clearScreen(){
                for (int i = 0; i < 50; i++)
                        cout << endl;
        }

    bool isGameOver(char board[])
    {
        if (board[1] == board[2] && board[2] == board [3] )
        {
            if (board[1] == 'X')
            {
            this->playerWins = 1;
                        return true;
            }
            else
            {
            this->playerWins = 2;
                        return true;
            }
        }
    else if (board[4] == board[5] && board[5] == board [6] )
        {
            if (board[4] == 'X')
            {
            this->playerWins = 1;
                        return true;
            }
            else
            {
            this->playerWins = 2;
                        return true;
            }
        }
        else if (board[7] == board[8] && board[8] == board [9] )
        {
            if (board[7] == 'X')
            {
            this->playerWins = 1;
                        return true;
            }
            else
            {
            this->playerWins = 2;
                        return true;
            }
        }
        else if (board[1] == board[5] && board[5] == board [9] )
        {
            if (board[1] == 'X')
           {
            this->playerWins = 1;
                        return true;
            }
            else
            {
            this->playerWins = 2;
                        return true;
            }
        }
        else if (board[3] == board[5] && board[5] == board [7] )
        {
            if (board[3] == 'X')
            {
            this->playerWins = 1;
                        return true;
            }
            else
            {
            this->playerWins = 2;
                        return true;
            }
        }
        else if (board[1] == board[4] && board[4] == board [7] )
        {
            if (board[1] == 'X')
            {
            this->playerWins = 1;
                        return true;
            }
            else
            {
            this->playerWins = 2;
                        return true;
            }
        }
        else if (board[2] == board[5] && board[5] == board [8] )
        {
            if (board[2] == 'X')
            {
            this->playerWins = 1;
                        return true;
            }
            else
            {
            this->playerWins = 2;
                        return true;
            }
        }
        else if (board[3] == board[6] && board[6] == board [9] )
        {
            if (board[3] == 'X')
            {
            this->playerWins = 1;
                        return true;
            }
            else
            {
            this->playerWins = 2;
                        return true;
            }
        }
                else if (board[1] != '1' && board[2] != '2' && board[3] != '3'
                    && board[4] != '4' && board[5] != '5' && board[6] != '6'
                  && board[7] != '7' && board[8] != '8' && board[9] != '9')
            {
            this->Draw = 3;
                        return true;
            }

                return false;
        }

};

int main() {

    tictactoe game;
    int playerTurn = 1;
    char playerMark, choice;
    int Move;
    char board[] = { '0','1', '2', '3', '4', '5', '6', '7', '8', '9' };
    do {
            	game.clearScreen();
		game.draw_board(board);

		cout << "TicTacToe" << endl;

		if (playerTurn == 1)
		{
			playerMark = 'X';
		}
		else
		{
			playerMark = 'O';
			{
                srand(time(0));
                Move = rand() % 9 + 1;
                cout << "Computer Move : "<< Move <<endl;
			}
		}
		  cout << "Player enter a number: " ;
     cin >> choice ;

    switch (choice)
		{
		case '1':
			if (playerTurn == 1)
			{
				board[1] = playerMark;
				playerTurn = 2;
			}
			else if (playerTurn == 2)
			{
				board[1] = playerMark;
				playerTurn = 1;
			}
			break;
		case '2':
			if (playerTurn == 1)
			{
				board[2] = playerMark;
				playerTurn = 2;
			}
			else if (playerTurn == 2)
			{
				board[2] = playerMark;
				playerTurn = 1;
			}
			break;
		case '3':
			if (playerTurn == 1)
			{
				board[3] = playerMark;
				playerTurn = 2;
			}
			else if (playerTurn == 2)
			{
				board[3] = playerMark;
				playerTurn = 1;
			}
			break;
		case '4':
			if (playerTurn == 1)
			{
				board[4] = playerMark;
				playerTurn = 2;
			}
			else if (playerTurn == 2)
			{
				board[4] = playerMark;
				playerTurn = 1;
			}
			break;
		case '5':
			if (playerTurn == 1)
			{
				board[5] = playerMark;
				playerTurn = 2;
			}
			else if (playerTurn == 2)
			{
				board[5] = playerMark;
				playerTurn = 1;
			}
			break;
		case '6':
			if (playerTurn == 1)
			{
				board[6] = playerMark;
				playerTurn = 2;
			}
			else if (playerTurn == 2)
			{
				board[6] = playerMark;
				playerTurn = 1;
			}
			break;
		case '7':
			if (playerTurn == 1)
			{
				board[7] = playerMark;
				playerTurn = 2;
			}
			else if (playerTurn == 2)
			{
				board[7] = playerMark;
				playerTurn = 1;
			}
			break;
		case '8':
			if (playerTurn == 1)
			{
				board[8] = playerMark;
				playerTurn = 2;
			}
			else if (playerTurn == 2)
			{
				board[8] = playerMark;
				playerTurn = 1;
			}
			break;
		case '9':
			if (playerTurn == 1)
			{
				board[9] = playerMark;
				playerTurn = 2;
			}
			else if (playerTurn == 2)
			{
				board[9] = playerMark;
				playerTurn = 1;
			}
			break;
		default:
			break;

        }


	} while (!game.isGameOver(board));
	game.clearScreen();
	game.draw_board(board);
	if (game.playerWins == 1)
	{
		cout << "Game Over! Player Wins!" << endl;
	}
	else if (game.playerWins == 2)
	{
		cout << "Game Over! Computer Wins!" << endl;
	}
	else if (game.Draw == 3)
    {
		cout << "Game Over! Draw!" << endl;
	}

	return 0;
}
Last edited on
it looks like it needs
board[Move] = 'O'; //or playermark
where you do move= rand stuff for the computer...

or, choice = move and if its computer turn, do not cin choice? Something like that?

basically, you compute move but never used it.
Last edited on
Line 13: Draw is never initialized. You should have a constructor to initialize your class.

Line 168: Why is board not an attribute of your game class?

Line 183: Do not call srand() multiple times (inside a loop). srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Lines 193-299: All your cases are nearly identical except for the index to the board. This is a good indication you need a function. BTW, choice should be an int, then you can use it directly as an index.
159
160
161
162
163
164
165
166
167
168
169
170
171
void do_turn(int indx)
    {
        if (playerTurn == 1)
        {
            board[indx] = playerMark;
            playerTurn = 2;
        }
        else if (playerTurn == 2)
        {
            board[indx] = playerMark;
            playerTurn = 1;
        }
    }

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Thanks for the help I have edited and used the formatting button, however I still can't seem to get the random move to actually display on the board.
It looks like you still haven't taken jonnin's advice. Reread his comment.

Also, see my code and AbstractAnon's bug fix in this post:
http://www.cplusplus.com/forum/beginner/247384/
Topic archived. No new replies allowed.