Tic Tac Toe AI

The program just crashes after a couple of go's and i can't figure out why

#
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
include <iostream>
	#include<random>
	#include <ctime>

	using namespace std;

	char grid[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	char player = 'X';
	char computer = 'O';

	void draw()
	{
		cout << "Tic Tac Toe Board" << endl;
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				cout << grid[i][j] << " ";
			}
			cout << endl;
		}
	}

	void input()
	{
		cout << "Please select where you want to go " << endl;
		int choice;
		cin >> choice;

		if (choice == 1)
		{
			if (grid[0][0] == '1')
				grid[0][0] = player;
			else
			{
				cout << "Grid is already in use" << endl;
				input();
			}
		}
		if (choice == 2)
		{
			if (grid[0][1] == '2')
				grid[0][1] = player;
			else
			{
				cout << "Grid is already in use" << endl;
				input();
			}
		}
		if (choice == 3)
		{
			if (grid[0][2] == '3')
				grid[0][2] = player;
			else
			{
				cout << "Grid is already in use" << endl;
				input();
			}
		}
		if (choice == 4)
		{
			if (grid[1][0] == '4')
				grid[1][0] = player;
			else
			{
				cout << "Grid is already in use" << endl;
				input();
			}
		}
		if (choice == 5)
		{
			if (grid[1][1] == '5')
				grid[1][1] = player;
			else
			{
				cout << "Grid is already in use" << endl;
				input();
			}
		}
		if (choice == 6)
		{
			if (grid[1][2] == '6')
				grid[1][2] = player;
			else
			{
				cout << "Grid is already in use" << endl;
				input();
			}
		}
		if (choice == 7)
		{
			if (grid[2][0] == '7')
				grid[2][0] = player;
			else
			{
				cout << "Grid is already in use" << endl;
				input();
			}
		}
		if (choice == 8)
		{
			if (grid[2][1] == '8')
				grid[2][1] = player;
			else
			{
				cout << "Grid is already in use" << endl;
				input();
			}
		}
		if (choice == 9)
		{
			if (grid[2][2] == '9')
				grid[2][2] = player;
			else
			{
				cout << "Grid is already in use" << endl;
				input();
			}
		}

	}

	char win()
	{
		if (grid[0][0] == 'X' && grid[0][1] == 'X' && grid[0][2] == 'X')
			return 'X';

		if (grid[1][0] == 'X' && grid[1][1] == 'X' && grid[1][2] == 'X')
			return 'X';

		if (grid[2][0] == 'X' && grid[2][1] == 'X' && grid[2][2] == 'X')
			return 'X';

		if (grid[0][0] == 'X' && grid[1][0] == 'X' && grid[2][0] == 'X')
			return 'X';

		if (grid[0][1] == 'X' && grid[1][1] == 'X' && grid[2][1] == 'X')
			return 'X';

		if (grid[0][2] == 'X' && grid[1][2] == 'X' && grid[2][2] == 'X')
			return 'X';

		if (grid[0][0] == 'X' && grid[1][1] == 'X' && grid[2][2] == 'X')
			return 'X';

		if (grid[2][0] == 'X' && grid[1][1] == 'X' && grid[0][2] == 'X')
			return 'X';


		if (grid[0][0] == 'O' && grid[0][1] == 'O' && grid[0][2] == 'O')
			return 'O';

		if (grid[1][0] == 'O' && grid[1][1] == 'O' && grid[1][2] == 'O')
			return 'O';

		if (grid[2][0] == 'O' && grid[2][1] == 'O' && grid[2][2] == 'O')
			return 'O';

		if (grid[0][0] == 'O' && grid[1][0] == 'O' && grid[2][0] == 'O')
			return 'O';

		if (grid[0][1] == 'O' && grid[1][1] == 'O' && grid[2][1] == 'O')
			return 'O';

		if (grid[0][2] == 'O' && grid[1][2] == 'O' && grid[2][2] == 'O')
			return 'O';

		if (grid[0][0] == 'O' && grid[1][1] == 'O' && grid[2][2] == 'O')
			return 'O';

		if (grid[2][0] == 'O' && grid[1][1] == 'O' && grid[0][2] == 'O')
			return 'O';

		return '/';
	}

	void opponent()
	{
		srand(static_cast<unsigned int>(time(0)));
		int secretNumber = rand() % 9 + 1;

		if (secretNumber == 1)
		{
			if (grid[0][0] == '1')
			{
				grid[0][0] = computer;
			}
			else if (grid[0][0] == 'X' || grid[0][0] == 'O')
			{
				opponent();
			}
		}
		if (secretNumber == 2)
		{
			if (grid[0][1] == '2')
			{
				grid[0][1] = computer;
			}
			else if (grid[0][1] == 'X' || grid[0][1] == 'O')
			{
				opponent();
			}
		}
		if (secretNumber == 3)
		{
			if (grid[0][2] == '3')
			{
				grid[0][2] = computer;
			}
			else if (grid[0][2] == 'X' || grid[0][2] == 'O')
			{
				opponent();
			}
		}
		if (secretNumber == 4)
		{
			if (grid[1][0] == '4')
			{
				grid[1][0] = computer;
			}
			else if (grid[1][0] == 'X' || grid[1][0] == 'O')
			{
				opponent();
			}
		}
		if (secretNumber == 5)
		{
			if (grid[1][1] == '5')
			{
				grid[1][1] = computer;
			}
			else if (grid[1][1] == 'X' || grid[1][1] == 'O')
			{
				opponent();
			}
		}
		if (secretNumber == 6)
		{
			if (grid[1][2] == '6')
			{
				grid[1][2] = computer;
			}
			else if (grid[1][2] == 'X' || grid[1][2] == 'O')
			{
				opponent();
			}
		}
		if (secretNumber == 7)
		{
			if (grid[2][0] == '7')
			{
				grid[2][0] = computer;
			}
			else if (grid[2][0] == 'X' || grid[2][0] == 'O')
			{
				opponent();
			}
		}
		if (secretNumber == 8)
		{
			if (grid[2][1] == '8')
			{
				grid[2][1] = computer;
			}
			else if (grid[2][1] == 'X' || grid[2][1] == 'O')
			{
				opponent();
			}
		}
		if (secretNumber == 9)
		{
			if (grid[2][2] == '9')
			{
				grid[2][2] = computer;
			}
			else if (grid[2][2] == 'X' || grid[2][2] == 'O')
			{
				opponent();
			}
		}

	}

	int main()

	{
		draw();

		while (win() != 'X' && win() != 'O')
		{
			input();
			draw();

			if (win() == 'X')
			{
				cout << "You won, congratulations!!" << endl;
			}

			opponent();
			draw();

			if (win() == 'O')
			{
				cout << "You were defeated by a machine! Pathetic" << endl;
			}
		}

		system("pause");

		return 0;
	}
Last edited on
srand is normally only called once in a program, unless you want to reset it to a known state to get the same set of values again. Its not the problem, just not really right either. Also consider c++ <random> instead.

Is input being recursive intentional? This could be a problem to debug and could crash if you enter bad data enough times, but as coded I think its ok.


opponent's recursion is more dangerous. It could blow the call stack easily if the board were nearly full and rand were uncooperative. Do you see a pattern where it crashes on the AI's turn when the board is very nearly full?

finally, please use code tags <> in the editor on the side bar for large code blocks.
Last edited on
Hi

I included the <random> header at the top?

the input side needs removing to be honest, that's when the game was built for the player to play themselves.

Yes, that's the issue i'm having, after about 3 turns the program crashes.

I guess my question is, is there a workaround for this or is the entire methodology flawed?
Last edited on
I can include <vector> and still do int* ip = new int[1000] too. The header is irrelevant.
You did not use <random> tools, you used C tools. Look at the fine examples in this site's reference area on random. But this is just so you can learn something, the C code still works so fix this issue last once it all works.


The method is flawed. Do not use recursion for this.
use a loop.
something like

do
get input
check it
if it is not used, update board.
while(input is a used square)

would be better. Recursion is a tricky subject and while what you have is technically correct it can get into trouble. Recursion calls the function *again*, creating a new copy of local variables and more junk on the stack, and if you roll, its used, push stack, roll, its used, push stack, roll, its used, push stack.... after a while that stack runs dry and it blows up. You only want to use 'controlled recursion' where the code is known to be turned to a loop by the compiler OR you control the iterations so it does not blow out.

there is a work-around. you can put a loop around your random number getter and fix it:
bool tbl[9] = {false};
do
loc = rand()%9;
while tbl[loc] == false
tbl[loc] = true;
...
this keeps you from calling the recursion every time -- the most it can be called now is 8 times, which should be safe.

you need to be careful with that idea too, as tbl needs to be passed into the function, or it will just make new ones every iteration :)
The workaround is just that -- an ugly band-aid. I recommend not doing this but it can be made to work. In general, in the wild.. just about anything can be made to work, even total train wrecks, but you should avoid this unless no other option.

This is actually a very interesting situation you have created. You can learn a lot from it, if you follow what I tried to explain.
Last edited on
You have a lot of code that is nearly identical. That's always a good sign that it can be simplified. For example, is a version of input() that is functionally the same as yours:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void
input()
{
    cout << "Please select where you want to go " << endl;
    int choice;
    cin >> choice;

    int r = (choice-1) / 3;     // row
    int c = (choice-1) % 3;     // column
    char ch = choice + '0';     // '1', '2', '3', etc.

    if (grid[r][c] == ch) {
        grid[r][c] = player;
    } else {
        cout << "Grid is already in use" << endl;
        input();
    }
}

Topic archived. No new replies allowed.