Animation and Warning not error for value return question

I know its long, but I wanted you to see where the functions are being called and how. I get a warning that says not all control paths return a value for check columns and rows. That is the point though, it is checking to see if they are blank, if so the computer can place his o there, if not he cannot--return null. I want to know if I can fix this though. it is just a warning.

Also, how do I go about just keeping a single board and the moves being placed there each time. I have researched system (pause) and clear screen, but I am not sure how to exactly use that in my code, so that I don't mess it up. This program has taken me two weeks to build. Thanks.

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
#include <iostream>
#include<string>

using namespace std;

int board[3][3]; //initial matrix declaration

int row, column, diagonal;

int checkrows(int c);
int checkcolumns(int r);

bool cpu_checkingfordefensivemove(int brd[3][3]);
bool cpu_checkingforwinningmove(int brd[3][3]);
bool checkforadraw(int brd[3][3]);

char checkforwinner(void); // declaration of functions
void createboard(void);
void humanplayermove(void);
void computermove(void);
void displayboard(void);

void newgameboard();


int main(void)
{
	char finished; char c;
	bool keep_playing = true;
	
	cout << "Shall We Play A Game Today? \n Yes or No.  (Y,N)" << endl;
	string p;
	cin >> p;

	if (p == "Y" || p == "y")
	{


		while (keep_playing)
		{

			cout << "Welcome To Tic Tac Toe \n\n" << endl;
			cout << "The Human Player will be the X, during this game \n\n" << endl;
			cout << "Here is how to play the game: \n\n 1) You must first enter which row you would like--from top to bottom.                i.e. 1,2,3 and then press enter \n 2) Enter which column you would like, and press enter again. \n      Good luck player \n\n" << endl;

			finished = ' ';
			createboard();

			do {
				displayboard();
				humanplayermove();
				finished = checkforwinner(); /* check winner */
				if (finished != ' ') break; /* if winner found...*/
				computermove();
				finished = checkforwinner(); /* check for winner again */
				if (finished != ' ') break; /* if winner found...*/
				if (checkforadraw(board)) //we don't have a winner and there are no open spaces.
				{
					displayboard();
					cout << endl;
					cout << "draw\n";
					break;
				}
				newgameboard();

			} while (finished == ' ');


			if (finished == 'X')
				cout << "You Have Defeated Me, this is impossible!!!! Let us Play Again....\n";
			else
				cout << "I have defeated you, I always said Machines Would become better than Humans at their own games. HAHAHA" << endl;
			displayboard(); /* show final positions */
			cout << "Would You like to go for another round? Y/N" << endl; cin >> c;
			if (c == 'N' || c == 'n')
				keep_playing = false;
			newgameboard();
		};

	}
	else  cout << "Maybe Next Time You will be brave enough to face me."  << endl;

	return 0;
}
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

void createboard(void) //matrix intitialisation
{
	int i, j;

	for (i = 0; i<3; i++)
		for (j = 0; j<3; j++) board[i][j] = ' ';
}


void humanplayermove(void) //call function for player input
{

	int x, y;

	cout << "Enter X,Y coordinates for your move: ";
	cin >> x >> y;

	x--; y--;

	if (board[x][y] != ' '){
		cout << "Invalid move, try again.\n";
		humanplayermove();
	}
	else board[x][y] = 'X';
}

void computermove(void) //AI move input
{
	char temp;
	int test[3][3], temporary[3][3];
	for (int i = 0; i < 3; ++i)
		for (int j = 0; j < 3; ++j)
		{
		test[i][j] = board[i][j]; temporary[i][j] = board[i][j];
		}
	int i, j;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
			if (board[i][j] == ' ')
			{
			temp = board[i][j];
			if (cpu_checkingforwinningmove(test))
			{
				if (row > 0)
				{
					if (board[row - 1][checkcolumns(row - 1)] != 'X')
						board[row - 1][checkcolumns(row - 1)] = 'O';
					return;
				}
				if (column > 0)
				{
					if (board[checkrows(column - 1)][column - 1] != 'X')
						board[checkrows(column - 1)][column - 1] = 'O';
					return;
				}
				if (diagonal == 1)
				{
					if (board[0][0] != 'X' && board[1][1] != 'X' && board[2][2] != 'X')
					{
						board[0][0] = 'O'; board[1][1] = 'O'; board[2][2] = 'O';
					}
					return;
				}
				else
				{
					if (board[0][2] != 'X' && board[1][1] != 'X' && board[2][0] != 'X')
					{
						board[0][2] = 'O'; board[1][1] = 'O'; board[2][0] = 'O';
					}
					return;
				}
				return;
				//break;
			}
			test[i][j] = temp;
			}
		if (board[i][j] == ' ')
		{
			temp = board[i][j];
			if (cpu_checkingforwinningmove(test))
			{
				if (row > 0)
				{
					if (board[row - 1][checkcolumns(row - 1)] != 'X')
						board[row - 1][checkcolumns(row - 1)] = 'O';
					return;
				}
				if (column > 0)
				{
					if (board[checkrows(column - 1)][column - 1] != 'X')
						board[checkrows(column - 1)][column - 1] = 'O';
					return;
				}
				if (diagonal == 1)
				{
					if (board[0][0] != 'X' && board[1][1] != 'X' && board[2][2] != 'X')
					{
						board[0][0] = 'O'; board[1][1] = 'O'; board[2][2] = 'O';
					}
					return;
				}
				else
				{
					if (board[0][2] != 'X' && board[1][1] != 'X' && board[2][0] != 'X')
					{
						board[0][2] = 'O'; board[1][1] = 'O'; board[2][0] = 'O';
					}
					return;
				}
				return;
			}
			test[i][j] = temp;
		}
	}
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
			if (board[i][j] == ' ')
			{
			temp = board[i][j];
			test[i][j] = 'O';
			if (!cpu_checkingfordefensivemove(test))
				break;
			test[i][j] = temp;
			}
		if (board[i][j] == ' ')
		{
			temp = board[i][j];
			test[i][j] = 'O';
			if (!cpu_checkingfordefensivemove(test))
				break;
			test[i][j] = temp;
		}
	}
	if (column > 0)
	{
		if (board[checkrows(column - 1)][column - 1] != 'X')
			board[checkrows(column - 1)][column - 1] = 'O';
		return;
	}
	if (row > 0)
	{
		if (board[row - 1][checkcolumns(row - 1)] != 'X')
			board[row - 1][checkcolumns(row - 1)] = 'O';
		return;
	}
	if (diagonal == 1)
	{
		if (board[0][0] != 'X')
			board[0][0] = 'O';
		if (board[1][1] != 'X')
			board[1][1] = 'O';
		if (board[2][2] != 'X')
			board[2][2] = 'O';
		return;
	}
	if (diagonal == 2)
	{
		if (board[0][2] != 'X')
			board[0][2] = 'O';
		if (board[1][1] != 'X')
			board[1][1] = 'O';
		if (board[2][0] != 'X')
			board[2][0] = 'O';
		return;
	}
	if (board[i][j] != 'X')
		board[i][j] = 'O';
}

void displayboard(void) //matrix display
{
	int t;

	for (t = 0; t<3; t++)
	{

		cout << " " << char(board[t][0]) << " | " << char(board[t][1]) << " | " << char(board[t][2]);

		if (t != 2)
			cout << "\n---|---|---\n";
	}
	cout << endl;
}

char checkforwinner(void) //used for identifying winner
{
	int i;

	for (i = 0; i<3; i++)  /* check rows */
		if (board[i][0] == board[i][1] &&
			board[i][0] == board[i][2]) return board[i][0];

	for (i = 0; i<3; i++)  /* check columns */
		if (board[0][i] == board[1][i] &&
			board[0][i] == board[2][i]) return board[0][i];

	/* test diagonals */
	if (board[0][0] == board[1][1] &&
		board[1][1] == board[2][2])
		return board[0][0];

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

	return ' ';
}

bool cpu_checkingfordefensivemove(int brd[3][3])
{
	char a, b, c, d, e, f, g, h, i;
	a = brd[0][0], b = brd[0][1], c = brd[0][2];
	d = brd[1][0], e = brd[1][1], f = brd[1][2];
	g = brd[2][0], h = brd[2][1], i = brd[2][2];
	if ((a == 'X' && b == 'X' && c != 'O') || (b == 'X' && c == 'X' && a != 'O') || (a == 'X' && c == 'X' && b != 'O'))
	{
		row = 1; return true;
	}
	if ((d == 'X' && e == 'X' && f != 'O') || (e == 'X' && f == 'X' && d != 'O') || (d == 'X' && f == 'X' && e != 'O'))
	{
		row = 2; return true;
	}
	if ((g == 'X' && h == 'X' && i != 'O') || (h == 'X' && i == 'X' && g != 'O') || (g == 'X' && i == 'X' && h != 'O'))
	{
		row = 3; return true;
	}
	if ((a == 'X' && d == 'X' && g != 'O') || (d == 'X' && g == 'X' && a != 'O') || (a == 'X' && g == 'X' && d != 'O'))
	{
		column = 1; return true;
	}
	if ((b == 'X' && e == 'X' && h != 'O') || (e == 'X' && h == 'X' && b != 'O') || (b == 'X' && h == 'X' && e != 'O'))
	{
		column = 2; return true;
	}
	if ((c == 'X' && f == 'X' && i != 'O') || (f == 'X' && i == 'X' && c != 'O') || (c == 'X' && i == 'X' && f != 'O'))
	{
		column = 3; return true;
	}
	if ((a == 'X' && e == 'X' && i != 'O') || (e == 'X' && i == 'X' && a != 'O') || (a == 'X' && i == 'X' && e != 'O'))
	{
		diagonal = 1; return true;
	}
	if ((g == 'X' && e == 'X' && c != 'O') || (e == 'X' && c == 'X' && g != 'O') || (g == 'X' && c == 'X' && e != 'O'))
	{
		diagonal = 2; return true;
	}
	return false;
}

bool cpu_checkingforwinningmove(int mat[3][3])
{
	char a, b, c, d, e, f, g, h, i;
	a = mat[0][0], b = mat[0][1], c = mat[0][2];
	d = mat[1][0], e = mat[1][1], f = mat[1][2];
	g = mat[2][0], h = mat[2][1], i = mat[2][2];
	if ((a == 'O' && b == 'O' && c != 'X') || (b == 'O' && c == 'O' && a != 'X') || (a == 'O' && c == 'O' && b != 'X'))
	{
		row = 1;
		return true;
	}
	if ((d == 'O' && e == 'O' && f != 'X') || (e == 'O' && f == 'O' && d != 'X') || (d == 'O' && f == 'O' && e != 'X'))
	{
		row = 2;
		return true;
	}
	if ((g == 'O' && h == 'O' && i != 'X') || (h == 'O' && i == 'O' && g != 'X') || (g == 'O' && i == 'O' && h != 'X'))
	{
		row = 3;
		return true;
	}
	if ((a == 'O' && d == 'O' && g != 'X') || (d == 'O' && g == 'O' && a != 'X') || (a == 'O' && g == 'O' && d != 'X'))
	{
		column = 1;
		return true;
	}
	if ((b == 'O' && e == 'O' && h != 'X') || (e == 'O' && h == 'O' && b != 'X') || (b == 'O' && h == 'O' && e != 'X'))
	{
		column = 2;
		return true;
	}
	if ((c == 'O' && f == 'O' && i != 'X') || (f == 'O' && i == 'O' && c != 'X') || (c == 'O' && i == 'O' && f != 'X'))
	{
		column = 3;
		return true;
	}
	if ((a == 'O' && e == 'O' && i != 'X') || (e == 'O' && i == 'O' && a != 'X') || (a == 'O' && i == 'O' && e != 'X'))
	{
		diagonal = 1;
		return true;
	}
	if ((g == 'O' && e == 'O' && c != 'X') || (e == 'O' && c == 'O' && g != 'X') || (g == 'O' && c == 'O' && e != 'X'))
	{
		diagonal = 2;
		return true;
	}
	return false;


}

bool checkforadraw(int brd[3][3])
{
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			if (brd[i][j] == ' ')
				return false;
		}
	}
	return true;
}

int checkcolumns(int r)
{
	for (int i = 0; i < 3; i++)
	{
		if (board[r][i] == ' ')
			return i;
	}
}

int checkrows(int c)
{
	for (int i = 0; i < 3; i++)
	{
		if (board[i][c] == ' ')
			return i;
	}
}

void newgameboard()
{
	row = 0;
	column = 0;
	diagonal = 0;
}
it is just a warning
It is a pretty dangerous warning. Only reason why it is not an error is because programmer can write so complex system that compiler cannot properly analyse it.
If your function ends without returning value, you have undefined behavior. Most commongly seen as crash or incorrect behavior.
So if your line/column does not have empty space, you have broken program.

Another one:
if (board[i][j] == ' ') As it is inside outer loop, but after inner one, you will access elements board[0][3], board[1][3] and board[2][3]. Same with test[i][j] = temp;.
Last edited on
Thank you so much. I went a head and changed the coding to

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int checkrows(int c)
{
	for (int i = 0; i < 3; i++)
	{
		if (board[i][c] == ' ')
			return i;
	} return 0;
}
int checkcolumns(int r)
{
	for (int i = 0; i < 3; i++)
	{
		if (board[r][i] == ' ')
			return i;
	} return 0;
} 

now the error went away. thank you again.

By chance, do you know how to make the function clear the screen so I only show one board the whole time, but keep all the moves. I am a little lost on where to put system("cls") in my programming to make the animation happen.
What do you mean by "keep all the moves"? As soon you do cls, you will have a black screen. If you want something on it, you will need to output it again.
(actually using cls is frowned upon. Best approach would be using WinAPI to controle console directly, but it is complex topic)
What I mean by that is, I want all the moves to continue to be shown, but only want one board showing. I believe I have to put a clear screen in my computer/human move function somewhere. I am continuing to play around with it but now have created more problems. I just trying to have the board stay on the screen throughout everything, instead of reprinting but I am confused about where to put the system clear screen. If I put it in the human move function, then when the game ends in a tie all the messages show to continue a game, which I definitely do not want. I am lost on this part of it.

Topic archived. No new replies allowed.