2 * "basic_cpp", a sample Stratego AI for the UCC Programming Competition 2012
3 * Declarations for classes Piece, Board and Basic_Cpp
4 * @author Sam Moore (matches) [SZM]
5 * @website http://matches.ucc.asn.au/stratego
6 * @email progcomp@ucc.asn.au or matches@ucc.asn.au
7 * @git git.ucc.asn.au/progcomp2012.git
25 * enum for possible ranks of pieces
27 typedef enum {UNKNOWN=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} Rank;
30 * enum for possible colours of pieces and the AI
32 typedef enum {RED=0, BLUE=1, NONE, BOTH} Colour;
36 * Class to represent a piece on the board
37 * TODO: Add features required for Pieces as used by your AI
38 * For example, can replace the single member "rank" with two, "maxRank" and "minRank"
39 * Or add an array of probability values for EVERY rank!
44 static char tokens[]; //The tokens used to identify various pieces
46 Piece(int newX, int newY,const Colour & newColour, const Rank & newRank = UNKNOWN)
47 : x(newX), y(newY), colour(newColour), rank(newRank) {}
50 bool Mobile() const {return rank != BOMB && rank != FLAG;}
52 static Colour Opposite(const Colour & colour) {return colour == RED ? BLUE : RED;}
55 const Colour colour; //The colour of the piece
56 Rank rank; //The rank of the piece
58 static Rank GetRank(char token); //Helper to get rank from character
63 * enum for Directions that a piece can move in
65 typedef enum {UP=0, DOWN=1, LEFT=2, RIGHT=3, DIRECTION_ERROR=4} Direction;
68 * Class to represent a board
73 Board(int width, int height); //Construct a board with width and height
74 virtual ~Board(); //Destroy the board
76 Piece * Get(int x, int y) const; //Retrieve single piece
77 Piece * Set(int x, int y, Piece * newPiece); //Add piece to board
79 int Width() const {return width;} //getter for width
80 int Height() const {return height;} //getter for height
82 bool ValidPosition(int x, int y) const {return (x >= 0 && x < width && y >= 0 && y < height);} //Helper - is position within board?
85 int width; //The width of the board
86 int height; //The height of the board
87 Piece ** * board; //The pieces on the board
92 * TODO: Make sure that if Piece is changed, BasicAI is updated to be consistent
93 * TODO: More complex AI's should inherit off this class.
94 * It is recommended that only MakeMove is altered - hence the other functions are not virtual.
99 BasicAI(); //Constructor
100 virtual ~BasicAI(); //Destructor
102 bool Setup(); //Implements setup protocol
103 bool MoveCycle(); //Implements the MoveCycle protocol
104 bool ReadBoard(); //Reads the board as part of the MoveCycle protocol
105 bool InterpretResult(); //Interprets the result of a move
106 virtual bool MakeMove(); //Makes a move - defaults to randomised moves
107 bool DebugPrintBoard(); //DEBUG - Prints the board to stderr
110 Board * board; //The board
111 std::vector<Piece*> units; //Allied units
112 std::vector<Piece*> enemyUnits; //Enemy units
114 bool ForgetUnit(Piece * forget); //Delete and forget about a unit
117 std::string colourStr;
121 * Purely static class of Helper functions
126 static Direction StrToDir(const std::string & str); //Helper - Convert string to a Direction enum
127 static void DirToStr(const Direction & dir, std::string & buffer); //Helper - Convert Direction enum to a string
128 static void MoveInDirection(int & x, int & y, const Direction & dir, int multiplier = 1); //Helper - Move a point in a direction
129 static int Tokenise(std::vector<std::string> & buffer, std::string & str, char split = ' '); //Helper - Split a string into tokens
130 static int Integer(std::string & fromStr); //Helper - convert a string to an integer
131 static void ReadLine(std::string & buffer); //Helper - read a line from stdin
133 //By making these private we ensure that no one can create an instance of Helper
134 //Yes we could use namespaces instead, but I prefer this method because you can use private static member variables
135 //Not that I am. But you can.