Changed directory structure (again)
[progcomp2012.git] / agents / basic_cpp / basic_cpp.h
1 /**
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 [email protected] or [email protected]
7  * @git git.ucc.asn.au/progcomp2012.git
8  */
9
10 #ifndef BASIC_CPP_H
11 #define BASIC_CPP_H
12
13 #include <cstdlib>
14 #include <iostream>
15 #include <string>
16 #include <sstream>
17 #include <vector>
18 #include <cassert>
19
20
21
22
23
24 /**
25  * enum for possible ranks of pieces
26  */
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;
28
29 /**
30  * enum for possible colours of pieces and the AI
31  */             
32 typedef enum {RED=0, BLUE=1, NONE, BOTH} Colour;
33
34
35 /**
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!
40  */
41 class Piece
42 {
43         public:
44                 static  char tokens[]; //The tokens used to identify various pieces
45
46                 Piece(int newX, int newY,const Colour & newColour, const Rank & newRank = UNKNOWN)
47                         : x(newX), y(newY), colour(newColour), rank(newRank) {}
48                 virtual ~Piece() {}
49
50                 bool Mobile() const {return rank != BOMB && rank != FLAG;}
51                 
52                 static Colour Opposite(const Colour & colour) {return colour == RED ? BLUE : RED;}
53
54                 int x; int y;
55                 const Colour colour; //The colour of the piece
56                 Rank rank; //The rank of the piece
57
58                 static Rank GetRank(char token); //Helper to get rank from character
59
60 };
61
62 /**
63  * enum for Directions that a piece can move in
64  */
65 typedef enum {UP=0, DOWN=1, LEFT=2, RIGHT=3, DIRECTION_ERROR=4} Direction;
66
67 /**
68  * Class to represent a board
69  */
70 class Board
71 {
72         public:
73                 Board(int width, int height); //Construct a board with width and height
74                 virtual ~Board(); //Destroy the board
75
76                 Piece * Get(int x, int y) const; //Retrieve single piece
77                 Piece *  Set(int x, int y, Piece * newPiece); //Add piece to board
78
79                 int Width() const {return width;} //getter for width
80                 int Height() const {return height;} //getter for height
81
82                 bool ValidPosition(int x, int y) const {return (x >= 0 && x < width && y >= 0 && y < height);} //Helper - is position within board?
83         private:
84
85                 int width; //The width of the board
86                 int height; //The height of the board
87                 Piece ** * board; //The pieces on the board
88 };
89
90 /**
91  * Basic AI class
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.
95  */
96 class BasicAI
97 {
98         public:
99                 BasicAI(); //Constructor
100                 virtual ~BasicAI(); //Destructor
101
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
108         protected:
109                 int turn;
110                 Board * board; //The board
111                 std::vector<Piece*> units; //Allied units
112                 std::vector<Piece*> enemyUnits; //Enemy units
113
114                 bool ForgetUnit(Piece * forget); //Delete and forget about a unit
115
116                 Colour colour;
117                 std::string colourStr;
118 };
119
120 /**
121  * Purely static class of Helper functions
122  */
123 class Helper
124 {
125         public:
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
132         private:
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.
136                 Helper() {}
137                 ~Helper() {}
138 };
139
140
141
142 #endif //BASIC_CPP_H
143
144 //EOF
145

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