4 #include <vector> //Uses C++ std::vectors to store pieces
5 #include <string> //Uses C++ std::string
7 * Header for the sample Stratego AI "forfax"
8 * @author Sam Moore 2011
12 * Class to represent a piece on the board
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.
20 Piece(int newX, int newY,const Colour & newColour);
21 Piece(int newX, int newY,const Colour & newColour, const Colour & rankKnownBy, const Type & fixedRank);
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;}
28 static char tokens[]; //The tokens used to identify various pieces
29 static int maxUnits[]; //The maximum allowed number of units of each piece
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;}
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
46 * Class to represent a board
51 Board(int width, int height);
55 std::vector<Piece*> & GetPieces(const Piece::Colour & colour) {return colour == Piece::RED ? red : blue;} //retrieve array of pieces
57 Piece * Get(int x, int y) const; //Retrieve single piece
58 Piece * Set(int x, int y, Piece * newPiece); //Add piece to board
60 int Width() const {return width;}
61 int Height() const {return height;}
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);
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);
72 static int redUnits[];
73 static int blueUnits[];
76 bool ForgetPiece(Piece * forget); //removes piece from the red and blue vectors
85 std::vector<Piece*> red; //Store all red pieces
86 std::vector<Piece*> blue; //Store all blue pieces
92 * Small class to manage the Forfax AI
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
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;
107 bool MakeFirstMove(); //Should only be called on the first turn
108 bool InterpretMove();
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
115 int remainingUnits[14][2][2]; //Known remaining units, accessed by (type, colour, accordingTo)
122 MovementChoice(Piece * newPiece, const Board::Direction & newDir, const Forfax & forfax, const Piece::Colour & accordingTo) : piece(newPiece), dir(newDir)
124 score = forfax.MovementBaseScore(piece, dir, accordingTo);
127 MovementChoice(const MovementChoice & cpy) : piece(cpy.piece), dir(cpy.dir), score(cpy.score)
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;}
140 Board::Direction dir;
146 class MovementTotalChoice : public MovementChoice
149 MovementTotalChoice(Piece * newPiece, const Board::Direction & newDir, const Forfax & forfax, const Piece::Colour & accordingTo) : MovementChoice(newPiece, newDir, forfax, accordingTo)
151 score = score/(forfax.MovementTotalScore(piece, dir, Piece::Opposite(accordingTo)));