054854bb270d1cd1a05a0bdd277b5314727c9df1
[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                 int lastx; int lasty;
58                 
59                 
60
61 };
62
63 /**
64  * Class to represent a board
65  */
66 class Board
67 {
68         public:
69                 Board(int width, int height);
70                 virtual ~Board();
71
72
73                 std::vector<Piece*> & GetPieces(const Piece::Colour & colour) {return colour == Piece::RED ? red : blue;} //retrieve array of pieces
74                 
75                 Piece * Get(int x, int y) const; //Retrieve single piece
76                 Piece * GetClosest(int x, int y, const Piece::Colour & search = Piece::BOTH) const; //Retrieve closest piece of specified colour to the point
77                 Piece *  Set(int x, int y, Piece * newPiece); //Add piece to board
78
79                 bool ValidPosition(int x, int y) const {return (x > 0 && x < width && y > 0 && y < height);}
80
81                 int Width() const {return width;}
82                 int Height() const {return height;}
83
84                 typedef enum {UP=0, DOWN=1, LEFT=2, RIGHT=3, NONE=4} Direction;
85                 static Direction StrToDir(const std::string & str);
86                 static void DirToStr(const Direction & dir, std::string & buffer);
87
88                 static void MoveInDirection(int & x, int & y, const Direction & dir, int multiplier = 1);
89                 static Direction DirectionBetween(int x1, int y1, int x2, int y2);
90                 static int NumberOfMoves(int x1, int y1, int x2, int y2);
91                 
92
93                 static int redUnits[];
94                 static int blueUnits[];
95         
96
97                 bool ForgetPiece(Piece * forget); //removes piece from the red and blue vectors
98
99         private:
100                 friend class Forfax;
101
102                 int width;
103                 int height;
104                 Piece ** * board;
105
106                 std::vector<Piece*> red; //Store all red pieces
107                 std::vector<Piece*> blue; //Store all blue pieces
108         
109
110 };
111
112 /** 
113  * Class to manage the Forfax AI
114  */
115 class Forfax
116 {
117         public:
118                 Forfax();
119                 virtual ~Forfax();
120
121                 typedef enum {OK, NO_NEWLINE, EXPECTED_ATTACKER, UNEXPECTED_DEFENDER, NO_ATTACKER, NO_DEFENDER, COLOUR_MISMATCH, INVALID_QUERY, BOARD_ERROR, VICTORY} Status;
122
123                 Status Setup(); //Waits for input to determine colour and board size, and then responds with setup
124                 Status MakeMove(); //Should be called each turn - determines Forfax's move
125
126
127                 //Move score functions
128                 double MovementScore(Piece * move, const Board::Direction & dir) const; //Calculate total score
129                 double CombatSuccessChance(Piece * attacker, Piece * defender) const; //Calculate chance of success in combat
130                 double CombatScore(int x, int y, Piece * attacker) const; //Calculate total worth of combat at a point
131                 double IntrinsicWorth(int x, int y) const; //How much a given point on the board is worth
132                 double VictoryScore(Piece * attacker, Piece * defender) const; //How much killing the defender is worth
133                 double DefeatScore(Piece * attacker, Piece * defender) const; //How much losing is worth
134
135
136                 void PrintBoard(std::ostream & out);
137
138         protected:
139                 Status MakeFirstMove(); //Should only be called on the first turn
140                 Status InterpretMove();
141         private:
142                 Board * board; //Forfax stores the state on a board
143                 Piece::Colour colour; //Forfax needs to know his colour
144                 std::string strColour; //String of colour
145                 int turnNumber; //Forfax needs to know what turn number it is
146                 
147                 static int remainingUnits[2][15]; //Known remaining units, accessed by [colour][type]
148
149 };
150
151 /**
152  * Helper class used to store various moves in the board, and their associated scores
153  */
154 class MovementChoice
155 {
156         public:
157                 MovementChoice(Piece * newPiece, const Board::Direction & newDir, const Forfax & forfax) : piece(newPiece), dir(newDir) 
158                 {
159                         score = forfax.MovementScore(piece, dir);
160                 }
161
162                 MovementChoice(const MovementChoice & cpy) : piece(cpy.piece), dir(cpy.dir), score(cpy.score)
163                 {
164
165                 }
166                 
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                 bool operator>=(const MovementChoice & a) const {return score >= a.score;}
171                 bool operator==(const MovementChoice & a) const {return score == a.score;}
172                 bool operator!=(const MovementChoice & a) const {return score != a.score;}
173
174                 Piece * piece;
175                 Board::Direction dir;
176                 double score;
177                                 
178         
179 };
180
181
182
183 #endif //FORFAX_H
184
185 //EOF
186
187

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