Tic Tac Toe- help and advice

I am making a tic tac toe game with competitive AI and having quite a bit of trouble. I can't figure out exactly where the problem is coming from and I have been messing with it for about a week now. The game plays correctly with two players but I'm having trouble with the AI part. The computer doesn't play outside of it's initial move unless there is an opportunity for either player to win. I had it running a little better previously but after rearranging the code so much I forgot how I had it. I am a beginner so any help and recommendations would be nice.

A side question I had was: is there any way to set up the computer's AI how I have it without typing every line. I'm looking for a shorter and concise alternative so it doesn't take up so many lines.


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
#include <iostream>
#include "System&TwoPlayers.h"

void gameMenu();
void compTurn();
void computerOponent();
void endGameC();
void intelligence();
void compWin();
char playerComp = 'O';
char playChoice = '9';

int main() {
	cout << "Would you like to play tic tac toe?" << endl
	<< "If so, press 'y', if not, press 'q' to quit." << endl;
	char play;
	cin >> play;
	if (play == 'y'){
		gameMenu();
	}
	if (play == 'q'){
		cout << "Goodbye." << endl;
}
}
void gameMenu(){
	cout << "Would you like to play tic tac toe against another player(p) or " <<
	"a computer(c)?" << endl;
	char gameChoice;
	cin >> gameChoice;
	if (gameChoice == 'p'){
		turnRotate();
	}
	else if (gameChoice == 'c'){
		computerOponent();
	}
	else{
		cout << "That is not a option." << endl;
		gameMenu();
	}
}

void computerOponent(){
	while (!win & !draw){
		if (turn){
			cout << "Player one, it is your turn." << endl
			<< "Press the number in the space you want to move." << endl;
			grid();
			mark = playerOne;
			playTurn();
			winCondition();
			turn = !turn;
	}
		else{
			mark = playerComp;
			compTurn();
			winCondition();
			turn = !turn;
			sleep(1);
		}
	}
	endGameC();
}

void compTurn(){
		intelligence();
		compWin();
		if ( playChoice == placement[0]){
			placement[0] = mark;
	}
		else if (playChoice == placement[1]){
			placement[1] = mark;
	}
		else if (playChoice == placement[2]){
			placement[2] = mark;
	}
		else if (playChoice == placement[3]){
			placement[3] = mark;	
	}
		else if (playChoice == placement[4]){
			placement[4] = mark;	
	}
		else if (playChoice == placement[5]){
			placement[5] = mark;	
	}
		else if (playChoice == placement[6]){
			placement[6] = mark;	
	}
		else if (playChoice == placement[7]){
			placement[7] = mark;	
	}
		else if (playChoice == placement[8]){
			placement[8] = mark;	
	}
		else{
			int variableConverter = playChoice - '0';
			variableConverter--;
			playChoice = variableConverter + '0';
		}
}

void endGameC(){
	if (win){
		if (mark == playerOne){
			grid();
			cout << "Congratulations. Player one has won the game." << endl;
		}
		if (mark == playerComp){
			grid();
			cout << "Yeah, that's right. I won the game!" << endl;
		}
	}
	if (draw){
		grid();
		cout << "Tie Game. There is no winner." << endl;
	}

}

void intelligence(){			////  order of code for computer placements:row1, row2, row3, column1, column2, column3,4diags
	if (placement[0] == playerOne & placement[1] == playerOne){			//// row 1 
		playChoice = '3';
	}
	if (placement[1] == playerOne & placement[2] == playerOne){
		playChoice = '1';
	}
	if (placement[0] == playerOne & placement[2] == playerOne){
		playChoice = '2';
	}
	if (placement[3] == playerOne & placement[4] == playerOne){			//// row 2
		playChoice = '6';
	}
	if (placement[4] == playerOne & placement[5] == playerOne){
		playChoice = '4';
	}
	if (placement[3] == playerOne & placement[5] == playerOne){
		playChoice = '5';
	}
	if (placement[6] == playerOne & placement[7] == playerOne){			//// row 3
		playChoice = '9';
	}
	if (placement[7] == playerOne & placement[8] == playerOne){
		playChoice = '7';
	}
	if (placement[6] == playerOne & placement[8] == playerOne){
		playChoice = '8';
	}
	if (placement[0] == playerOne & placement[3] == playerOne){			////column 1
		playChoice = '7';
	}
	if (placement[3] == playerOne & placement[6] == playerOne){
		playChoice = '1';
	}
	if (placement[0] == playerOne & placement[6] == playerOne){
		playChoice = '4';
	}
	if (placement[1] == playerOne & placement[4] == playerOne){			////column 2
		playChoice = '8';
	}
	if (placement[4] == playerOne & placement[7] == playerOne){
		playChoice = '2';
	}
	if (placement[1] == playerOne & placement[7] == playerOne){
		playChoice = '5';
	}
	if (placement[2] == playerOne & placement[5] == playerOne){			////column 3
		playChoice = '8';
	}
	if (placement[5] == playerOne & placement[8] == playerOne){
		playChoice = '3';
	}
	if (placement[2] == playerOne & placement[8] == playerOne){
		playChoice = '6';
	}
	if (placement[0] == playerOne & placement[4] == playerOne){			////diags
		playChoice = '9';
	}
	if (placement[2] == playerOne & placement[4] == playerOne){
		playChoice = '7';
	}
	if (placement[6] == playerOne & placement[4] == playerOne){
		playChoice = '3';
	}
	if (placement[8] == playerOne & placement[4] == playerOne){
		playChoice = '1';
	}
	if (placement[0] == playerOne & placement[8] == playerOne){
		playChoice = '5';
	}
	if (placement[2] == playerOne & placement[6] == playerOne){
		playChoice = '5';
	}
}

void compWin(){			////  order of code for computer placements:row1, row2, row3, column1, column2, column3,4diags
	if (placement[0] == playerComp & placement[1] == playerComp){			//// row 1
		playChoice = '3';
	}
	if (placement[1] == playerComp & placement[2] == playerComp){
		playChoice = '1';
	}
	if (placement[0] == playerComp & placement[2] == playerComp){
		playChoice = '2';
	}
	if (placement[3] == playerComp & placement[4] == playerComp){			//// row 2
		playChoice = '6';
	}
	if (placement[4] == playerComp & placement[5] == playerComp){
		playChoice = '4';
	}
	if (placement[3] == playerComp & placement[5] == playerComp){
		playChoice = '5';
	}
	if (placement[6] == playerComp & placement[7] == playerComp){			//// row 3
		playChoice = '9';
	}
	if (placement[7] == playerComp & placement[8] == playerComp){
		playChoice = '7';
	}
	if (placement[6] == playerComp & placement[8] == playerComp){
		playChoice = '8';
	}
	if (placement[0] == playerComp & placement[3] == playerComp){			////column 1
		playChoice = '7';
	}
	if (placement[3] == playerComp & placement[6] == playerComp){
		playChoice = '1';
	}
	if (placement[0] == playerComp & placement[6] == playerComp){
		playChoice = '4';
	}
	if (placement[1] == playerComp & placement[4] == playerComp){			////column 2
		playChoice = '8';
	}
	if (placement[4] == playerComp & placement[7] == playerComp){
		playChoice = '2';
	}
	if (placement[1] == playerComp & placement[7] == playerComp){
		playChoice = '5';
	}
	if (placement[2] == playerComp & placement[5] == playerComp){			////column 3
		playChoice = '8';
	}
	if (placement[5] == playerComp & placement[8] == playerComp){
		playChoice = '3';
	}
	if (placement[2] == playerComp & placement[8] == playerComp){
		playChoice = '6';
	}
	if (placement[0] == playerComp & placement[4] == playerComp){			////diags
		playChoice = '9';
	}
	if (placement[2] == playerComp & placement[4] == playerComp){
		playChoice = '7';
	}
	if (placement[6] == playerComp & placement[4] == playerComp){
		playChoice = '3';
	}
	if (placement[8] == playerComp & placement[4] == playerComp){
		playChoice = '1';
	}
	if (placement[0] == playerComp & placement[8] == playerComp){
		playChoice = '5';
	}
	if (placement[2] == playerComp & placement[6] == playerComp){
		playChoice = '5';
	}
}
On line 43, you've written this:
while (!win & !draw){

which doesn't do what you think it does.

& is the bitwise AND operator.

&& is the binary AND operator, which is what you want.

EDIT* ironically, this makes no difference.
Last edited on
Yeah I have noticed that. I messed around with it for a while because I thought it needed to be || or |, but then realized the same thing you did.

Thanks though.
Last edited on
Topic archived. No new replies allowed.