Dedicated server stuff should now work
[progcomp2013.git] / agents / c++ / 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 /**
25  * @class Piece
26  * @purpose Represent a quantum chess piece
27  */
28 class Piece
29 {
30         public:
31
32                 typedef enum {PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, UNKNOWN} Type;
33                 typedef enum {WHITE=0, BLACK=1} Colour;
34
35                 virtual ~Piece() {} // destructor
36
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
42                 
43                 static Type str2type(const std::string & str);
44                 static Colour str2colour(const std::string & str);
45                 static const char * type2str(const Type & t);
46                 
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);
49                 
50         private:
51                 friend class Board;
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
55
56                 
57
58 };
59
60 /**
61  * @class Square
62  * @purpose Represent a Square on the board; not necessarily occupied
63  */
64 class Square
65 {
66         public:
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)
73 };
74
75 /**
76  * @class Board
77  * @purpose Represent a quantum chess board
78  */
79 class Board
80 {
81         public:
82                 Board(bool choose_types = false); // constructor
83                 Board(Board & parent); // clones a board, copy on write
84                 virtual ~Board(); // destructor
85
86
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)
91                 {
92                         return ((colour == Piece::WHITE) ? white_unknown : black_unknown);
93                 }
94                 
95                 int & nUnknown(const Piece::Colour & colour)
96                 {
97                         return ((colour == Piece::WHITE) ? white_nUnknown : black_nUnknown);
98                 }
99                 
100                 // helper; return king given player colour      
101                 Piece * king(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white_king : black_king);}
102                 
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);
106         
107                 Square & square(int x, int y) {return grid[x][y];} // get square on board
108
109                 void Get_moves(Piece * p, std::vector<Square*> & v); // get allowed moves for piece of known type
110
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);}
113                 
114
115
116         private:
117                 Square grid[BOARD_WIDTH][BOARD_HEIGHT];
118
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;
125                 int white_nUnknown;
126                 int black_nUnknown;
127                 Piece * white_king;
128                 Piece * black_king;
129                 
130                 
131
132                 // Add a move to the vector if it is valid
133                 void Move(Piece * p, int x, int y, std::vector<Square*> & v);
134
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);
137 };
138
139 /**
140  * @class Exception
141  * @purpose The only exception.
142  */
143 class Exception
144 {
145         public:
146                 Exception(const char * funct, const char * fmt, ...)
147                 {
148                         fprintf(stderr, "Exception in %s - ", funct);
149                         va_list va;
150                         va_start(va, fmt);
151                         vfprintf(stderr, fmt, va);
152                         va_end(va);
153                         fprintf(stderr, "\n");                  
154                 }
155         
156 };
157
158 #endif //_QCHESS_H
159
160 //EOF

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