Help with Excercise

So I'm doing an excercise on a tic-tac-toe program with some A.I. But I am being given a ton of errors, sadly the errors are beyond me and was hoping anyone could help me?
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
	// Tic-Tac-Toe
// Plays the game of tic-tac-toe against a human opponent

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

// global constants

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 askYesNo(string question);
int askNumber(string question, int high, int low = 0);
char humanPiece();
char opponent(char piece);
void displayBoard(const vector<char>& board);
char winner(const vector<char>& board);
bool isLegal(const vector<char>& board, int move);
int humanMove(const vector<char>& board, char human);
int computerMove(vector<char>board,char computer);
void announceWinner(char winner, char computer, char human);

// main function

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

	instructions();
	char human = humanPiece();
	char computer = opponent(human);
	char turn = X;
	displayBoard(board);

	while(winner(board) == NO_ONE)
	{
		if (turn == human)
		{
			move = humanMove(board, human);
			board[move] = human;
		}

		else
		{
			move = computerMove(board, computer);
			board[move] = computer;
		}

		displayBoard(board);
		turn = opponent(turn);
	}

	announceWinner(winner(board),computer,human);

	return 0;
}

void instructions()
{
	cout << "Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\n";
	cout << "--where human brain is pit against silicon proccessor\n\n";

	cout << "Make your move known by entering a number, 0-8. The number\n";
	cout << "corresponds to the desired board position, as illustrated:\n\n";

	cout << "		0 | 1 | 2\n";
	cout << "		3 | 4 | 5\n";
	cout << "		6 | 7 | 8\n\n";

	cout << "Prepare yourself, human. The battle is about to begin.\n\n";
}

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

	return response;
}

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

	return number;
}

char humanPiece()
{
	char go_first = askYesNo("Do you require the first move?");
	if(go_first == 'y')
	{
		cout << "\nThen take the first move. You will need it.\n";
		return X;
	}

	else
	{
		cout << "\nYour bravery will be your undoing... I will go first.\n";
		return 0;
	}
}

char opponent(char piece)
{
	if (piece == X) // remember must be a capital X!
	{
		return 0;
	}

	else
	{
		return X;
	}
}

void displayBoard(const vector<char>& board) // vector is passed through a constant
{
	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\t";
}

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)

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

// since nobody has won it isn't a tie, the game isn't over

return NO_ONE;

}

inline bool isLegal(int move, const vector<char>& board)

{
	return (board[move] == EMPTY);
}

int humanMove(const vector<char>& board, char human)
{
	int move = askNumber("Where will you move?", (board.size() - 1 ));
	while(!isLegal(move,board))
	{
		cout << "\nThat square is already occupied, foolish human.\n";
		move = askNumber("Where will you move?", (board.size() - 1));
	}

	cout << "Fine...\n";

	return move;

}

int computerMove(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(isLegal(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(isLegal(move,board))
			{
				board[move] = human;
				found = winner (board) == human;
				board[move] = EMPTY;
			}

			if(!found)
			{
				++move;
			}


		}

	}

	// otherwise, moving to the best open square is to move to make
	if(!found)
	{
		move = 0;
		unsigned int i = 0;

		const int BEST_MOVES[] = {4,0,2,6,8,1,3,5,7};
		//pick best square
		while(!found && i < board.size())
		{
			move = BEST_MOVES[i];
			if(isLegal(move,board))
			{
				found = true;
			}

			++i;

		}

	}

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

	return move;

}

void announceWinner(char winner,char computer,char human)
{
	if(winner == computer)
	{
		cout << winner << "'s won!\n";
		cout << "As I predicted, human. I am triumphant once more -- proof\n";
		cout << "that computers are superior to humans in all regards.\n";
	}

	else if(winner == human)
	{
		cout << winner << "'s won!\n";
		cout << "No, no! It cannot be! Somehow you tricked me human.\n";
		cout << "But never again! I, the computer, so swear it!\n";

	}

	else
	{
		cout << "It's a tie.\n";
		cout << "You were most lucky, human, and somehow you managed to tie me.\n";
		cout << "Celebrate... for this is the best you will ever achieve.\n";
	}


}
Errors due to post being to long:
1>------ Build started: Project: Tic-Tac-Toe A.I, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(168): error C2679: binary '[' : no operator found which takes a right-hand operand of type 'const int [8][3]' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(1119): could be 'const char &std::vector<_Ty>::operator [](unsigned int) const'
1> with
1> [
1> _Ty=char
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(1135): or 'char &std::vector<_Ty>::operator [](unsigned int)'
1> with
1> [
1> _Ty=char
1> ]
1> while trying to match the argument list '(const std::vector<_Ty>, const int [8][3])'
1> with
1> [
1> _Ty=char
1> ]
1>c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(190): error C2601: 'isLegal' : local function definitions are illegal
1> c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(148): this line contains a '{' which has not yet been matched
1>c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(195): error C2601: 'humanMove' : local function definitions are illegal
1> c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(148): this line contains a '{' which has not yet been matched
1>c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(211): error C2601: 'computerMove' : local function definitions are illegal
1> c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(148): this line contains a '{' which has not yet been matched
1>c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(287): error C2601: 'announceWinner' : local function definitions are illegal
1> c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(148): this line contains a '{' which has not yet been matched
1>c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(312): fatal error C1075: end of file found before the left brace '{' at 'c:\users\admin\documents\visual studio 2012\projects\tic-tac-toe a.i\tic-tac-toe a.i\source.cpp(148)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


EDIT: For some reason it runs?
Last edited on
Topic archived. No new replies allowed.