Tic tac toe game.

I'm trying to make a simple command-line tic-tac-toe game where you have an option to play against another player or the computer. I attempted to make a function where the computer tries to "think" where to put its turn, but every time I play the game, it decides to choose two places to put its turn in, even if the player has put a move in that place. I've been trying to find my mistake for a long time now, so I thought I'd ask for help. Here's the code:

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;

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

//declare functions
void printgrid();
int checkwin();
bool cpucheck();

int main(){
	START:
	int player = 1, i, choice, cpuorplayer;
	char mark;
	cout << "Choose whether to play against another player (1) or a computer (2): ";
	cin >> cpuorplayer;
	
	while((cpuorplayer<1)||(cpuorplayer>2)){
		cout << "Invalid choice. Try again: ";
		cin>> cpuorplayer;
	}	
	
	
	if(cpuorplayer==1){ //--------------------------------------------------------------------------two players
	
		do{
			printgrid();
			player=(player%2)?1:2;
			cout << "Player " << player << ", your turn. Choose your move:  ";
			cin >> choice;
	
			mark=(player == 1)?'X':'O';
	
			if (choice == 1 && g[1] == '1')
				g[1] = mark;
			else if (choice == 2 && g[2] == '2')
				g[2] = mark;
			else if (choice == 3 && g[3] == '3')
				g[3] = mark;
			else if (choice == 4 && g[4] == '4')
				g[4] = mark;
			else if (choice == 5 && g[5] == '5')
				g[5] = mark;
			else if (choice == 6 && g[6] == '6')
				g[6] = mark;
			else if (choice == 7 && g[7] == '7')
				g[7] = mark;
			else if (choice == 8 && g[8] == '8')
				g[8] = mark;
			else if (choice == 9 && g[9] == '9')
				g[9] = mark;
			else
			{
				cout<<"Invalid move ";
	
				player--;
				cin.ignore();
				cin.get();
			}
			i=checkwin();
	
			player++;
		}while(i==-1);
		
		printgrid();
		
		if(i==1)
			cout<<"Player "<<--player<<" wins.";
		else
			cout<<"It's a draw!'";
	}
	
	if(cpuorplayer==2){ //--------------------------------------------------------------------------vs computer
		do{
			printgrid();

			player = (player % 2) ? 1 : 2;

			if(player == 2){
				cout << "Press Enter for CPU's turn." << endl;
				if(!cpucheck())
					choice = rand() % 9;
					mark = 'X';
					int turn = 1;
					int placed = 0;
					
					while(placed == 0){
						if (choice == 1 && g[1] == '1'){
							g[1] = mark;
							placed = 1;
						}
						else if (choice == 2 && g[2] == '2'){
							g[2] = mark;
							placed = 1;
						}
						else if (choice == 3 && g[3] == '3'){
							g[3] = mark;
							placed = 1;
						}
						else if (choice == 4 && g[4] == '4'){
							g[4] = mark;
							placed = 1;
						}
						else if (choice == 5 && g[5] == '5'){
							g[5] = mark;
							placed = 1;
						}
						else if (choice == 6 && g[6] == '6'){
							g[6] = mark;
							placed = 1;
						}
						else if (choice == 7 && g[7] == '7'){
							g[7] = mark;
							placed = 1;
						}
						else if (choice == 8 && g[8] == '8'){
							g[8] = mark;
							placed = 1;
						}
						else if (choice == 9 && g[9] == '9'){
							g[9] = mark;
							placed = 1;
						}
						else
							choice=rand()%9;
					}
				
				i=checkwin();
				player++;
				_getche();
			}

		else if(player==1){
			cout << "Player, your turn. Choose your move:  ";
			cin >> choice;
			mark='O';

			if (choice == 1 && g[1] == '1')
				g[1] = mark;
			else if (choice == 2 && g[2] == '2')
				g[2] = mark;
			else if (choice == 3 && g[3] == '3')
				g[3] = mark;
			else if (choice == 4 && g[4] == '4')
				g[4] = mark;
			else if (choice == 5 && g[5] == '5')
				g[5] = mark;
			else if (choice == 6 && g[6] == '6')
				g[6] = mark;
			else if (choice == 7 && g[7] == '7')
				g[7] = mark;
			else if (choice == 8 && g[8] == '8')
				g[8] = mark;
			else if (choice == 9 && g[9] == '9')
				g[9] = mark;
			else
			{
				cout<<"Invalid move ";
	
				player--;
				cin.ignore();
				cin.get();
			}
			i=checkwin();
	
			player++;
		}
		}while(i == -1);	
		printgrid();
		
		if(i==1){
			if((player-1) == 1)
				cout << "Congratulations, you won!" << endl;
			if((player-1) == 2)
				cout << "CPU won!" << endl;
		}
		else
			cout<<"It's a draw!'";

	}
		
	
	int yn;
	cout << endl << "Press 1 if you want to play again ";
	cin >> yn;
	
	if (yn == 1){
		g[1] = '1', g[2] = '2', g[3] = '3', g[4] = '4', g[5] = '5', g[6] = '6', g[7] = '7', g[8] = '8', g[9] = '9';
		goto START;
	}
	
	return 0;
}
//------------------- start of additional functions

void printgrid(){ //prints out the grid
	cout << endl;
	cout << "   |   |" << endl;
	cout << " " << g[1] << " | " << g[2] << " | " << g[3] << endl;
	cout << "   |   |" << endl;
	cout << "-----------" << endl;
	cout << "   |   |" << endl;
	cout << " " << g[4] << " | " << g[5] << " | " << g[6] << endl;
	cout << "   |   |" << endl;
	cout << "-----------" << endl;
	cout << "   |   |" << endl;
	cout << " " << g[7] << " | " << g[8] << " | " << g[9] << endl;
	cout << "   |   |" << endl;
	cout << endl;
}



int checkwin(){ //check if anyone has won. returns 1 if has, 0 if game is over with a draw, -1 for game in progress
if (g[1] == g[2] && g[2] == g[3])
		return 1;
	else if (g[4] == g[5] && g[5] == g[6])
		return 1;
	else if (g[7] == g[8] && g[8] == g[9])
		return 1;
	else if (g[1] == g[4] && g[4] == g[7])
		return 1;
	else if (g[2] == g[5] && g[5] == g[8])
		return 1;
	else if (g[3] == g[6] && g[6] == g[9])
		return 1;
	else if (g[1] == g[5] && g[5] == g[9])
		return 1;
	else if (g[3] == g[5] && g[5] == g[7])
		return 1;
	else if (g[1] != '1' && g[2] != '2' && g[3] != '3' 
                	&& g[4] != '4' && g[5] != '5' && g[6] != '6' 
                	&& g[7] != '7' && g[8] != '8' && g[9] != '9')
		return 0;
	else
		return -1;
}

bool cpucheck(){ //------------------------------------------------------------------checks for offense and defense, returns 1 if makes a move, 0 if nothing
	if(g[2] == g[3] || g[4] == g[7] || g[5] == g[9]){
		g[1] = 'X';
		return true;
	}
	else if(g[1] == g[3] || g[5] == g[8]){
		g[2] = 'X';
		return true;
	}
	else if(g[1] == g[2] || g[6] == g[9] || g[5] == g[7]){
		g[3] = 'X';
		return true;
	}
	else if(g[5] == g[6] || g[1] == g[7]){
		g[4] = 'X';
		return true;
	}
	else if(g[2] == g[8] || g[4] == g[6] || g[1] == g[9] || g[3] == g[7]){
		g[5] = 'X';
		return true;
	}
	else if(g[3] == g[9] || g[4] == g[5]){
		g[6] = 'X';
		return true;
	}
	else if(g[3] == g[5] || g[1] == g[4] || g[8] == g[9]){
		g[7] = 'X';
		return true;
	}
	else if(g[2] == g[5] || g[7] == g[9]){
		g[8] = 'X';
		return true;
	}
	else if(g[1] == g[5] || g[3] == g[6] || g[7] == g[8]){
		g[9] = 'X';
		return true;
	}
	else if(((g[1] == g[9] && g[5] == 'X') && g[1] == 'O') || ((g[3] == g[7] && g[3] == 'O') && g[5] == 'X')){ //-------------------------specialized defensive mechanism
		if(g[2] == '2'){
			g[2] = 'X';
			return true;
		}
		else if(g[6] == '6'){
			g[6] = 'X';
			return true;
		}
		else if(g[8] == '8'){
			g[8] = 'X';
			return true;
		}
		else if(g[4] == '4'){
			g[4] = 'X';
			return true;
		}
	}
	else if(g[5] == 'O' && (g[1] == 'X' || g[3] == 'X' || g[7] == 'X' || g[9] == 'X')){ //-----------------------specialized offense
		if(g[1] == 'X' && g[9] == '9'){
			g[9] = 'X';
			return true;
		}
		else if(g[3] == 'X' && g[7] == '7'){
			g[7] = 'X';
			return true;
		}
		else if(g[9] == 'X' && g[1] == '1'){
			g[1] = 'X';
			return true;
		}
		else if(g[7] == 'X' && g[3] == '3'){
			g[3] = 'X';
			return true;
		}
	else
		return false;
	}
}

// -------------------- end of additional functions 
but every time I play the game, it decides to choose two places to put its turn in

Perhaps you should print the value of choice after doing choice=rand()%9;

Also this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
			if (choice == 1 && g[1] == '1')
				g[1] = mark;
			else if (choice == 2 && g[2] == '2')
				g[2] = mark;
			else if (choice == 3 && g[3] == '3')
				g[3] = mark;
			else if (choice == 4 && g[4] == '4')
				g[4] = mark;
			else if (choice == 5 && g[5] == '5')
				g[5] = mark;
			else if (choice == 6 && g[6] == '6')
				g[6] = mark;
			else if (choice == 7 && g[7] == '7')
				g[7] = mark;
			else if (choice == 8 && g[8] == '8')
				g[8] = mark;
			else if (choice == 9 && g[9] == '9')
				g[9] = mark;
Can be simplified to g[choice] = mark;
you might want to add if (choice >= 1 && 9 >= choice) above that though.
thanks, simplified that! also noticed i was missing curly brackets after an if statement at one point, fixed that, too. the problem of the computer making two moves in one turn is solved, however now sometimes it just doesn't make a move on its turn. either the rand()%9 value is the move the player made in their previous turn, or there is no cout.

updated code:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;

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

//declare functions
void printgrid();
int checkwin();
bool cpucheck();

int main(){
	START:
	int player = 1, i, choice, cpuorplayer;
	char mark;
	cout << "Choose whether to play against another player (1) or a computer (2): ";
	cin >> cpuorplayer;
	
	while((cpuorplayer<1)||(cpuorplayer>2)){
		cout << "Invalid choice. Try again: ";
		cin>> cpuorplayer;
	}	
	
	
	if(cpuorplayer==1){ //--------------------------------------------------------------------------two players
	
		do{
			printgrid();
			player=(player%2)?1:2;
			cout << "Player " << player << ", your turn. Choose your move:  ";
			cin >> choice;
	
			mark=(player == 1)?'X':'O';
	
			if (choice >= 1 && choice <= 9){
				if (g[choice] != 'X' && g[choice] != 'O')
					g[choice] = mark;
			}
			else
			{
				cout<<"Invalid move ";
	
				player--;
				cin.ignore();
				cin.get();
			}
			i=checkwin();
	
			player++;
		}while(i==-1);
		
		printgrid();
		
		if(i==1)
			cout<<"Player "<<--player<<" wins.";
		else
			cout<<"It's a draw!'";
	}
	
	if(cpuorplayer==2){ //--------------------------------------------------------------------------vs computer
		do{
			printgrid();

			player = (player % 2) ? 1 : 2;

			if(player == 2){
				cout << "Press Enter for CPU's turn." << endl;
				if(!cpucheck()){
					choice = rand() % 9;
					cout << "Rand % 9 is " << choice << endl;
					mark = 'X';
					int placed = 0;
					
					while(placed == 0){
						if (choice >= 1 && choice <= 9){
							if (g[choice] != 'X' && g[choice] != 'O')
								g[choice] = mark;
								placed = 1;
						}
						else
							choice=rand()%9;
					}}
				
				i=checkwin();
				player++;
				_getche();
			}

			else if(player==1){
			cout << "Player, your turn. Choose your move:  ";
			cin >> choice;
			mark='O';

			if (choice >= 1 && choice <= 9){
				if (g[choice] != 'X' && g[choice] != 'O')
					g[choice] = mark;
			}
			else
			{
				cout<<"Invalid move ";
	
				player--;
				cin.ignore();
				cin.get();
			}
			i=checkwin();
	
			player++;
		}
		}while(i == -1);	
		printgrid();
		
		if(i==1){
			if((player-1) == 1)
				cout << "Congratulations, you won!" << endl;
			if((player-1) == 2)
				cout << "CPU won!" << endl;
		}
		else
			cout<<"It's a draw!'";

	}
		
	
	int yn;
	cout << endl << "Press 1 if you want to play again ";
	cin >> yn;
	
	if (yn == 1){
		g[1] = '1', g[2] = '2', g[3] = '3', g[4] = '4', g[5] = '5', g[6] = '6', g[7] = '7', g[8] = '8', g[9] = '9';
		goto START;
	}
	
	return 0;
}
//------------------- start of additional functions

void printgrid(){ //prints out the grid
	cout << endl;
	cout << "   |   |" << endl;
	cout << " " << g[1] << " | " << g[2] << " | " << g[3] << endl;
	cout << "   |   |" << endl;
	cout << "-----------" << endl;
	cout << "   |   |" << endl;
	cout << " " << g[4] << " | " << g[5] << " | " << g[6] << endl;
	cout << "   |   |" << endl;
	cout << "-----------" << endl;
	cout << "   |   |" << endl;
	cout << " " << g[7] << " | " << g[8] << " | " << g[9] << endl;
	cout << "   |   |" << endl;
	cout << endl;
}



int checkwin(){ //check if anyone has won. returns 1 if has, 0 if game is over with a draw, -1 for game in progress
if (g[1] == g[2] && g[2] == g[3])
		return 1;
	else if (g[4] == g[5] && g[5] == g[6])
		return 1;
	else if (g[7] == g[8] && g[8] == g[9])
		return 1;
	else if (g[1] == g[4] && g[4] == g[7])
		return 1;
	else if (g[2] == g[5] && g[5] == g[8])
		return 1;
	else if (g[3] == g[6] && g[6] == g[9])
		return 1;
	else if (g[1] == g[5] && g[5] == g[9])
		return 1;
	else if (g[3] == g[5] && g[5] == g[7])
		return 1;
	else if (g[1] != '1' && g[2] != '2' && g[3] != '3' 
                	&& g[4] != '4' && g[5] != '5' && g[6] != '6' 
                	&& g[7] != '7' && g[8] != '8' && g[9] != '9')
		return 0;
	else
		return -1;
}

bool cpucheck(){ //------------------------------------------------------------------checks for offense and defense, returns 1 if makes a move, 0 if nothing
	if(g[2] == g[3] || g[4] == g[7] || g[5] == g[9]){
		g[1] = 'X';
		return true;
	}
	else if(g[1] == g[3] || g[5] == g[8]){
		g[2] = 'X';
		return true;
	}
	else if(g[1] == g[2] || g[6] == g[9] || g[5] == g[7]){
		g[3] = 'X';
		return true;
	}
	else if(g[5] == g[6] || g[1] == g[7]){
		g[4] = 'X';
		return true;
	}
	else if(g[2] == g[8] || g[4] == g[6] || g[1] == g[9] || g[3] == g[7]){
		g[5] = 'X';
		return true;
	}
	else if(g[3] == g[9] || g[4] == g[5]){
		g[6] = 'X';
		return true;
	}
	else if(g[3] == g[5] || g[1] == g[4] || g[8] == g[9]){
		g[7] = 'X';
		return true;
	}
	else if(g[2] == g[5] || g[7] == g[9]){
		g[8] = 'X';
		return true;
	}
	else if(g[1] == g[5] || g[3] == g[6] || g[7] == g[8]){
		g[9] = 'X';
		return true;
	}
	else if(((g[1] == g[9] && g[5] == 'X') && g[1] == 'O') || ((g[3] == g[7] && g[3] == 'O') && g[5] == 'X')){ //-------------------------specialized defensive mechanism
		if(g[2] == '2'){
			g[2] = 'X';
			return true;
		}
		else if(g[6] == '6'){
			g[6] = 'X';
			return true;
		}
		else if(g[8] == '8'){
			g[8] = 'X';
			return true;
		}
		else if(g[4] == '4'){
			g[4] = 'X';
			return true;
		}
	}
	else if(g[5] == 'O' && (g[1] == 'X' || g[3] == 'X' || g[7] == 'X' || g[9] == 'X')){ //-----------------------specialized offense
		if(g[1] == 'X' && g[9] == '9'){
			g[9] = 'X';
			return true;
		}
		else if(g[3] == 'X' && g[7] == '7'){
			g[7] = 'X';
			return true;
		}
		else if(g[9] == 'X' && g[1] == '1'){
			g[1] = 'X';
			return true;
		}
		else if(g[7] == 'X' && g[3] == '3'){
			g[3] = 'X';
			return true;
		}
	else
		return false;
	}
}

// -------------------- end of additional functions 
Topic archived. No new replies allowed.