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
7 * @git git.ucc.asn.au/progcomp2012.git
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
20 class Board; //Forward declaration used by class Piece
23 * Class to represent a piece on the board
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.
31 Piece(int newX, int newY,const Colour & newColour);
32 Piece(int newX, int newY,const Colour & newColour, const Type & fixedRank);
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;}
39 static char tokens[]; //The tokens used to identify various pieces
40 static int maxUnits[]; //The maximum allowed number of units of each piece
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;}
46 if (minRank == maxRank)
47 return (minRank != Piece::FLAG && minRank != Piece::BOMB);
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
64 * Class to represent a board
69 Board(int width, int height);
73 std::vector<Piece*> & GetPieces(const Piece::Colour & colour) {return colour == Piece::RED ? red : blue;} //retrieve array of pieces
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
79 bool ValidPosition(int x, int y) const {return (x > 0 && x < width && y > 0 && y < height);}
81 int Width() const {return width;}
82 int Height() const {return height;}
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);
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);
93 static int redUnits[];
94 static int blueUnits[];
97 bool ForgetPiece(Piece * forget); //removes piece from the red and blue vectors
106 std::vector<Piece*> red; //Store all red pieces
107 std::vector<Piece*> blue; //Store all blue pieces
113 * Class to manage the Forfax AI
121 typedef enum {OK, NO_NEWLINE, EXPECTED_ATTACKER, UNEXPECTED_DEFENDER, NO_ATTACKER, NO_DEFENDER, COLOUR_MISMATCH, INVALID_QUERY, BOARD_ERROR, VICTORY} Status;
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
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
136 void PrintBoard(std::ostream & out);
139 Status MakeFirstMove(); //Should only be called on the first turn
140 Status InterpretMove();
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
147 static int remainingUnits[2][15]; //Known remaining units, accessed by [colour][type]
152 * Helper class used to store various moves in the board, and their associated scores
157 MovementChoice(Piece * newPiece, const Board::Direction & newDir, const Forfax & forfax) : piece(newPiece), dir(newDir)
159 score = forfax.MovementScore(piece, dir);
162 MovementChoice(const MovementChoice & cpy) : piece(cpy.piece), dir(cpy.dir), score(cpy.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 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;}
175 Board::Direction dir;