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
26 * @purpose Represent a quantum chess piece
32 typedef enum {PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, UNKNOWN} Type;
33 typedef enum {WHITE=0, BLACK=1} Colour;
35 virtual ~Piece() {} // destructor
37 int x; int y; // position of the piece
38 Colour colour; // colour of the piece
39 int type_index; // indicates state the piece is in; 0, 1, or -1 (unknown)
40 Type types[2]; // states of the piece
41 Type current_type; // current state of the piece
43 static Type str2type(const std::string & str);
44 static Colour str2colour(const std::string & str);
45 static const char * type2str(const Type & t);
47 static Piece * AddPiece(std::vector<Piece*> & v, int x, int y, const Colour & colour, const Type & type1, const Type & type2, int type_index=-1);
48 static Piece * AddPiece(std::vector<Piece*> & v, const Piece & cpy);
52 Piece(int x, int y, const Colour & colour, const Type & type1, const Type & type2
53 , int type_index); // constructor
54 Piece(const Piece & cpy); // copy constructor
62 * @purpose Represent a Square on the board; not necessarily occupied
67 Square() : x(-1), y(-1), piece(NULL) {} // constructor
68 Square(int new_x, int new_y, Piece * new_piece = NULL) : x(new_x), y(new_y), piece(new_piece) {} //UNUSED
69 Square(const Square & cpy) : x(cpy.x), y(cpy.y), piece(cpy.piece) {} // copy constructor (UNUSED)
70 virtual ~Square() {} //destructor
71 int x; int y; // position of the square
72 Piece * piece; // Piece that is in the Square (NULL if unoccupied)
77 * @purpose Represent a quantum chess board
82 Board(bool choose_types = false); // constructor
83 Board(Board & parent); // clones a board, copy on write
84 virtual ~Board(); // destructor
87 // helper; return vector of pieces given player colour
88 std::vector<Piece*> & pieces(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white : black);}
89 // helper; return map of unidentified 2nd types for given colour
90 std::map<Piece::Type, int> & unknown_types(const Piece::Colour & colour)
92 return ((colour == Piece::WHITE) ? white_unknown : black_unknown);
95 int & nUnknown(const Piece::Colour & colour)
97 return ((colour == Piece::WHITE) ? white_nUnknown : black_nUnknown);
100 // helper; return king given player colour
101 Piece * king(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white_king : black_king);}
103 void Update_move(int x, int y, int x2, int y2); // move a piece
104 void Update_select(int x, int y, int index, const std::string & type); // update a selected piece
105 void Update_select(int x, int y, int index, const Piece::Type & t);
107 Square & square(int x, int y) {return grid[x][y];} // get square on board
109 void Get_moves(Piece * p, std::vector<Square*> & v); // get allowed moves for piece of known type
111 // determine if position is on the board
112 bool Valid_position(int x, int y) {return (x >= 0 && x <= BOARD_WIDTH-1 && y >= 0 && y <= BOARD_HEIGHT-1);}
117 Square grid[BOARD_WIDTH][BOARD_HEIGHT];
119 // All pieces for each player
120 std::vector<Piece*> white;
121 std::vector<Piece*> black;
122 // The number of pieces with each 2nd type that are still unidentified
123 std::map<Piece::Type, int> white_unknown;
124 std::map<Piece::Type, int> black_unknown;
132 // Add a move to the vector if it is valid
133 void Move(Piece * p, int x, int y, std::vector<Square*> & v);
135 // Add all valid moves in a direction, stopping at the first invalid move
136 void Scan(Piece * p, int vx, int vy, std::vector<Square*> & v);
141 * @purpose The only exception.
146 Exception(const char * funct, const char * fmt, ...)
148 fprintf(stderr, "Exception in %s - ", funct);
151 vfprintf(stderr, fmt, va);
153 fprintf(stderr, "\n");