Stuff happened
[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                 Piece(int x, int y, const Colour & colour, const Type & type1, const Type & type2=UNKNOWN, int type_index = -1); // constructor
36                 Piece(const Piece & cpy); // copy constructor
37                 virtual ~Piece() {} // destructor
38
39                 int x; int y; // position of the piece
40                 Colour colour; // colour of the piece
41                 int type_index; // indicates state the piece is in; 0, 1, or -1 (unknown)
42                 Type types[2]; // states of the piece
43                 Type current_type; // current state of the piece
44                 
45                 static Type str2type(const std::string & str);
46                 static Colour str2colour(const std::string & str);
47
48 };
49
50 /**
51  * @class Square
52  * @purpose Represent a Square on the board; not necessarily occupied
53  */
54 class Square
55 {
56         public:
57                 Square() : x(-1), y(-1), piece(NULL) {} // constructor
58                 Square(int new_x, int new_y, Piece * new_piece = NULL) : x(new_x), y(new_y), piece(new_piece) {} //UNUSED
59                 Square(const Square & cpy) : x(cpy.x), y(cpy.y), piece(cpy.piece) {} // copy constructor (UNUSED)
60                 virtual ~Square() {} //destructor
61                 int x;  int y; // position of the square
62                 Piece * piece; // Piece that is in the Square (NULL if unoccupied)
63 };
64
65 /**
66  * @class Board
67  * @purpose Represent a quantum chess board
68  */
69 class Board
70 {
71         public:
72                 Board(bool choose_types = false); // constructor
73                 Board(const Board & cpy); // copy constructor
74                 virtual ~Board(); // destructor
75
76
77                 // helper; return vector of pieces given player colour
78                 std::vector<Piece*> & pieces(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white : black);} 
79                 // helper; return king given player colour      
80                 Piece * king(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white_king : black_king);}
81                 
82                 void Update_move(int x, int y, int x2, int y2); // move a piece
83                 void Update_select(int x, int y, int index, const std::string & type); // update a selected piece
84         
85                 Square & square(int x, int y) {return grid[x][y];} // get square on board
86
87                 void Get_moves(Piece * p, std::vector<Square*> & v); // get allowed moves for piece
88
89                 // determine if position is on the board
90                 bool Valid_position(int x, int y) {return (x >= 0 && x <= BOARD_WIDTH-1 && y >= 0 && y <= BOARD_HEIGHT-1);}
91
92         private:
93                 Square grid[BOARD_WIDTH][BOARD_HEIGHT];
94
95         
96                 std::vector<Piece*> white;
97                 std::vector<Piece*> black;
98                 Piece * white_king;
99                 Piece * black_king;
100
101                 // Add a move to the vector if it is valid
102                 void Move(Piece * p, int x, int y, std::vector<Square*> & v);
103
104                 // Add all valid moves in a direction, stopping at the first invalid move
105                 void Scan(Piece * p, int vx, int vy, std::vector<Square*> & v);
106 };
107
108 /**
109  * @class Exception
110  * @purpose The only exception.
111  */
112 class Exception
113 {
114         public:
115                 Exception(const char * funct, const char * fmt, ...)
116                 {
117                         fprintf(stderr, "Exception in %s - ", funct);
118                         va_list va;
119                         va_start(va, fmt);
120                         vfprintf(stderr, fmt, va);
121                         va_end(va);
122                         fprintf(stderr, "\n");                  
123                 }
124         
125 };
126
127 #endif //_QCHESS_H
128
129 //EOF

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