Tic Tac Toe Not Working

Good morning all. I have manually copied the below code from the book 'Beginning C++ Through Game Programming' as i could not seem to find it online using the link given. There seems to be a few errors in the code which i am unable to fix, my goal was to write up the code, run it and see what the output looks like then go through the code and figure out what everything does and how it's been put together. Would anyone be willing to assist in rectifying these errors so i can get the code to run.

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

using namespace std;

const char x = 'x';
const char o = 'o';
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';

// Function prototypes

void instructions();
char ask_yes_no (string question);
int ask_number(string question, int high, int low = 0);
char human_piece();
char opponent(char piece);
void display_board(const vector<char>& board);
char winner(const vector<char>& board);
bool is_legal(const vector<char>& board, int move);
int human_move(const vector<char>& board, char human);
int computer_move(vector<char> board, char computer);
void announce_winner(char winner, char computer, char human);

int main()

{

	int move;
	const int NUM_SQUARES = 9;
	vector <char> board(NUM_SQUARES, EMPTY);

	instructions();
	char human = human_piece();
	char computer = opponent(human);
	char turn = x;

	display_board(board);

	while (winner(board) == NO_ONE)
	{
		if (turn == human)
		{
			move = human_move(board, human);
			board[move] = human;
		}
		else
		{
			move = computer_move(board, human);
			board[move] = human
				;
		}
		display_board(board);
		turn = opponent(turn);
	}

	announce_winner(winner(board), computer, human);
	
	return 0;

}

void instructions()
{
	cout << "Welcome to TIC TAC TOE" << endl << endl;
	cout << "Make your move by entering a number between 0 - 8" << endl << endl;
	cout << "The number corresponds to the desired board position, as illustrated" << endl << endl;
	cout << "0 | 1 | 2" << endl;
	cout << "........." << endl;
	cout << "3 | 4 | 5" << endl;
	cout << "........." << endl;
	cout << "6 | 7 | 8" << endl << endl;
	cout << "Prepare yourself, the game is on!" << endl << endl;
}

char ask_yes_no(string question)
{
	char response;
	do
	{
		cout << question << " (y/n) ";
		cin >> response;
	} while (response != 'y' && response != 'n');
	return response;
}

int ask_number(string question, int high, int low)
{
	int number;
	do
	{
		cout << question << "( " << low << " - " << high << "): ";
		cin >> number;
	} while (number > high || number < low);
	return number;
}

char human_piece()
{
	char go_first = ask_yes_no("Do you require the first move");
	if (go_first = 'y')
	{
		cout << "Then go ahead, take the first move" << endl;
		return x;
	}
	else
	{
		cout << "Your bravery will be your undoing, i will go first!" << endl;
		return o;
	}
}

char opponent(char piece)
{
	if (piece == x)
	{
		return o;
	}
	else
	{
		return x;
	}
}

void display_board(const vector<char>& board)
{
	cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2];
	cout << "\n\t" << "---------";
	cout << "\n\t" << board[3] << " | " << board[4] << " | " << board[5];
	cout << "\n\t" << "---------";
	cout << "\n\t" << board[6] << " | " << board[7] << " | " << board[8];
	cout << "\n\n";
}

char winner(const vector<char>& board)
{
	//all possible winning rows
	const int WINNING_ROWS[8][3] =
	{
	{0, 1, 2},
	{3, 4, 5},
	{6, 7, 8},
	{0, 3, 6},
	{1, 4, 7},
	{2, 5, 8},
	{0, 4, 8},
	{2, 4, 6}

	};
}

const int TOTAL_ROWS = 8;

// If any winning row has three values that are the same (and not EMPTY)
// Then we have a winner

	for (int row = 0; row < TOTAL_ROWS; ++row)
{
	if ((board[WINNING_ROWS[row][0]] != EMPTY) &&
		(board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &&
		(board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]))
	{
		return board[WINNING_ROWS[row][0]];
	}
		
}

//since nobody has won, check for a tie (no empty squares left)

if (count(board.begin(), board.end(), EMPTY) == 0)
return TIE;

//since nobody has won and it isn't a tie, the game isn't over
return NO_ONE;

}

inline bool is_legal(int move, const vector<char>& board)
{
	return (board[move] = EMPTY) :
}

int human_move(const vector<char>& board, char human)
{
	int move = ask_number("Where will you move?", (board.size() - 1));
	while (!is_legal(move, board))
	{
		cout << "\n That square is alreay occupied, foolish human.\n";
		move = ask_number("Where will you move?"), (board.size() - 1));
	}
	cout << "Fine..\n"
	return move;
}

int computer_move(vector<char> board, char computer)

{
	unsigned int move = 0;
	bool found = false;
	// if computer can win on  next move, that's the move to make
	while (!found && move < board.size())
	{
		if (is_legal(move, board))
		{
			board[move] = computer;
			found = winner(board) == computer;
			board[move] = empty;
		}
		if (!found)
		{
			++move;
		}
	}

	// otherwise, if human can win on next move, that's the move to make

	if (!found)
	{
		move = 0;
		char human = opponent(computer);

		while (!found && move < board.size())
		{
			if (is_legal(move, board))
			{
				board[move] = human;
				found = winner(board) == human;
				board[move] = EMPTY;
			}
			if (!found)
			{
				++move;
			}
		}
	}

	if (!found)
	{
		move = 0;
		unsigned int i = 0;
		const int BEST_MOVES[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 };
		// Pick best open square

		while (!found && i < board.size())
		{
			move = BEST_MOVES[i];
			if (is_legal(move, board))
			{
				found = true;
			}
			++i;
		}
	}

	cout << "I shall take square number" << move << endl << endl;
	return move;

}

void announce_winner(char winner, char computer, char human)
{
	if (winner == computer)
	{
		cout << "The winner is the computer" << endl;
	}
	else if (winner == human)
	{
		cout << "The winner is the human" << endl;
	}
	else
	{
		cout << "It was a tie break" << endl;
	}
}



Regarding the errors, these lines are looking to be problematic

return NO_ONE;

char human = opponent(computer);

while (!found && move < board.size())

return move;
Last edited on
Edit your post to include code tags.
http://www.cplusplus.com/articles/jEywvCM9/
Please use [.code] beacon (without the dot) and tell us the exact error so we can help you.
Hi

Apologies for the poor format i have rectified that and will use proper formatting going forward. I have pointed out the 4 lines of code which seem to be the problem, there are many error messages however, if need be i will write them all out but there looks to be around 20 :-/
Well it looks like the brace at line 153 is misplaced, because there's a lot of code following it which isn't inside a function.
Hi

I have managed to find the entire code through another website which runs so i'll start going through it, thanks
Topic archived. No new replies allowed.