TicTacToe not ending the game?

Guys, I have no idea why my Tic-Tac-Toe game isn't ending when a winner is clearly established, or when it's a draw. I looked at my code several times and nothing stood out to me that could be causing this problem. Please help! 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
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
 //Tic Tac Toe game using arrays
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const int row = 3;
const int col = 3;

	string TicTacToe[row][col] = 
	{
		{"1 ", "2 ", "3 "},
		{"4 ", "5 ", "6 "},
		{"7 ", "8 ", "9 "}
	};

void PrintBoard(); //function prototype
void P1choice();
void P2choice();
bool checkWinner();

int main ()
{
	bool gameover = false;
	cout << "Welcome to Tic-Tac-Toe!" << endl;

	cout << "\nPlayer 1 you are going to be X..." << endl;
	cout << "Player 2 you are going to be O..." << endl;

	do
	{
	PrintBoard();
	P1choice();
	gameover = checkWinner();

	if (gameover == true)
		cout << "\nThanks for playing!" << endl;

	PrintBoard();
	P2choice();
	gameover = checkWinner();

	if (gameover == true)
		cout << "\nThanks for playing!" << endl;

	}

	while (gameover != true);

	system("pause");
	return 0;
}

void PrintBoard ()
{

	 cout << "______" << endl;
	for (int x = 0; x < row; x++)
	{
		for (int y = 0; y < col; y++)
		{
			cout << TicTacToe[x][y];
		}
		cout << endl;
	}
	cout << "______" << endl;
}

void P1choice ()
{
	int location;

	cout << "\nPlayer 1, please choose a location (1-9) for X." << endl;
	cin >> location;

	while (location < 1 || location > 9)
	{
		cout << "Player 1 Invalid. Choose a location (1-9) for X." << endl;
		cin.clear();
		cin.ignore();
		cin >> location;
	}

	if (location == 1)
		 TicTacToe[0][0] = "X ";
	else if (location == 2)
		TicTacToe[0][1] = "X ";
	else if (location == 3)
		TicTacToe[0][2] = "X ";
	else if (location == 4)
		TicTacToe[1][0] = "X ";
	else if (location == 5)
		TicTacToe[1][1] = "X ";
	else if (location == 6)
		TicTacToe[1][2] = "X ";
	else if (location == 7)
		TicTacToe[2][0] = "X ";
	else if (location == 8)
		TicTacToe[2][1] = "X ";
	else if (location == 9)
		TicTacToe[2][2] = "X ";

}

void P2choice ()
{
	int location;

	cout << "\nPlayer 2, please choose a location (1-9) for O." << endl;
	cin >> location;

	while (location < 1 || location > 9)
	{
		cout << "Player 2 Invalid. Choose a location (1-9) for O." << endl;
		cin.clear();
		cin.ignore();
		cin >> location;
	}

	if (location == 1)
		 TicTacToe[0][0] = "O ";
	else if (location == 2)
		TicTacToe[0][1] = "O ";
	else if (location == 3)
		TicTacToe[0][2] = "O ";
	else if (location == 4)
		TicTacToe[1][0] = "O ";
	else if (location == 5)
		TicTacToe[1][1] = "O ";
	else if (location == 6)
		TicTacToe[1][2] = "O ";
	else if (location == 7)
		TicTacToe[2][0] = "O ";
	else if (location == 8)
		TicTacToe[2][1] = "O ";
	else if (location == 9)
		TicTacToe[2][2] = "O ";

}

bool checkWinner()
{
	bool gameover = true;
	bool gameoverFalse = false;
	int counter = 0;

	if (TicTacToe[0][0] == "X" && TicTacToe[0][1] == "X" && TicTacToe[0][2] == "X") 
	{                                                                               
		cout << "Player 1 (X) wins!" << endl;                                       
		return gameover;
	}
	else if (TicTacToe[0][0] == "X" && TicTacToe[1][0] == "X" && TicTacToe[2][0] == "X") 
	{                                                                                    
		cout << "Player 1 (X) wins!" << endl;                                            
		return gameover;
	}
	else if (TicTacToe[0][0] == "X" && TicTacToe[1][1] == "X" && TicTacToe[2][2] == "X") 
	{																					 
		cout << "Player 1 (X) wins!" << endl;											 
		return gameover;
	}
	else if (TicTacToe[0][1] == "X" && TicTacToe[1][1] == "X" && TicTacToe[2][1] == "X") 
	{                                                                                                                                                                    
		cout << "Player 1 (X) wins!" << endl;                                           
		return gameover;
	}
	else if (TicTacToe[0][2] == "X" && TicTacToe[1][2] == "X" && TicTacToe[2][2] == "X") 
	{                                                                                                                                                               
		cout << "Player 1 (X) wins!" << endl;                                           
		return gameover;
	}
	else if (TicTacToe[1][0] == "X" && TicTacToe[1][1] == "X" && TicTacToe[1][2] == "X") 
	{																					 
		cout << "Player 1 (X) wins!" << endl;										     
		return gameover;
	}
	else if (TicTacToe[2][0] == "X" && TicTacToe[2][1] == "X" && TicTacToe[2][2] == "X") 
	{                                                                                                                                                         
		cout << "Player 1 (X) wins!" << endl;                                            
		return gameover;
	}
	

	else if (TicTacToe[0][0] == "O" && TicTacToe[0][1] == "O" && TicTacToe[0][2] == "O")
	{                                                                               
		cout << "Player 2 (O) wins!" << endl;                                       
		return gameover;
	}
	else if (TicTacToe[0][0] == "O" && TicTacToe[1][0] == "O" && TicTacToe[2][0] == "O") 
	{                                                                                    
		cout << "Player 2 (O) wins!" << endl;                                            
		return gameover;
	}
	else if (TicTacToe[0][0] == "O" && TicTacToe[1][1] == "O" && TicTacToe[2][2] == "O") 
	{																					 
		cout << "Player 2 (O) wins!" << endl;											 
		return gameover;
	}
	else if (TicTacToe[0][1] == "O" && TicTacToe[1][1] == "O" && TicTacToe[2][1] == "O") 
	{                                                                                                                                                                  
		cout << "Player 2 (O) wins!" << endl;                                            
		return gameover;
	}
	else if (TicTacToe[0][2] == "O" && TicTacToe[1][2] == "O" && TicTacToe[2][2] == "O") 
	{                                                                                                                                                                   
		cout << "Player 2 (O) wins!" << endl;                                            
		return gameover;
	}
	else if (TicTacToe[1][0] == "O" && TicTacToe[1][1] == "O" && TicTacToe[1][2] == "O") 
	{																					 
		cout << "Player 2 (O) wins!" << endl;										     
		return gameover;
	}
	else if (TicTacToe[2][0] == "O" && TicTacToe[2][1] == "O" && TicTacToe[2][2] == "O") 
	{                                                                                                                                                                   
		cout << "Player 2 (O) wins!" << endl;                                            
		return gameover;
	}
	counter += 1;

	if (counter == 9)
	{
		cout << "Draw! No winner!" << endl;
		return gameover;
	}
	else 
		return gameoverFalse;
}
Last edited on
The reason why checkWinner() never returns true is that you compare with "X" ("O" respectively) while the string contains "X ". Note the space after the X.

Further: There should be an else branch after line 37.
Topic archived. No new replies allowed.