4bd747816cb069f89bc0ce4f17b333fb5b33247d
[progcomp2012.git] / samples / forfax / forfax.h
1 /**
2  * "forfax", a sample Stratego AI for the UCC Programming Competition 2012
3  * Declarations for classes Piece, Board and Forfax, Declaration/Implementation of helper class MovementChoice
4  * @author Sam Moore (matches) [SZM]
5  * @website http://matches.ucc.asn.au/stratego
6  * @email [email protected] or [email protected]
7  * @git git.ucc.asn.au/progcomp2012.git
8  */
9
10 #ifndef FORFAX_H
11 #define FORFAX_H
12
13 #include <vector> //Uses C++ std::vectors to store pieces
14 #include <string> //Uses C++ std::string
15 #include <iostream> //For debug
16 #include <cassert> //For debug
17
18
19
20 class Board;    //Forward declaration used by class Piece
21         
22 /**
23  * Class to represent a piece on the board
24  */
25 class Piece
26 {
27         public:
28                 typedef enum {ERROR=14,BOMB=13,MARSHAL=12, GENERAL=11, COLONEL=10, MAJOR=9, CAPTAIN=8, LIEUTENANT=7, SERGEANT=6, MINER=5, SCOUT=4, SPY=3, FLAG=2,BOULDER=1, NOTHING=0} Type; //Type basically defines how strong the piece is
29                 typedef enum {RED=0, BLUE=1, NONE, BOTH} Colour; //Used for the allegiance of the pieces - terrain counts as NONE.
30
31                 Piece(int newX, int newY,const Colour & newColour);
32                 Piece(int newX, int newY,const Colour & newColour, const Type & fixedRank);
33                 virtual ~Piece() {}
34                 
35                 void SetCoords(int newX, int newY) {x = newX; y = newY;}
36                 void GetCoords(int & storeX, int & storeY) const {storeX = x; storeY = y;}
37                 const Colour & GetColour() const {return colour;}
38
39                 static  char tokens[]; //The tokens used to identify various pieces
40                 static int maxUnits[]; //The maximum allowed number of units of each piece
41
42                 static Type GetType(char fromToken); //Retrieves the type of a piece given its character token
43                 static Colour Opposite(const Colour & colour) {return colour == RED ? BLUE : RED;}
44                 bool Mobile() const
45                 {
46                         if (minRank == maxRank)
47                                 return (minRank != Piece::FLAG && minRank != Piece::BOMB);
48                         else
49                                 return true;
50                 }
51
52                 int x; int y;
53                 const Colour colour; //The colour of the piece
54                 Type minRank; //The minimum possible rank of the piece
55                 Type maxRank; //The maximum possible rank of the piece
56                 int lastMove;
57                 
58                 
59
60 };
61
62 /**
63  * Class to represent a board
64  */
65 class Board
66 {
67         public:
68                 Board(int width, int height);
69                 virtual ~Board();
70
71
72                 std::vector<Piece*> & GetPieces(const Piece::Colour & colour) {return colour == Piece::RED ? red : blue;} //retrieve array of pieces
73                 
74                 Piece * Get(int x, int y) const; //Retrieve single piece
75                 Piece *  Set(int x, int y, Piece * newPiece); //Add piece to board
76
77                 bool ValidPosition(int x, int y) const {return (x > 0 && x < width && y > 0 && y < height);}
78
79                 int Width() const {return width;}
80                 int Height() const {return height;}
81
82                 typedef enum {UP=0, DOWN=1, LEFT=2, RIGHT=3, NONE=4} Direction;
83                 static Direction StrToDir(const std::string & str);
84                 static void DirToStr(const Direction & dir, std::string & buffer);
85
86                 static void MoveInDirection(int & x, int & y, const Direction & dir, int multiplier = 1);
87                 static Direction DirectionBetween(int x1, int y1, int x2, int y2);
88
89                 
90
91                 static int redUnits[];
92                 static int blueUnits[];
93         
94
95                 bool ForgetPiece(Piece * forget); //removes piece from the red and blue vectors
96
97         private:
98                 friend class Forfax;
99
100                 int width;
101                 int height;
102                 Piece ** * board;
103
104                 std::vector<Piece*> red; //Store all red pieces
105                 std::vector<Piece*> blue; //Store all blue pieces
106         
107
108 };
109
110 /** 
111  * Class to manage the Forfax AI
112  */
113 class Forfax
114 {
115         public:
116                 Forfax();
117                 virtual ~Forfax();
118
119                 typedef enum {OK, NO_NEWLINE, EXPECTED_ATTACKER, UNEXPECTED_DEFENDER, NO_ATTACKER, NO_DEFENDER, COLOUR_MISMATCH, INVALID_QUERY, BOARD_ERROR, VICTORY} Status;
120
121                 Status Setup(); //Waits for input to determine colour and board size, and then responds with setup
122                 Status MakeMove(); //Should be called each turn - determines Forfax's move
123
124
125                 //Move score functions
126                 double MovementScore(Piece * move, const Board::Direction & dir) const; //Calculate total score
127                 double CombatSuccessChance(Piece * attacker, Piece * defender) const; //Calculate chance of success in combat
128                 double IntrinsicWorth(int x, int y) const; //How much a given point on the board is worth
129                 double VictoryScore(Piece * attacker, Piece * defender) const; //How much killing the defender is worth
130                 double DefeatScore(Piece * attacker, Piece * defender) const; //How much losing is worth
131
132
133                 void PrintBoard(std::ostream & out);
134
135         protected:
136                 Status MakeFirstMove(); //Should only be called on the first turn
137                 Status InterpretMove();
138         private:
139                 Board * board; //Forfax stores the state on a board
140                 Piece::Colour colour; //Forfax needs to know his colour
141                 std::string strColour; //String of colour
142                 int turnNumber; //Forfax needs to know what turn number it is
143                 
144                 static int remainingUnits[2][15]; //Known remaining units, accessed by [colour][type]
145
146 };
147
148 /**
149  * Helper class used to store various moves in the board, and their associated scores
150  */
151 class MovementChoice
152 {
153         public:
154                 MovementChoice(Piece * newPiece, const Board::Direction & newDir, const Forfax & forfax) : piece(newPiece), dir(newDir) 
155                 {
156                         score = forfax.MovementScore(piece, dir);
157                 }
158
159                 MovementChoice(const MovementChoice & cpy) : piece(cpy.piece), dir(cpy.dir), score(cpy.score)
160                 {
161
162                 }
163                 
164                 bool operator<(const MovementChoice & a) const {return score < a.score;}
165                 bool operator>(const MovementChoice & a) const {return score > a.score;}
166                 bool operator<=(const MovementChoice & a) const {return score <= a.score;}
167                 bool operator>=(const MovementChoice & a) const {return score >= a.score;}
168                 bool operator==(const MovementChoice & a) const {return score == a.score;}
169                 bool operator!=(const MovementChoice & a) const {return score != a.score;}
170
171                 Piece * piece;
172                 Board::Direction dir;
173                 double score;
174                                 
175         
176 };
177
178
179 #endif //FORFAX_H
180
181 //EOF
182
183

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