My Tic Tac Toe

I made a tic tac toe but at the end it keeps saying game is a tie and I cant fix it. Please help.

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>
using namespace std;

int main()
{
	char box1 = '1';
	char box2 = '2';
	char box3 = '3';
	char box4 = '4';
	char box5 = '5';
	char box6 = '6';
	char box7 = '7';
	char box8 = '8';
	char box9 = '9';
	char player1;
	char player2;
	char input1;
	char input2;
	bool again = false;
	int gameLoop = 0;
	bool wholeLoop = false;

	do {
		system("CLS");
		wholeLoop = false;
		gameLoop = 0;
		char box1 = '1';
	box2 = '2';
	box3 = '3';
	box4 = '4';
	box5 = '5';
	box6 = '6';
	box7 = '7';
	box8 = '8';
	box9 = '9';
	cout << "Tic Tac Toe By Notepad\n" << endl;
	do {	
		
		again = false;
		cout << "Player1 Choose O or X : ";
		cin >> input1;
	
		if(input1=='O')
		{
			input2 = 'X';
		}
		else if(input1 == 'X')
		{
			input2 = 'O';
		}
		else
		{
			again = true;
			system("CLS");
		}
	}while(again == true);
	
	cout << "\nPlayer 1 is " << input1 << "\nPlayer 2 is " << input2 << endl;
	cout << "\nGame will begin first with Player 1\n" << endl;
	system("pause");

	
	do {
	do {
	again = false;
	system("CLS");
	cout << box1 <<"|"<< box2 << "|" << box3 << endl;
	cout << "-+-+-" << endl;
	cout << box4 <<"|"<< box5 << "|" << box6 << endl;
	cout << "-+-+-" << endl;
	cout << box7 <<"|"<< box8 << "|" << box9 << endl;
	
	cout << "\nPlayer 1 : ";
	cin >> player1;
	if(!(player1=='1'||player1=='2'||player1=='3'||player1=='4'||player1=='5'||player1=='6'||player1=='7'||player1=='8'||player1=='9'))
	{
		again = true;
	}
	else if(player1=='1')
	{
		if(box1!=input2){
		box1 = input1;
		}else{again = true;}
	}
	else if(player1=='2')
	{
		if(box2!=input2){
		box2 = input1;
		}else{again = true;}
	}
	else if(player1=='3')
	{
		if(box3!=input2){
		box3 = input1;
		}else{again = true;}
	}
	else if(player1=='4')
	{
		if(box4!=input2){
		box4 = input1;
		}else{again = true;}
	}
	else if(player1=='5')
	{
		if(box5!=input2){
		box5 = input1;
		}else{again = true;}
	}
	else if(player1=='6')
	{
		if(box6!=input2){
		box6 = input1;
		}else{again = true;}
	}
	else if(player1=='7')
	{
		if(box7!=input2){
		box7 = input1;
		}else{again = true;}
	}
	else if(player1=='8')
	{
		if(box8!=input2){
		box8 = input1;
		}else{again = true;}
	}
	else if(player1=='9')
	{
		if(box9!=input2){
		box9 = input1;
		}else{again = true;}
	}
	}while(again==true);
	gameLoop += 1;

	do {
	again = false;
	system("CLS");
	cout << box1 <<"|"<< box2 << "|" << box3 << endl;
	cout << "-+-+-" << endl;
	cout << box4 <<"|"<< box5 << "|" << box6 << endl;
	cout << "-+-+-" << endl;
	cout << box7 <<"|"<< box8 << "|" << box9 << endl;
	
	cout << "\nPlayer 2 : ";
	cin >> player2;
	if(!(player2=='1'||player2=='2'||player2=='3'||player2=='4'||player2=='5'||player2=='6'||player2=='7'||player2=='8'||player2=='9'))
	{
		again = true;
	}
	else if(player2=='1')
	{
		if(box1!=input1){
		box1 = input2;
		}else{again = true;}
	}
	else if(player2=='2')
	{
		if(box2!=input1){
		box2 = input2;
		}else{again = true;}
	}
	else if(player2=='3')
	{
		if(box3!=input1){
		box3 = input2;
		}else{again = true;}
	}
	else if(player2=='4')
	{
		if(box4!=input1){
		box4 = input2;
		}else{again = true;}
	}
	else if(player2=='5')
	{
		if(box5!=input1){
		box5 = input2;
		}else{again = true;}
	}
	else if(player2=='6')
	{
		if(box6!=input1){
		box6 = input2;
		}else{again = true;}
	}
	else if(player2=='7')
	{
		if(box7!=input1){
		box7 = input2;
		}else{again = true;}
	}
	else if(player2=='8')
	{
		if(box8!=input1){
		box8 = input2;
		}else{again = true;}
	}
	else if(player2=='9')
	{
		if(box9!=input1){
		box9 = input2;
		}else{again = true;}
	}
	}while(again==true);
	gameLoop += 1;
	if(box1 == input1 && box2 == input1 && box3 == input1 || box4 == input1 && box5 == input1 && box6 == input1 || box7 == input1 && box8 == input1 && box9 == input1){
		cout << "Winner is Player1!\n" << endl;
		
	}
	else if(box1 == input1 && box4 == input1 && box7 == input1 || box2 == input1 && box5 == input1 && box8 == input1 || box3 == input1 && box6 == input1 && box9 == input1){
		cout << "\nWinner is Player1!\n" << endl;
		
	}
	else if(box1 == input1 && box5 == input1 && box9 == input1 || box3 == input1 && box5 == input1 && box7 == input1){
		cout << "\nWinner is Player1!\n" << endl;
		
	}
	else{
		cout << "\nGame is a tie\n" << endl;
	}
	if(box1 == input2 && box2 == input2 && box3 == input2 || box4 == input2 && box5 == input2 && box6 == input2 || box7 == input2 && box8 == input2 && box9 == input2){
		cout << "\nWinner is Player2!\n" << endl;
		
	}
	else if(box1 == input2 && box4 == input2 && box7 == input2 || box2 == input2 && box5 == input2 && box8 == input2 || box3 == input2 && box6 == input2 && box9 == input2){
		cout << "\nWinner is Player2!\n" << endl;
		
	}
	else if(box1 == input2 && box5 == input2 && box9 == input2 || box3 == input2 && box5 == input2 && box7 == input2){
		cout << "\nWinner is Player2!\n" << endl;
	}
	else{
		cout << "\nGame is a tie\n" << endl;
	}
	}while(gameLoop<8);
	
	
	do {
	again = false;
	cout << "PLay again? (y/n) :";
	cin >> input1;
	if(input1 == 'y'){
		wholeLoop = true;
	}
	else if(input1 == 'n'){
		system("CLS");
		cout << "Thanks for playing!" << endl;
		return 0;
	}
	else{
	again = true;
	}
	}while(again == true);
	}while(wholeLoop == true);
}
maybe the ifs arent set up correctly.
I usually stay away from else ifs, and just use regular ifs
This looks like a good program to learn how to use a debugger. Stepping through all the if statements should reveal your bug.

This seems alike a lot of code for a tic-tac-toe program. Once you have fixed your bug, I suggest looking up how others wrote the game.
You can cut this down quite a bit. I would also recommend using functions.
You should change this code so that a tie is not declared before you have checked whether player1 or player2 has won. Also, when checking player2 winning you should still use else if.

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
	if(box1 == input1 && box2 == input1 && box3 == input1 || box4 == input1 && box5 == input1 && box6 == input1 || box7 == input1 && box8 == input1 && box9 == input1){
		cout << "Winner is Player1!\n" << endl;
		
	}
	else if(box1 == input1 && box4 == input1 && box7 == input1 || box2 == input1 && box5 == input1 && box8 == input1 || box3 == input1 && box6 == input1 && box9 == input1){
		cout << "\nWinner is Player1!\n" << endl;
		
	}
	else if(box1 == input1 && box5 == input1 && box9 == input1 || box3 == input1 && box5 == input1 && box7 == input1){
		cout << "\nWinner is Player1!\n" << endl;
		
	}
	else{
		cout << "\nGame is a tie\n" << endl;// You declare a tie when maybe player 2 has won
	}
	if(box1 == input2 && box2 == input2 && box3 == input2 || box4 == input2 && box5 == input2 && box6 == input2 || box7 == input2 && box8 == input2 && box9 == input2){
		cout << "\nWinner is Player2!\n" << endl;
		
	}
	else if(box1 == input2 && box4 == input2 && box7 == input2 || box2 == input2 && box5 == input2 && box8 == input2 || box3 == input2 && box6 == input2 && box9 == input2){
		cout << "\nWinner is Player2!\n" << endl;
		
	}
	else if(box1 == input2 && box5 == input2 && box9 == input2 || box3 == input2 && box5 == input2 && box7 == input2){
		cout << "\nWinner is Player2!\n" << endl;
	}
	else{
		cout << "\nGame is a tie\n" << endl; // Because the check for player2 is in a separate 
// if branch, a tie can be declared after player1 has been declared a winner 
In my tic tac toe game I used the Visual part of Visual C++. It might be easier to use that.
Last edited on
If you're making a console application, I'd recommend using a 2D array. It makes it easy to draw the board and check adjoining spaces for win conditions.
Last edited on
Topic archived. No new replies allowed.