Fix bugs with FifoPlayer and Networking
[progcomp2013.git] / agents / silverfish / qchess.h
1 /**
2  * agent++ : A Sample agent for UCC::Progcomp2013
3  * @file qchess.h
4  * @purpose Declarations for game related classes; Piece, Square, Board
5  */
6
7 #ifndef _QCHESS_H
8 #define _QCHESS_H
9
10
11 // board height and width (don't change!)
12 #define BOARD_HEIGHT 8
13 #define BOARD_WIDTH 8
14
15 #include <string>
16 #include <vector>
17 #include <map>
18 #include <cstdlib>
19 #include <iostream>
20
21 #include <stdarg.h>
22 #include <cstdio> // for vfprintf... for the Exception
23
24 class Board; //forward declaration
25
26 /**
27  * @class Piece
28  * @purpose Represent a quantum chess piece
29  */
30 class Piece
31 {
32         public:
33
34                 typedef enum {PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, UNKNOWN} Type;
35                 typedef enum {WHITE=0, BLACK=1} Colour;
36                 
37                 static Type AllTypes[];
38                 static Colour AllColours[];
39                 
40
41                 virtual ~Piece() {} // destructor
42
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
48                 
49                 static Type str2type(const std::string & str);
50                 static Colour str2colour(const std::string & str);
51                 static const char * type2str(const Type & t);
52                 
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);
55                 
56                 static Colour Opposite(const Colour & c)
57                 {
58                         return (c == WHITE) ? BLACK : WHITE;    
59                 }
60                 
61                 double ProbIsType(Board & b, const Piece::Type & t);
62                 
63         private:
64                 friend class Board;
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
68
69                 
70
71 };
72
73 /**
74  * @class Square
75  * @purpose Represent a Square on the board; not necessarily occupied
76  */
77 class Square
78 {
79         public:
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)
86         
87                 const std::map<Piece*, double> & Coverage(const Piece::Colour & c)
88                 {
89                         return (c == Piece::WHITE) ? coverage[Piece::WHITE] : coverage[Piece::BLACK];
90                 }
91                 
92                 void Update_coverage(Board & b);
93                 
94         private:
95                 std::map<Piece::Colour, std::map<Piece*, double> > coverage;
96 };
97
98 /**
99  * @class Board
100  * @purpose Represent a quantum chess board
101  */
102 class Board
103 {
104         public:
105                 Board(bool choose_types = false); // constructor
106                 Board(Board & parent); // clones a board, copy on write
107                 virtual ~Board(); // destructor
108
109
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)
114                 {
115                         return ((colour == Piece::WHITE) ? white_unknown : black_unknown);
116                 }
117                 
118                 int & nUnknown(const Piece::Colour & colour)
119                 {
120                         return ((colour == Piece::WHITE) ? white_nUnknown : black_nUnknown);
121                 }
122                 
123                 // helper; return king given player colour      
124                 Piece * king(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white_king : black_king);}
125                 
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);
129         
130                 Square & square(int x, int y) {return grid[x][y];} // get square on board
131
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
134
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);}
137                 
138
139                 // scans from a square until a piece is reached
140                 Piece * ScanPiece(Square & s, int vx, int vy);
141                 
142                 // Get a piece
143                 Square & SquareAt(int x, int y);
144
145         private:
146                 Square grid[BOARD_WIDTH][BOARD_HEIGHT];
147
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;
154                 int white_nUnknown;
155                 int black_nUnknown;
156                 Piece * white_king;
157                 Piece * black_king;
158                 
159                 
160
161                 // Add a move to the vector if it is valid
162                 void CheckMove(Piece * p, int x, int y, std::vector<Square*> & v);
163
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);
166                 
167                 
168 };
169
170 /**
171  * @class Exception
172  * @purpose The only exception.
173  */
174 class Exception
175 {
176         public:
177                 Exception(const char * funct, const char * fmt, ...)
178                 {
179                         fprintf(stderr, "Exception in %s - ", funct);
180                         va_list va;
181                         va_start(va, fmt);
182                         vfprintf(stderr, fmt, va);
183                         va_end(va);
184                         fprintf(stderr, "\n");                  
185                 }
186         
187 };
188
189 #endif //_QCHESS_H
190
191 //EOF

UCC git Repository :: git.ucc.asn.au