2 * agent++ : A Sample agent for UCC::Progcomp2013
4 * @purpose Definitions for game related classes; Piece, Square, Board
15 * @param new_x, new_y - Position of piece
16 * @param new_colour - Colour of piece
17 * @param type1, type2 - Types of piece
18 * @param new_type_index - Index for initial type of piece
19 * @param new_piece_index - Index for piece in a vector
21 Piece::Piece(int new_x, int new_y, const Piece::Colour & new_colour, const Piece::Type & type1, const Piece::Type & type2,
23 : x(new_x), y(new_y), colour(new_colour), type_index(new_type_index), types(), current_type()
25 types[0] = type1; types[1] = type2;
26 if (type_index < 0 || type_index >= 2)
28 current_type = Piece::UNKNOWN;
32 current_type = types[type_index];
38 * @param cpy - Piece to copy construct from
40 Piece::Piece(const Piece & cpy) : x(cpy.x), y(cpy.y), colour(cpy.colour), type_index(cpy.type_index)
42 types[0] = cpy.types[0];
43 types[1] = cpy.types[1];
48 * @param choose_types - Indicates whether Board should setup the 2nd types of pieces; default false
50 Board::Board(bool choose_types)
51 : white(), black(), white_unknown(), black_unknown(), white_nUnknown(0), black_nUnknown(0),
52 white_king(NULL), black_king(NULL)
55 // initialise all the Squares
56 for (int x = 0; x < BOARD_WIDTH; ++x)
58 for (int y = 0; y < BOARD_HEIGHT; ++y)
65 // const arrays simplify below code
66 Piece::Colour colours[] = {Piece::BLACK, Piece::WHITE};
67 Piece::Type types[] = {Piece::PAWN, Piece::BISHOP, Piece::KNIGHT, Piece::ROOK, Piece::QUEEN};
69 // frequency of each type of piece
70 map<Piece::Type, int> freq;
71 freq[Piece::ROOK] = 2;
72 freq[Piece::BISHOP] = 2;
73 freq[Piece::KNIGHT] = 2;
74 freq[Piece::QUEEN] = 1;
75 freq[Piece::PAWN] = 8;
85 // for white and black...
86 for (int i = 0; i < 2; ++i)
88 vector<Piece*> & v = pieces(colours[i]); // get vector of pieces
93 int y = (i == 0) ? 1 : BOARD_HEIGHT-2;
94 for (int x = 0; x < BOARD_WIDTH; ++x)
96 Piece::AddPiece(v, x, y, colours[i], Piece::PAWN, Piece::UNKNOWN);
100 y = (i == 0) ? 0 : BOARD_HEIGHT-1;
101 Piece::AddPiece(v, 0, y, colours[i], Piece::ROOK, Piece::UNKNOWN);
102 Piece::AddPiece(v, BOARD_WIDTH-1, y, colours[i], Piece::ROOK, Piece::UNKNOWN);
103 Piece::AddPiece(v, 1, y, colours[i], Piece::KNIGHT, Piece::UNKNOWN);
104 Piece::AddPiece(v, BOARD_WIDTH-2, y, colours[i], Piece::KNIGHT, Piece::UNKNOWN);
105 Piece::AddPiece(v, 2, y, colours[i], Piece::BISHOP, Piece::UNKNOWN);
106 Piece::AddPiece(v, BOARD_WIDTH-3, y, colours[i], Piece::BISHOP, Piece::UNKNOWN);
107 Piece::AddPiece(v, 3, y, colours[i], Piece::QUEEN, Piece::UNKNOWN);
109 Piece * k = Piece::AddPiece(v, 4, y, colours[i], Piece::KING, Piece::KING, 1);
116 // add to board and choose second types if required
117 map<Piece::Type, int> f(freq);
119 for (unsigned j = 0; j < v.size(); ++j)
122 grid[p->x][p->y].piece = p;
125 if (p->types[1] != Piece::UNKNOWN)
131 } while (f[types[type2]] <= 0);
132 f[types[type2]] -= 1;
134 p->types[1] = types[type2];
148 * @param cpy - Board to clone
150 Board::Board(Board & cpy)
151 : white(cpy.white), black(cpy.black), white_unknown(cpy.white_unknown), black_unknown(cpy.black_unknown),
152 white_nUnknown(cpy.white_nUnknown), black_nUnknown(cpy.black_nUnknown),
153 white_king(cpy.white_king), black_king(cpy.black_king)
155 for (int x = 0; x < BOARD_WIDTH; ++x)
157 for (int y = 0; y < BOARD_HEIGHT; ++y)
161 if (cpy.grid[x][y].piece != NULL)
163 vector<Piece*> & v = pieces(cpy.grid[x][y].piece->colour);
164 Piece::AddPiece(v, *(cpy.grid[x][y].piece));
177 for (int x = 0; x < BOARD_WIDTH; ++x)
179 for (int y = 0; y < BOARD_HEIGHT; ++y)
181 delete grid[x][y].piece;
188 * @funct Update_select
189 * @purpose Update Piece that has been selected
190 * @param x, y - Position of Piece to update
191 * @param index - 0 or 1 - State the Piece "collapsed" into
192 * @param type - Type of the Piece as a string
194 void Board::Update_select(int x, int y, int index, const string & type)
196 Board::Update_select(x, y, index, Piece::str2type(type));
200 * @funct Update_select
201 * @purpose Update Piece that has been selected
202 * @param x, y - Position of Piece to update
203 * @param index - 0 or 1 - State the Piece "collapsed" into
204 * @param t - Type of the Piece
206 void Board::Update_select(int x, int y, int index, const Piece::Type & t)
208 cerr << "Updating " << x << "," << y << " " << grid[x][y].piece << " " << index << " " << t << "\n";
209 Square & s = grid[x][y];
213 assert(s.piece != NULL);
214 assert(index >= 0 && index < 2);
215 s.piece->type_index = index;
217 if (s.piece->types[index] == Piece::UNKNOWN)
219 map<Piece::Type, int> & m = unknown_types(s.piece->colour);
222 throw Exception("Board::Update_select", "Too many pieces of type %s found", Piece::type2str(t));
224 nUnknown(s.piece->colour)--;
227 s.piece->types[index] = t;
228 s.piece->current_type = t;
233 * @purpose Move a Piece from one square to another
234 * @param x1, y1 - Coords of Square containing moving Piece
235 * @param x2, y2 - Coords of Square to move into
236 * NOTE: Any Piece in the destination Square will be destroyed ("taken")
237 * and the Board's other members updated accordingly
239 void Board::Update_move(int x1, int y1, int x2, int y2)
241 Square & s1 = grid[x1][y1];
242 Square & s2 = grid[x2][y2];
248 if (s2.piece != NULL)
250 vector<Piece*> & p = pieces(s2.piece->colour);
251 vector<Piece*>::iterator i = p.begin();
262 Piece * k = king(s2.piece->colour);
265 if (k->colour == Piece::WHITE)
283 * @purpose Get all moves for a Piece and store them
285 * @param v - vector to store Squares in. Will *not* be cleared.
287 void Board::Get_moves(Piece * p, vector<Square*> & v)
289 assert(p->current_type != Piece::UNKNOWN);
291 int x = p->x; int y = p->y;
292 if (p->current_type == Piece::KING)
298 Move(p, x+1, y+1, v);
299 Move(p, x+1, y-1, v);
300 Move(p, x-1, y+1, v);
301 Move(p, x-1, y-1, v);
303 else if (p->current_type == Piece::KNIGHT)
305 Move(p, x+2, y+1, v);
306 Move(p, x+2, y-1, v);
307 Move(p, x-2, y+1, v);
308 Move(p, x-2, y-1, v);
309 Move(p, x+1, y+2, v);
310 Move(p, x-1, y+2, v);
311 Move(p, x+1, y-2, v);
312 Move(p, x-1, y-2, v);
314 else if (p->current_type == Piece::PAWN)
316 int y1 = (p->colour == Piece::WHITE) ? BOARD_HEIGHT-2 : 1;
317 int y2 = (p->colour == Piece::WHITE) ? y1 - 2 : y1 + 2;
318 if (p->types[0] == Piece::PAWN && p->y == y1)
323 y2 = (p->colour == Piece::WHITE) ? y - 1 : y + 1;
326 if (Valid_position(x-1, y2) && grid[x-1][y2].piece != NULL)
328 if (Valid_position(x+1, y2) && grid[x+1][y2].piece != NULL)
331 else if (p->current_type == Piece::BISHOP)
338 else if (p->current_type == Piece::ROOK)
345 else if (p->current_type == Piece::QUEEN)
361 * @purpose Add a move to the vector, if it is valid
362 * @param p - Piece that would move
363 * @param x, y - Destination Square coords
364 * @param v - vector to put the destination Square in, if the move is valid
366 void Board::Move(Piece * p, int x, int y, vector<Square*> & v)
368 if (Valid_position(x, y) && (grid[x][y].piece == NULL || grid[x][y].piece->colour != p->colour))
370 v.push_back(&(grid[x][y]));
373 // cerr << "Square " << x << "," << y << " invalid; " << grid[x][y].piece << "\n";
378 * @purpose Add moves in a specified direction to the vector, until we get to an invalid move
379 * @param p - Piece to start scanning from
380 * @param vx, vy - "velocity" - change in coords each move
381 * @param v - vector to store valid Squares in
383 void Board::Scan(Piece * p, int vx, int vy, vector<Square*> & v)
387 while (Valid_position(x, y) && (grid[x][y].piece == NULL || grid[x][y].piece->colour != p->colour))
389 v.push_back(&(grid[x][y]));
390 if (grid[x][y].piece != NULL)
399 * @purpose Convert string to Piece::Type
400 * @param str - The string
401 * @returns A Piece::Type
403 Piece::Type Piece::str2type(const string & str)
407 else if (str == "queen")
409 else if (str == "rook")
411 else if (str == "bishop")
412 return Piece::BISHOP;
413 else if (str == "knight")
414 return Piece::KNIGHT;
415 else if (str == "pawn")
417 else if (str == "unknown")
418 return Piece::UNKNOWN;
420 throw Exception("Piece::str2type", "String \"%s\" doesn't represent a type", str.c_str());
421 return Piece::UNKNOWN;
426 * @purpose Convert Piece::Type to string
427 * @param t - The Types
428 * @returns a const char*
430 const char * Piece::type2str(const Piece::Type & t)
449 throw Exception("Piece::type2str", "Unknown type %d", (int)t);
456 * @purpose Convert string to Piece::Colour
457 * @param str - The string
458 * @returns A Piece::Colour
460 Piece::Colour Piece::str2colour(const string & str)
464 else if (str == "black")
467 throw Exception("Piece::str2colour", "string \"%s\" is not white|black", str.c_str());
468 return Piece::BLACK; // should never get here
473 * @purpose Creates a new Piece and adds it to a vector
474 * @param v - The vector
475 * @params - All remaining parameters passed to Piece::Piece
476 * @returns Pointer to the new Piece
478 Piece * Piece::AddPiece(vector<Piece*> & v, int x, int y, const Piece::Colour & colour, const Piece::Type & t1, const Piece::Type & t2,
481 Piece * p = new Piece(x,y,colour,t1, t2,type_index);
488 * @purpose Copy a Piece and add it to a vector
489 * @param v - The vector
490 * @param cpy - Piece to copy
491 * @returns Pointer to the new Piece
493 Piece * Piece::AddPiece(vector<Piece*> & v, const Piece & cpy)
495 Piece * p = new Piece(cpy);