First actual commit
[progcomp2012.git] / samples / forfax / forfax.h
1 #ifndef FORFAX_H
2 #define FORFAX_H
3
4 #include <vector> //Uses C++ std::vectors to store pieces
5 #include <string> //Uses C++ std::string
6 /**
7  * Header for the sample Stratego AI "forfax"
8  * @author Sam Moore 2011
9  */
10 class Board;            
11 /**
12  * Class to represent a piece on the board
13  */
14 class Piece
15 {
16         public:
17                 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
18                 typedef enum {RED=0, BLUE=1, NONE, BOTH} Colour; //Used for the allegiance of the pieces - terrain counts as NONE.
19
20                 Piece(int newX, int newY,const Colour & newColour);
21                 Piece(int newX, int newY,const Colour & newColour, const Colour & rankKnownBy, const Type & fixedRank);
22                 virtual ~Piece() {}
23                 
24                 void SetCoords(int newX, int newY) {x = newX; y = newY;}
25                 void GetCoords(int & storeX, int & storeY) const {storeX = x; storeY = y;}
26                 const Colour & GetColour() const {return colour;}
27
28                 static  char tokens[]; //The tokens used to identify various pieces
29                 static int maxUnits[]; //The maximum allowed number of units of each piece
30
31                 static Type GetType(char fromToken); //Retrieves the type of a piece given its character token
32                 static Colour Opposite(const Colour & colour) {return colour == RED ? BLUE : RED;}
33
34
35                 int x; int y;
36                 const Colour colour; //The colour of the piece
37                 Type minRank[2]; //The minimum possible rank of the piece, according to each colour
38                 Type maxRank[2]; //The maximum possible rank of the piece, according to each colour
39                 int lastMove;
40                 
41                 
42
43 };
44
45 /**
46  * Class to represent a board
47  */
48 class Board
49 {
50         public:
51                 Board(int width, int height);
52                 virtual ~Board();
53
54
55                 std::vector<Piece*> & GetPieces(const Piece::Colour & colour) {return colour == Piece::RED ? red : blue;} //retrieve array of pieces
56                 
57                 Piece * Get(int x, int y) const; //Retrieve single piece
58                 Piece *  Set(int x, int y, Piece * newPiece); //Add piece to board
59
60                 int Width() const {return width;}
61                 int Height() const {return height;}
62
63                 typedef enum {UP=0, DOWN=1, LEFT=2, RIGHT=3, NONE=4} Direction;
64                 static Direction StrToDir(const std::string & str);
65                 static void DirToStr(const Direction & dir, std::string & buffer);
66
67                 static void MoveInDirection(int & x, int & y, const Direction & dir, int multiplier = 1);
68                 static Direction DirectionBetween(int x1, int y1, int x2, int y2);
69
70                 
71
72                 static int redUnits[];
73                 static int blueUnits[];
74         
75
76                 bool ForgetPiece(Piece * forget); //removes piece from the red and blue vectors
77
78         private:
79                 friend class Forfax;
80
81                 int width;
82                 int height;
83                 Piece ** * board;
84
85                 std::vector<Piece*> red; //Store all red pieces
86                 std::vector<Piece*> blue; //Store all blue pieces
87         
88
89 };
90
91 /** 
92  * Small class to manage the Forfax AI
93  */
94 class Forfax
95 {
96         public:
97                 Forfax();
98                 virtual ~Forfax();
99                 bool Setup(); //Waits for input to determine colour and board size, and then responds with setup
100                 bool MakeMove(); //Should be called each turn - determines Forfax's move
101
102                 double CombatSuccessChance(Piece * attacker, Piece * defender, const Piece::Colour & accordingTo) const;
103                 double MovementBaseScore(Piece * move, const Board::Direction & dir, const Piece::Colour & accordingTo) const;
104                 double MovementTotalScore(Piece * move, const Board::Direction & dir, const Piece::Colour & accordingTo) const;
105
106         protected:
107                 bool MakeFirstMove(); //Should only be called on the first turn
108                 bool InterpretMove();
109         private:
110                 Board * board; //Forfax stores the state on a board
111                 Piece::Colour colour; //Forfax needs to know his colour
112                 std::string strColour; //String of colour
113                 int turnNumber; //Forfax needs to know what turn number it is
114                 
115                 int remainingUnits[14][2][2]; //Known remaining units, accessed by (type, colour, accordingTo)
116
117 };
118
119 class MovementChoice
120 {
121         public:
122                 MovementChoice(Piece * newPiece, const Board::Direction & newDir, const Forfax & forfax, const Piece::Colour & accordingTo) : piece(newPiece), dir(newDir) 
123                 {
124                         score = forfax.MovementBaseScore(piece, dir, accordingTo);
125                 }
126
127                 MovementChoice(const MovementChoice & cpy) : piece(cpy.piece), dir(cpy.dir), score(cpy.score)
128                 {
129
130                 }
131                 
132                 bool operator<(const MovementChoice & a) const {return score < a.score;}
133                 bool operator>(const MovementChoice & a) const {return score > a.score;}
134                 bool operator<=(const MovementChoice & a) const {return score <= a.score;}
135                 bool operator>=(const MovementChoice & a) const {return score >= a.score;}
136                 bool operator==(const MovementChoice & a) const {return score == a.score;}
137                 bool operator!=(const MovementChoice & a) const {return score != a.score;}
138
139                 Piece * piece;
140                 Board::Direction dir;
141                 double score;
142                                 
143         
144 };
145
146 class MovementTotalChoice : public MovementChoice
147 {
148         public:
149                 MovementTotalChoice(Piece * newPiece, const Board::Direction & newDir, const Forfax & forfax, const Piece::Colour & accordingTo) : MovementChoice(newPiece, newDir, forfax, accordingTo)
150                 {
151                         score = score/(forfax.MovementTotalScore(piece, dir, Piece::Opposite(accordingTo)));
152                 }
153 };
154
155 #endif //FORFAX_H
156
157 //EOF
158
159

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