Improve tic-tac-toe game

My following code plays a tic tac toe game vs computer, where the computer is making random moves.

I am trying to modify the application so that I can choose who is going to make the first move, me or the computer, but I still haven't found a way.

Help will be greatly appreciated!

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
#include "stdafx.h"
#include <iostream>

using namespace std;

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

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



void Instructions()
{
	cout << "1 - Easy Game\n";
	cout << "2 - Quit Game\n";
}

void Draw()
{
	system("cls");
	cout <<"\tWelcome to Tic Tac Toe 2.0\n";

	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
	}
}

void Input()
{
	int a;
	cin >> a;

	if(a == 1) 
		if(board[0][0] == '1')
			board[0][0] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}

	else if(a == 2) 
		if(board[0][1] == '2')
			board[0][1] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
	else if(a == 3) 
		if(board[0][2] == '3')
			board[0][2] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
	else if(a == 4) 
		if(board[1][0] == '4')
			board[1][0] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
	else if(a == 5) 
		if(board[1][1] == '5')
			board[1][1] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 6) 
		if(board[1][2] == '6')
			board[1][2] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 7) 
		if(board[2][0] == '7')
			board[2][0] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 8) 
		if(board[2][1] == '8')
			board[2][1] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 9) 
		if(board[2][2] == '9')
		   board[2][2] = player;
		else
		{
			cout << "Field already taken, please try again." << endl <<endl;
			Input();
		}
}

int ComputerEasy()
{
	int i, j;
	bool found = false;
	while (found == false)
	{

		i = rand() % 3;
		j = rand() % 3;
		if(board[i][j] != player && board[i][j] != computer)
			{			
				return board[i][j] = computer;
				found = true;
			}
	}
}

char ToggleComputer()
{
	if(player == 'X')
		return computer = 'O';

	else if(player == 'O')
		return computer = 'X';
}

int CheckWin()
{
	
	if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
		return 'X';
	if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
		return 'X';
	if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
		return 'X';
	
	
	if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
		return 'X';
	if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
		return 'X';
	if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
		return 'X';

	//diagonals
	if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
		return 'X';
	if(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
		return 'X';
		
	if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
		return 'O';
	if(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
		return 'O';
	if(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
		return 'O';
	

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

	//diagonals
	if(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
		return 'O';
	if(board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
		return 'X';

	return '/';	
}

int main()
{
	
	cout << endl;
	Instructions();

	int choice;
	cin >> choice;

	Draw();

	while(choice == 1)
	{
		int moves = 0;
		moves++;
		Draw();
		Input();
		
		if (CheckWin() == 'X')
		{
			Draw();
			cout << "Player X won!";
			break;
		}
		else if(CheckWin() == 'O')
		{
			Draw();
			cout << "Player O won!";
			break;
		}
		else if (moves == 9)
		{
			Draw();
			cout <<" It's a draw!";
			break;
		}
		ComputerEasy();
		ToggleComputer();
	}

	system("pause");
}
Last edited on
bool user_goes_first;

from user input, set user_goes_first appropriately

line 209:
1
2
3
4
if (user_goes_first)
   Input();
else
   user_goes_first = true;


So the first time through, if the computer is supposed to go first, Input() is skipped and goes to the computer taking a turn.
Thank you for your answer, but still I have an issue as it keeps asking over and over again the question "Will you go first?". I don't know how to make it ask only the first time. Also the first move must always be an 'X'.

Here is my updated code and I will explain it a little more :

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 "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

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

char player = 'X';
char computer = 'O';
void Instructions()
{
	cout << "1 - Easy Game\n";
	cout << "2 - Quit Game\n";
}

void Draw()
{
	system("cls");
	cout <<"\tWelcome to Tic Tac Toe 2.0\n";

	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
	}
}

char askYesNo(string question)
{
	char response;
	do
	{
		cout << question << "y/n";
		cin >> response;
	} while (response != 'y' && response != 'n');

	return response;
}

void Input()
{
	int a;
	cin >> a;

	if(a == 1) 
		if(board[0][0] == '1')
			board[0][0] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}

	else if(a == 2) 
		if(board[0][1] == '2')
			board[0][1] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
	else if(a == 3) 
		if(board[0][2] == '3')
			board[0][2] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
	else if(a == 4) 
		if(board[1][0] == '4')
			board[1][0] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
	else if(a == 5) 
		if(board[1][1] == '5')
			board[1][1] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 6) 
		if(board[1][2] == '6')
			board[1][2] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 7) 
		if(board[2][0] == '7')
			board[2][0] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 8) 
		if(board[2][1] == '8')
			board[2][1] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 9) 
		if(board[2][2] == '9')
		   board[2][2] = player;
		else
		{
			cout << "Field already taken, please try again." << endl <<endl;
			Input();
		}
}

int ComputerEasy()
{
	int i, j;
	bool found = false;
	while (found == false)
	{

		i = rand() % 3;
		j = rand() % 3;
		if(board[i][j] != player && board[i][j] != computer)
			{			
				return board[i][j] = computer;
				found = true;
			}
	}
}

char ToggleComputer()
{
	if(player == 'X')
		return computer = 'O';

	else if(player == 'O')
		return computer = 'X';
}

char UserGoFirst()
{
	char go_first = askYesNo("Will you go first?");
	if (go_first == 'y')
	{
		cout << "Make your move.\n";
		return 'X';
	}
	if 	(go_first == 'n')
	{
		cout <<"I will go first.";
		return 'O';
	}
}

int CheckWin()
{
	
	if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
		return 'X';
	if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
		return 'X';
	if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
		return 'X';
	
	
	if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
		return 'X';
	if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
		return 'X';
	if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
		return 'X';

	//diagonals
	if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
		return 'X';
	if(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
		return 'X';
		
	if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
		return 'O';
	if(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
		return 'O';
	if(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
		return 'O';
	

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

	//diagonals
	if(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
		return 'O';
	if(board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
		return 'X';

	return '/';	
}

int main()
{
	
	cout << endl;
	Instructions();

	int choice;
	cin >> choice;

	Draw();

	while(choice == 1)
	{
		int moves = 0;
		moves++;
		Draw();
		if(UserGoFirst() == 'X')
		{
			Input();
				
		}
		else
		{
			UserGoFirst() == 'O';
		}
		
		Draw();
		if (CheckWin() == 'X')
		{
			Draw();
			cout << "Player X won!";
			break;
		}
		else if(CheckWin() == 'O')
		{
			Draw();
			cout << "Player O won!";
			break;
		}
		else if (moves == 9)
		{
			Draw();
			cout <<" It's a draw!";
			break;
		}
		ComputerEasy();
		ToggleComputer();
	}

	system("pause");
}


I will explain the following functions:

char askYesNo(string question) // asks a question and returns y on n

char UserGoFirst() //return 'X' if the user will go first and 'O if the computer will go first

Each iteration of your while loop is for one turn. Right now, you have UserGoFirst inside the while loop. So for every iteration (turn), there will be a prompt to ask which user should go first.

You likely want the UserGoFirst() call outside the while loop.
This is my updated code with the solution for who goes first computer, or player, but always the first one will be an 'X', I am not sure that my solution is a great one, but at least it works as for my previous tries that didn't do what I wanted.

My solution is:
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
char response;
	cout << "Do you want to go first? y/n";
	cin >> response;

	if(response == 'y')
	{
		goto user;
	}
	else
	{
		computer = 'X';
		player = 'O';
		goto computer;
	}
	

	while(choice == 1)
	{
		Draw();
		if (CheckWin() == 'X')
		{
			Draw();
			cout << "Player X won!";
			break;
		}
		else if(CheckWin() == 'O')
		{
			Draw();
			cout << "Player O won!";
			break;
		}
		else if (moves == 9)
		{
			Draw();
			cout <<" It's a draw!";
			break;
		}

	computer:
		moves++;
		ComputerEasy();
		Draw();

	user:
		Input();	
		ToggleComputer();

	}



But i still have an issue:

If the board is full that means there were made 9 moves, it won't annonce the winner or the draw (maybe because the game is waiting for another move, but another move will be impossible as the board is full)

Here is the full 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
#include <iostream>
#include <conio.h>
#include <string>
#include <ctime>
using namespace std;

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

char player = 'X';
char computer = 'O';
void Instructions()
{
	cout << "1 - Easy Game\n";
	cout << "2 - Quit Game\n";
}

void Draw()
{
	system("cls");
	cout <<"\tWelcome to Tic Tac Toe 2.0\n";

	for(int i=0; i<3; i++)
	{
		for(int j=0; j<3; j++)
		{
			cout << board[i][j] << " ";
		}
		cout << endl;
	}
}



void Input()
{
	int a;
	cin >> a;

	if(a == 1) 
		if(board[0][0] == '1')
			board[0][0] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}

	else if(a == 2) 
		if(board[0][1] == '2')
			board[0][1] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
	else if(a == 3) 
		if(board[0][2] == '3')
			board[0][2] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
	else if(a == 4) 
		if(board[1][0] == '4')
			board[1][0] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
	else if(a == 5) 
		if(board[1][1] == '5')
			board[1][1] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 6) 
		if(board[1][2] == '6')
			board[1][2] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 7) 
		if(board[2][0] == '7')
			board[2][0] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 8) 
		if(board[2][1] == '8')
			board[2][1] = player;
		else
		{
			cout <<"Field already taken, please try again." << endl <<endl;
			Input();
		}
		else if(a == 9) 
		if(board[2][2] == '9')
		   board[2][2] = player;
		else
		{
			cout << "Field already taken, please try again." << endl <<endl;
			Input();
		}
}

int ComputerEasy()
{
	srand(time(0));
	int i, j;
	bool found = false;
	while (found == false)
	{

		i = rand() % 3;
		j = rand() % 3;
		if(board[i][j] != player && board[i][j] != computer)
			{			
				return board[i][j] = computer;
				found = true;
			}
	}
}

char ToggleComputer()
{
	if(player == 'X')
		return computer = 'O';

	else if(player == 'O')
		return computer = 'X';
}



int CheckWin()
{
	
	if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
		return 'X';
	if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')
		return 'X';
	if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
		return 'X';
	
	
	if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')
		return 'X';
	if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')
		return 'X';
	if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')
		return 'X';

	//diagonals
	if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')
		return 'X';
	if(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')
		return 'X';
		
	if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
		return 'O';
	if(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
		return 'O';
	if(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
		return 'O';
	

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

	//diagonals
	if(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
		return 'O';
	if(board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
		return 'X';

	return '/';	
}

int main()
{
	int moves = 0;
	cout << endl;
	Instructions();

	int choice;
	cin >> choice;

	Draw();

	char response;
	cout << "Do you want to go first? y/n";
	cin >> response;

	if(response == 'y')
	{
		goto user;
	}
	else
	{
		computer = 'X';
		player = 'O';
		goto computer;
	}
	

	while(choice == 1)
	{
		Draw();
		if (CheckWin() == 'X')
		{
			Draw();
			cout << "Player X won!";
			break;
		}
		else if(CheckWin() == 'O')
		{
			Draw();
			cout << "Player O won!";
			break;
		}
		else if (moves == 9)
		{
			Draw();
			cout <<" It's a draw!";
			break;
		}

	computer:
		moves++;
		ComputerEasy();
		Draw();

	user:
		Input();	
		ToggleComputer();

	}
	getch();
}
Topic archived. No new replies allowed.