Tic Tac Toe problem

Hello all, forgive me for including my entire program, but the problem
is very annoying and I can't figure out where it is. Essentially, after a winner or a tie, program will reach the return statement which should exit the class, but it doesn't. It jumps out of the drawBoard() member function and into the two_in_row() member function, where it goes through that and sometimes makes an additional move, changing the outcome of the game.

Part 1
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
//Implementaion file that includes all of the classes and their innards
//classImp.h

#include <iostream>
#include <cstdlib>		//for random number generator
#include <ctime>		//for seed (srand)
#include "dec.h"

#define LINES "\n\n\n\n\n\n\n\n\n\n\n\n"
#define TAB "   "
#define BIGTAB "\t\t\t" 
using namespace std;

void Draw::opponent(int &opp)
{
	cout << "Who do you want to play against?\nEnter the number corresponding to your choice.\n";
	cout << "1. Player\n";
	cout << "2. Computer\n";
	cin >> opp;
	
	//discards characters until new line is found
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	cout << endl;
	//if user did not enter a valid repsonse
	if (opp != 1 && opp != 2)
	{
		cout << "Please choose a valid choice.\n";
		opponent(opp);
	}
	player = 0;
	//playing against another player
	if (opp == 1)
		p_v_p(player1, player2);
	//playing against the computer
	else
		p_v_comp(player1);
}

void Draw::p_v_p(string &player1, string &player2)
{
	cout << "Player 1 name: ";
	getline(cin, player1);
	cout << "Player 2 name: ";
	getline(cin, player2);
	cout << endl;
	cout << "To choose a square, type in the corresponding number.\n";
}

void Draw::p_v_comp(string &player1)
{
	cout << "Player name: ";
	getline(cin, player1);
	cout << "To choose a square, type in the corresponding number.\n";
	srand(time(NULL));		//For the random number generator
}

void Draw::cells()
{
	board[0][0] = '1';
	board[0][1] = '2';
	board[0][2] = '3';
	board[1][0] = '4';
	board[1][1] = '5';
	board[1][2] = '6';
	board[2][0] = '7';
	board[2][1] = '8';
	board[2][2] = '9';	
}

//Draws the empty playing board
int Draw::drawBoard()
{
	cout << LINES;
	cout << BIGTAB << TAB << board[0][0] << " |" << TAB << board[0][1] << " |" << TAB << board[0][2] << "\n";
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << " " << "----+-----+----\n";
	cout << BIGTAB << TAB << board[1][0] << " |" << TAB << board[1][1] << " |" << TAB << board[1][2] << "\n";
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << " " << "----+-----+----\n";
	cout << BIGTAB << TAB << board[2][0] << " |" << TAB << board[2][1] << " |" << TAB << board[2][2] << "\n";
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	//next person's turn
	if (player == 0 || player == 2)
		player = 1;
	else
		player = 2;
	int win = testWinner();
	//if there is a winner or tie, exit the program
	if (win == 1)
	{
		cout << "RETURN\n";
		return 0;
	}
	if (win == 0)
	{
		//if playing against another player
		if (opp == 1)
			playerMoves(player1, player2, player, opp);
		//if playing against computer
		else
		{
			success = compMoves(player1, player);
			if (success == 1)
				return 0;
		}
	}
}

void Draw::playerMoves(string player1, string player2, int player, int opp)
{
	if (player == 0 || player == 1)
		cout << player1 << "'s turn: ";
	else
	{
		if (opp == 1)			
			cout << player2 << "'s turn: ";		
		//if playing against computer, move to compMoves
		else
			compMoves(player1, player);
	}
	string input;		//each player's move
	getline(cin, input);
	while (input != "1" && input != "2" && input != "3" && input != "4" && input != "5" &&
		   input != "6" && input != "7" && input != "8" && input != "9")
	{
		cout << "Error: Please enter a cell 1-9\n";
		cout << "Please choose again: ";
		getline(cin, input);
	}
	//makes the letter a character
	char move = input[0];
	//if the number is different, move on
	if (!isSame(move))
		newBoard(move, player);
	//if it is the same, print revert back to ask for another number
	else
		playerMoves(player1, player2, player, opp);
}

int Draw::compMoves(string player1, int player)
{
	opp = 2;
	//defaults to playerMoves
	if (player == 0 || player == 1)
		playerMoves(player1, player2, player, opp);
	else if (count == 9)
		return 1;
	else 
	{
		//if it's the computer's turn
		//if there are no possible winners
		if (!two_in_row())
		{
			char move = randomCell();
			newBoard(move, player);
		}
	}
}


Last edited on
Part 2
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
bool Draw::two_in_row()
{
	int moveTest = 0;			//tests to see if any move has been made
	char moveComp = 'a';		//the computers move
	//testing each cell for the opponent's piece
	for (int r = 0; r < 3; r++)
	{
		int tempR = r;
		for (int c = 0; c < 3; c++)
		{
			int tempC = c;
			//if the cell has 'X' test for adjacent cells
			if (board[r][c] == 'X')
			{
				//if the cell is on the edge, move to the other side
				if (r == 2)
				{
					tempR = 0;
					//MOVING IN THE MIDDLE OF THE COLUMN
					if (board[tempR][c] == 'X')
					{
						if (!isSame(tempR, c))
						{
							//assigns the new move to the array
							if (c == 0)
								moveComp = '4';
							if (c == 1)
								moveComp = '5';
							if (c == 2)
								moveComp = '6';
							//stores the new value into the array
							storeArr(moveComp);
							board[tempR + 1][c] = 'O'; //0 + 1 = middle cell
							tempArr[count] = moveComp;
							drawBoard();
							moveTest = 1;
						}
						else
							continue;
					}
				}
				else if (c == 2)
				{
					tempC = 0;
					//MOVING IN THE MIDDLE OF THE ROW
					if (board[r][tempC] == 'X')
					{
						if (!isSame(r, tempC))
						{
							if (r == 0)
								moveComp = '2';
							if (r == 1)
								moveComp = '5';
							if (r == 2)
								moveComp = '8';
							storeArr(moveComp);
							board[r][tempC + 1] = 'O';
							tempArr[count] = moveComp;
							drawBoard();
							moveTest = 1;
						}
						else
							continue;
					}
				}
				else
				{
					//if the next cell right has an 'X'
					if (board[r][c + 1] == 'X')
					{
						//MOVING TO THE LEFT SIDE OF THE ROW
						if ((c + 1) == 2)
						{
							tempC = 0;
							if (!isSame(r, tempC))
							{
								if (r == 0)
									moveComp = '1';
								if (r == 1)
									moveComp = '4';
								if (r == 2)
									moveComp = '7';
								storeArr(moveComp);
								board[r][tempC] = 'O';
								tempArr[count] = moveComp;
								drawBoard();
								moveTest = 1;
							}
							else
								continue;
						}
						//MOVING TO THE RIGHT SIDE OF THE ROW
						else
						{
							if (!isSame(r, c + 2))
							{
								if (r == 0)
									moveComp = '3';
								if (r == 1)
									moveComp = '6';
								if (r == 2)
									moveComp = '9';
								storeArr(moveComp);
								board[r][c + 2] = 'O';
								tempArr[count] = moveComp;
								drawBoard();
								moveTest = 1;
							}
							else
								continue;
						}						
					}
					//if the cell below has an 'X'
					if (board[r + 1][c] == 'X')
					{
						//MOVING TO THE TOP OF THE COLUMN
						if ((r + 1) == 2)
						{
							tempR = 0;
							if (!isSame(tempR, c))
							{
								if (c == 0)
									moveComp = '1';
								if (c == 1)
									moveComp = '2';
								if (c == 2)
									moveComp = '3';
								storeArr(moveComp);
								board[tempR][c] = 'O';
								tempArr[count] = moveComp;
								drawBoard();
								moveTest = 1;
							}
						}
						//MOVING TO THE BOTTOM OF THE COLUMN
						else
						{
							if (!isSame(r + 2, c))
							{
								if (c == 0)
									moveComp = '7';
								if (c == 1)
									moveComp = '8';
								if (c == 2)
									moveComp = '9';
								storeArr(moveComp);
								board[r + 2][c] = 'O';
								tempArr[count] = moveComp;
								drawBoard();
								moveTest = 1;
							}
							else
								continue;
						}
					}
				}
			}
		}
	}
	//MOVING IN THE CENTER CELL (testing the corners)
	if ((board[0][0] == 'X' && board[2][2] == 'X') || (board[0][2] == 'X' && board[2][0] == 'X'))
	{
		if (!isSame(1, 1))
		{
			moveComp = '5';
			storeArr(moveComp);
			board[1][1] = 'O';
			tempArr[count] = moveComp;
			drawBoard();
			moveTest = 1;
		}
	}
	//testing each diagonal combination
	if (board[0][0] == 'X' && board[1][1] == 'X')
	{
		if (!isSame(2, 2))
		{
			moveComp = '9';
			storeArr(moveComp);
			board[2][2] = 'O';
			tempArr[count] = moveComp;
			drawBoard();
			moveTest = 1;
		}
	}
	if (board[1][1] == 'X' && board[2][2] == 'X')
	{
		if (!isSame(0, 0))
		{
			moveComp = '1';
			storeArr(moveComp);
			board[0][0] = 'O';
			tempArr[count] = moveComp;
			drawBoard();
			moveTest = 1;
		}
	}
	if (board[2][0] == 'X' && board[1][1] == 'X')
	{
		if (!isSame(0, 2))
		{
			moveComp = '3';
			storeArr(moveComp);
			board[0][2] = 'O';
			tempArr[count] = moveComp;
			drawBoard();
			moveTest = 1;
		}
	}
	if (board[1][1] == 'X' && board[0][2] == 'X')
	{
		if (!isSame(2, 0))
		{
			moveComp = '7';
			storeArr(moveComp);
			board[2][0] = 'O';
			tempArr[count] = moveComp;
			drawBoard();
			moveTest = 1;
		}
	}
	//if there are no cells that are 2 in a row
	if (moveTest == 0)
		return false;
}
char Draw::randomCell()
{
	int random;
	//makes the seed time different every time it is run	
	random = 1 + (rand() % (57 - 48) + 48);		//picks a random number from 1-9
	char randomChar = static_cast<char>(random);	//converts random number to applicable char
	if (!isSame(randomChar))
		return randomChar;
	else
		randomCell();
}

bool Draw::isSame(int r, int c)
{
	if (board[r][c] == 'O')
		return true;
	else
		return false;
}

bool Draw::isSame(char move)
{
	
	for (int i = 0; i < count; i++)
	{
		storeArr(move);
		//tests if the current move is the same as previous ones
		if (tempArr[i] == move)
		{
			if (opp == 1 || (opp == 2 && player == 1))
			{
				cout << "YOU'RE A DUMBASS.\n";
				cout << "Enter another number.\n";
			}
			return true;
		}
	}
	return false;
}

void Draw::storeArr(char move)
{
	//stores the current move
	tempArr[count] = move;
}

//inserts and  'X' or 'O' into the correct cell
void Draw::newBoard(int move, int player)
{
	switch (move)
	{
	case '1':
		if (player == 1)
			board[0][0] = 'X';
		else
			board[0][0] = 'O';
		drawBoard();
		break;
	case '2':
		if (player == 1)
			board[0][1] = 'X';
		else
			board[0][1] = 'O';
		drawBoard();
		break;
	case '3':
		if (player == 1)
			board[0][2] = 'X';
		else
			board[0][2] = 'O';
		drawBoard();
		break;
	case '4':
		if (player == 1)
			board[1][0] = 'X';
		else
			board[1][0] = 'O';
		drawBoard();
		break;
	case '5':
		if (player == 1)
			board[1][1] = 'X';
		else
			board[1][1] = 'O';
		drawBoard();
		break;
	case '6':
		if (player == 1)
			board[1][2] = 'X';
		else
			board[1][2] = 'O';
		drawBoard();
		break;
	case '7':
		if (player == 1)
			board[2][0] = 'X';
		else
			board[2][0] = 'O';
		drawBoard();
		break;
	case '8':
		if (player == 1)
			board[2][1] = 'X';
		else
			board[2][1] = 'O';
		drawBoard();
		break;
	case '9':
		if (player == 1)
			board[2][2] = 'X';
		else
			board[2][2] = 'O';
		drawBoard();
		break;
	}
}
Last edited on
Part 3
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

//tests to see if there is a winner or not
int Draw::testWinner()
{
	//tests every possible winning combination
	//Player 1 wins
	if ((board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') ||
		(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') ||
		(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') ||
		(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') ||
		(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X') ||
		(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') ||
		(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') ||
		(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X'))
	{
		cout << player1 << " wins!!" << endl;
		return 1;
	}
	//Player 2 wins
	if ((board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') ||
		(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') || 
		(board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') ||
		(board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') ||
		(board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O') ||
		(board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O') ||
		(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') ||
		(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O'))
	{
		cout << player2 << " wins!!" << endl;
		return 1;
	}
	//if all cells have been entered with no winner
	if (count == 9)
	{
		cout << "It's a tie!!" << endl;
		return 1;
	}
	//if no winner
	else
	{
		count++;
		return 0;
	}
}
Topic archived. No new replies allowed.