2 * agent++ : A Sample agent for UCC::Progcomp2013
4 * @purpose Declarations for game related classes; Piece, Square, Board
11 // board height and width (don't change!)
12 #define BOARD_HEIGHT 8
22 #include <cstdio> // for vfprintf... for the Exception
24 class Board; //forward declaration
28 * @purpose Represent a quantum chess piece
34 typedef enum {PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, UNKNOWN} Type;
35 typedef enum {WHITE=0, BLACK=1} Colour;
37 static Type AllTypes[];
38 static Colour AllColours[];
41 virtual ~Piece() {} // destructor
43 int x; int y; // position of the piece
44 Colour colour; // colour of the piece
45 int type_index; // indicates state the piece is in; 0, 1, or -1 (unknown)
46 Type types[2]; // states of the piece
47 Type current_type; // current state of the piece
49 static Type str2type(const std::string & str);
50 static Colour str2colour(const std::string & str);
51 static const char * type2str(const Type & t);
53 static Piece * AddPiece(std::vector<Piece*> & v, int x, int y, const Colour & colour, const Type & type1, const Type & type2, int type_index=-1);
54 static Piece * AddPiece(std::vector<Piece*> & v, const Piece & cpy);
56 static Colour Opposite(const Colour & c)
58 return (c == WHITE) ? BLACK : WHITE;
61 double ProbIsType(Board & b, const Piece::Type & t);
65 Piece(int x, int y, const Colour & colour, const Type & type1, const Type & type2
66 , int type_index); // constructor
67 Piece(const Piece & cpy); // copy constructor
75 * @purpose Represent a Square on the board; not necessarily occupied
80 Square() : x(-1), y(-1), piece(NULL) {} // constructor
81 Square(int new_x, int new_y, Piece * new_piece = NULL) : x(new_x), y(new_y), piece(new_piece) {} //UNUSED
82 Square(const Square & cpy) : x(cpy.x), y(cpy.y), piece(cpy.piece) {} // copy constructor (UNUSED)
83 virtual ~Square() {} //destructor
84 int x; int y; // position of the square
85 Piece * piece; // Piece that is in the Square (NULL if unoccupied)
87 const std::map<Piece*, double> & Coverage(const Piece::Colour & c)
89 return (c == Piece::WHITE) ? coverage[Piece::WHITE] : coverage[Piece::BLACK];
92 void Update_coverage(Board & b);
95 std::map<Piece::Colour, std::map<Piece*, double> > coverage;
100 * @purpose Represent a quantum chess board
105 Board(bool choose_types = false); // constructor
106 Board(Board & parent); // clones a board, copy on write
107 virtual ~Board(); // destructor
110 // helper; return vector of pieces given player colour
111 std::vector<Piece*> & pieces(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white : black);}
112 // helper; return map of unidentified 2nd types for given colour
113 std::map<Piece::Type, int> & unknown_types(const Piece::Colour & colour)
115 return ((colour == Piece::WHITE) ? white_unknown : black_unknown);
118 int & nUnknown(const Piece::Colour & colour)
120 return ((colour == Piece::WHITE) ? white_nUnknown : black_nUnknown);
123 // helper; return king given player colour
124 Piece * king(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white_king : black_king);}
126 void Update_move(int x, int y, int x2, int y2); // move a piece
127 void Update_select(int x, int y, int index, const std::string & type); // update a selected piece
128 void Update_select(int x, int y, int index, const Piece::Type & t);
130 Square & square(int x, int y) {return grid[x][y];} // get square on board
132 void Get_moves(Square & s, const Piece::Type & t, std::vector<Square*> & v);
133 void Get_moves(Piece * p, std::vector<Square*> & v); // get allowed moves for piece of known type
135 // determine if position is on the board
136 bool Valid_position(int x, int y) const {return (x >= 0 && x <= BOARD_WIDTH-1 && y >= 0 && y <= BOARD_HEIGHT-1);}
139 // scans from a square until a piece is reached
140 Piece * ScanPiece(Square & s, int vx, int vy);
143 Square & SquareAt(int x, int y);
146 Square grid[BOARD_WIDTH][BOARD_HEIGHT];
148 // All pieces for each player
149 std::vector<Piece*> white;
150 std::vector<Piece*> black;
151 // The number of pieces with each 2nd type that are still unidentified
152 std::map<Piece::Type, int> white_unknown;
153 std::map<Piece::Type, int> black_unknown;
161 // Add a move to the vector if it is valid
162 void CheckMove(Piece * p, int x, int y, std::vector<Square*> & v);
164 // Add all valid moves in a direction, stopping at the first invalid move
165 void ScanMoves(Piece * p, int vx, int vy, std::vector<Square*> & v);
172 * @purpose The only exception.
177 Exception(const char * funct, const char * fmt, ...)
179 fprintf(stderr, "Exception in %s - ", funct);
182 vfprintf(stderr, fmt, va);
184 fprintf(stderr, "\n");