Tic Tac Toe project

Me and my partner have to create a Tic Tac Toe game for our project. I was wondering if anyone could look at the code for me and help me figure out the errors? Any help is 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <conio.h>
using namespace std;

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

int win();
void Board();

int main() {
	
	char playerAnswer;
	char symbol;
	int player = 1;
	int i;
	int playerChoice;
	string playerName1;
	string playerName2;

	cout << "Would you like to play a game y/n?: ";
	cin >> playerAnswer;

	if (playerAnswer == 'y') {
		// Choosing 1 Player or 2 Player
		while (playerAnswer != 'q') {
			cout << "Enter Player 1 name: ";
			cin >> playerName1;
			cout << endl;

			// Two Player or AI
			cout << "If you would like to play with an AI, enter 'AI' in the Player 2 name.";
			cout << "\nEnter Player 2 name: ";
			cin >> playerName2;
			cout << endl;

			// AI
			if (playerName2 == "AI") {
				cout << "AI activated." << endl;
				cout << "Player 1: " << playerName1 << endl;
				cout << "Player 2: " << playerName2 << endl;
			}

			else {
				cout << "Player 1: " << playerName1 << endl;
				cout << "Player 2: " << playerName2 << endl;
			}

			// Quitting the game // Can occur at any time
	else if (playerAnswer == 'q') {
		cout << "Goodbye." << endl;
	}
		}

		Board();

		player = (player % 2) ? 1 : 2;

		cout << "Player " << player << " , make your move: ";
		cin >> playerChoice;

		symbol = (player % 2) ? 'X' : 'O';

		// Choosing Squares
		if (playerChoice == 1 && square[1] == '1')
			square[1] = symbol;

		else if (playerChoice == 2 && square[2] == '2')
			square[2] = symbol;

		else if (playerChoice == 3 && square[3] == '3')
			square[3] = symbol;

		else if (playerChoice == 4 && square[4] == '4')
			square[4] = symbol;

		else if (playerChoice == 5 && square[5] == '5')
			square[5] = symbol;

		else if (playerChoice == 6 && square[6] == '6')
			square[6] = symbol;

		else if (playerChoice == 7 && square[7] == '7')
			square[7] = symbol;

		else if (playerChoice == 8 && square[8] == '8')
			square[8] = symbol;

		else if (playerChoice == 9 && square[9] == '9')
			square[9] = symbol;

		// Invalid
		else {
			cout << "\aError: Invalid Move";

			player--;
			cin.ignore();
			cin.get();
		}
		i = win();

		//Swapping the Players


		player++;
	}

	while (i == -1);
	Board();


		if (i == 1) 
			cout << "== Player " << --player << " wins!";
		
		else 
			cout << "== Game Over! Draw!";
			cin.ignore();
			cin.get();
			return 0;
		




		//Invalid Answer Given
	else  {
		cout << "\aInvalid Response. ";
		cout << "Program Terminated" << endl;
		return 0;
	}
}

	//Game Board
	void Board() {
		system("cls");
		cout << "\n\n\tTic Tac Toe\n\n";

		cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
		cout << endl;

		cout << "     |     |     " << endl;
		cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;

		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;

		cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;

		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;

		cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;

		cout << "     |     |     " << endl << endl;
	}

	int win() {
		// Player Win
		// Horizontal
		if (square[1] == square[2] && square[2] == square[3])
			return 1;

		else if (square[4] == square[5] && square[5] == square[6])
			return 1;

		else if (square[7] == square[8] && square[8] == square[9])
			return 1;

		// Vertical
		else if (square[1] == square[4] && square[4] == square[7])
			return 1;


		else if (square[2] == square[5] && square[5] == square[8])
			return 1;

		else if (square[3] == square[6] && square[6] == square[9])
			return 1;


		// Diagonal
		else if (square[1] == square[5] && square[5] == square[9])
			return 1;

		else if (square[3] == square[5] && square[5] == square[7])
			return 1;

		else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
			&& square[4] != '4' && square[5] != '5' && square[6] != '6'
			&& square[7] != '7' && square[8] != '8' && square[9] != '9')
			return 0;
		else
			return -1;
	}
Last edited on
What errors - compiler error, crash, gibberish output?
Well in Visual Studio, It says that the
1
2
3
else if (playerAnswer == 'q') {
		cout << "Goodbye." << endl;
	}

is expected as a statement?
and
1
2
3
4
5
else  {
		cout << "\aInvalid Response. ";
		cout << "Program Terminated" << endl;
		return 0;
	}

is expected as one also.
This is causing the program not to compile, I tried taking these out, which just caused it to repeat entering player names.
Check the brackets { }. For example, that else if (playerAnswer == 'q') on line 53 is aligned with the if (playerAnswer == 'y') on line 27, but there's no closing } for the while loop started on line 29 before the else if. So the else if is in the while and the compiler can't match it to a preceding if.
Okay, so I fixed all the braces, and I also made it so there were no errors, yet when I compile it just repeats the asking for the player names, here is the 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <conio.h>
using namespace std;

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

int win();
void Board();

int main() {
	
	char playerAnswer;
	char symbol;
	int player = 1;
	int i;
	int playerChoice;
	string playerName1;
	string playerName2;

	cout << "Would you like to play a game y/n?: ";
	cin >> playerAnswer;

	if (playerAnswer == 'y') {
		// Choosing 1 Player or 2 Player
		while (playerAnswer != 'q') {
			cout << "Enter Player 1 name: ";
			cin >> playerName1;
			cout << endl;

			// Two Player or AI
			cout << "If you would like to play with an AI, enter 'AI' in the Player 2 name.";
			cout << "\nEnter Player 2 name: ";
			cin >> playerName2;
			cout << endl;

			// AI
			if (playerName2 == "AI") {
				cout << "AI activated." << endl;
				cout << "Player 1: " << playerName1 << endl;
				cout << "Player 2: " << playerName2 << endl;
			}

			else {
				cout << "Player 1: " << playerName1 << endl;
				cout << "Player 2: " << playerName2 << endl;
			}
		}
	}

			// Quitting the game // Can occur at any time
	else if (playerAnswer == 'q') {
		cout << "Goodbye." << endl;
	}
		

		Board();

		player = (player % 2) ? 1 : 2;

		cout << "Player " << player << " , make your move: ";
		cin >> playerChoice;

		symbol = (player % 2) ? 'X' : 'O';

		// Choosing Squares
		if (playerChoice == 1 && square[1] == '1') {
			square[1] = symbol;
		}

		else if (playerChoice == 2 && square[2] == '2') {
			square[2] = symbol;
		}
		else if (playerChoice == 3 && square[3] == '3') {
			square[3] = symbol;
		}
		else if (playerChoice == 4 && square[4] == '4') {
			square[4] = symbol;
		}
		else if (playerChoice == 5 && square[5] == '5') {
			square[5] = symbol;
		}
		else if (playerChoice == 6 && square[6] == '6') {
			square[6] = symbol;
		}
		else if (playerChoice == 7 && square[7] == '7') {
			square[7] = symbol;
		}
		else if (playerChoice == 8 && square[8] == '8') {
			square[8] = symbol;
		}
		else if (playerChoice == 9 && square[9] == '9') {
			square[9] = symbol;
		}
		// Invalid
		else {
			cout << "\aError: Invalid Move";

			player--;
			cin.ignore();
			cin.get();
		}
		i = win();

		//Swapping the Players


		player++;
	

	while (i == -1); {
		Board();


		if (i == 1) {
			cout << "== Player " << --player << " wins!";
		}
		else if (i==0){
			cout << "== Game Over! Draw!";
			cin.ignore();
			cin.get();
			return 0;
		}






		//Invalid Answer Given
		else {
			cout << "\aInvalid Response. ";
			cout << "Program Terminated" << endl;
			return 0;
		}
	}
}

	//Game Board
	void Board() {
		system("cls");
		cout << "\n\n\tTic Tac Toe\n\n";

		cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
		cout << endl;

		cout << "     |     |     " << endl;
		cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;

		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;

		cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;

		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;

		cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;

		cout << "     |     |     " << endl << endl;
	}

	int win() {
		// Player Win
		// Horizontal
		if (square[1] == square[2] && square[2] == square[3]) {
			return 1;
		}
		else if (square[4] == square[5] && square[5] == square[6])
			return 1;

		else if (square[7] == square[8] && square[8] == square[9])
			return 1;

		// Vertical
		else if (square[1] == square[4] && square[4] == square[7])
			return 1;


		else if (square[2] == square[5] && square[5] == square[8])
			return 1;

		else if (square[3] == square[6] && square[6] == square[9]) {
			return 1;
		}

		// Diagonal
		else if (square[1] == square[5] && square[5] == square[9]) {
			return 1;
		}
		else if (square[3] == square[5] && square[5] == square[7]) {
			return 1;
		}
		else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
			&& square[4] != '4' && square[5] != '5' && square[6] != '6'
			&& square[7] != '7' && square[8] != '8' && square[9] != '9'){
			return 0;
	}
		else {
			return -1;
		}
	}
while (playerAnswer != 'q') This will always be true so you have an endless loop.

Another problem:
1
2
3
4
else if (playerAnswer == 'q') 
{
  cout << "Goodbye." << endl;
}

You actually need to end your program
It seems like that while (playerAnswer != 'q') could be pulled outside the if statement to control the overall game.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while user wants to play a game
    get player names
    while no one has won and the board is not full
        display the board
        decide whose turn it is
        get player move
        if player move valid 
            mark square with player symbol
        otherwise
            invalid move message
    if someone won
        display winner message
    otherwise (draw)
        display draw message
    ask if user wants to play another game
Last edited on
One last question (I think) I fixed what you said, and the game works for 2 player, but it does not work for our simple AI, and after a game is finished it just goes back to asking for player name. Here is the 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <conio.h>
using namespace std;

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

int win();
void Board();

int main() {
	
	char playerAnswer;
	char symbol;
	int player = 1;
	int i;
	int playerChoice;
	string playerName1;
	string playerName2;

	cout << "Would you like to play a game y/n?: ";
	cin >> playerAnswer;
	while (playerAnswer != 'n') {

		if (playerAnswer == 'y') {
			// Choosing 1 Player or 2 Player

			cout << "Enter Player 1 name: ";
			cin >> playerName1;
			cout << endl;

			// Two Player or AI
			cout << "If you would like to play with an AI, enter 'AI' in the Player 2 name.";
			cout << "\nEnter Player 2 name: ";
			cin >> playerName2;
			cout << endl;

			// AI
			if (playerName2 == "AI") {
				cout << "AI activated." << endl;
				cout << "Player 1: " << playerName1 << endl;
				cout << "Player 2: " << playerName2 << endl;
			}

			else {
				cout << "Player 1: " << playerName1 << endl;
				cout << "Player 2: " << playerName2 << endl;
			}

		}

		do
		{


			Board();

			player = (player % 2) ? 1 : 2;

			cout << "Player " << player << " , make your move: ";
			cin >> playerChoice;

			symbol = (player == 1) ? 'X' : 'O';

			// Choosing Squares
			if (playerChoice == 1 && square[1] == '1') {
				square[1] = symbol;
			}

			else if (playerChoice == 2 && square[2] == '2') {
				square[2] = symbol;
			}
			else if (playerChoice == 3 && square[3] == '3') {
				square[3] = symbol;
			}
			else if (playerChoice == 4 && square[4] == '4') {
				square[4] = symbol;
			}
			else if (playerChoice == 5 && square[5] == '5') {
				square[5] = symbol;
			}
			else if (playerChoice == 6 && square[6] == '6') {
				square[6] = symbol;
			}
			else if (playerChoice == 7 && square[7] == '7') {
				square[7] = symbol;
			}
			else if (playerChoice == 8 && square[8] == '8') {
				square[8] = symbol;
			}
			else if (playerChoice == 9 && square[9] == '9') {
				square[9] = symbol;
			}
			// Invalid
			else {
				cout << "\aError: Invalid Move";

				player--;
				cin.ignore();
				cin.get();
			}
			i = win();

			//Swapping the Players


			player++;

		}
		while (i == -1); {
			Board();


			if (i == 1) {
				cout << "== Player " << --player << " wins!";
			}
			else if (i == 0) {
				cout << "== Game Over! Draw!";
				cin.ignore();
				cin.get();
				return 0;
			}






			//Invalid Answer Given
			else {
				cout << "\aInvalid Response. ";
				cout << "Program Terminated" << endl;
				return 0;
			}
		}
	}
}

	//Game Board
	void Board() {
		system("cls");
		cout << "\n\n\tTic Tac Toe\n\n";

		cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
		cout << endl;

		cout << "     |     |     " << endl;
		cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;

		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;

		cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;

		cout << "_____|_____|_____" << endl;
		cout << "     |     |     " << endl;

		cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;

		cout << "     |     |     " << endl << endl;
	}

	int win() {
		// Player Win
		// Horizontal
		if (square[1] == square[2] && square[2] == square[3]) {
			return 1;
		}
		else if (square[4] == square[5] && square[5] == square[6])
			return 1;

		else if (square[7] == square[8] && square[8] == square[9])
			return 1;

		// Vertical
		else if (square[1] == square[4] && square[4] == square[7])
			return 1;


		else if (square[2] == square[5] && square[5] == square[8])
			return 1;

		else if (square[3] == square[6] && square[6] == square[9]) {
			return 1;
		}

		// Diagonal
		else if (square[1] == square[5] && square[5] == square[9]) {
			return 1;
		}
		else if (square[3] == square[5] && square[5] == square[7]) {
			return 1;
		}
		else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
			&& square[4] != '4' && square[5] != '5' && square[6] != '6'
			&& square[7] != '7' && square[8] != '8' && square[9] != '9'){
			return 0;
	}
		else {
			return -1;
		}
	}
From what I see, if the player enters "AI" for the second player, there's a message confirming that at line 43. But the code in the loop still asks each player to make a move. If they've chosen "AI" as the second player, are you supposed to have the computer analyze the choices on the board and select an empty spot?

At the end of the game, you can ask the player if they want to play again, so if they enter 'n', the loop won't restart and ask for player names.
The simple AI is supposed to pick a empty spot and enter an "o" into it, then it's supposed to go back to player one, and okay I'll try that.
closed account (48T7M4Gy)
An improvement to the AI without too much trouble would be for the AI to check if there is a winning space to occupy AND win the game with that move.
Topic archived. No new replies allowed.