basic_cpp.h 4.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * "basic_cpp", a sample Stratego AI for the UCC Programming Competition 2012
 * Declarations for classes Piece, Board and Basic_Cpp
 * @author Sam Moore (matches) [SZM]
 * @website http://matches.ucc.asn.au/stratego
 * @email [email protected] or [email protected]
 * @git git.ucc.asn.au/progcomp2012.git
 */

#ifndef BASIC_CPP_H
#define BASIC_CPP_H

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cassert>





/**
 * enum for possible ranks of pieces
 */
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;

/**
 * enum for possible colours of pieces and the AI
 */		
typedef enum {RED=0, BLUE=1, NONE, BOTH} Colour;


/**
 * Class to represent a piece on the board
 * TODO: Add features required for Pieces as used by your AI
 * 	For example, can replace the single member "rank" with two, "maxRank" and "minRank" 
 *	Or add an array of probability values for EVERY rank!
 */
class Piece
{
	public:
		static  char tokens[]; //The tokens used to identify various pieces

		Piece(int newX, int newY,const Colour & newColour, const Rank & newRank = UNKNOWN)
			: x(newX), y(newY), colour(newColour), rank(newRank) {}
		virtual ~Piece() {}

		bool Mobile() const {return rank != BOMB && rank != FLAG;}
		
		static Colour Opposite(const Colour & colour) {return colour == RED ? BLUE : RED;}

		int x; int y;
		const Colour colour; //The colour of the piece
		Rank rank; //The rank of the piece

		static Rank GetRank(char token); //Helper to get rank from character

};

/**
 * enum for Directions that a piece can move in
 */
typedef enum {UP=0, DOWN=1, LEFT=2, RIGHT=3, DIRECTION_ERROR=4} Direction;

/**
 * Class to represent a board
 */
class Board
{
	public:
		Board(int width, int height); //Construct a board with width and height
		virtual ~Board(); //Destroy the board

		Piece * Get(int x, int y) const; //Retrieve single piece
		Piece *  Set(int x, int y, Piece * newPiece); //Add piece to board

		int Width() const {return width;} //getter for width
		int Height() const {return height;} //getter for height

		bool ValidPosition(int x, int y) const {return (x >= 0 && x < width && y >= 0 && y < height);} //Helper - is position within board?
	private:

		int width; //The width of the board
		int height; //The height of the board
		Piece ** * board; //The pieces on the board
};

/**
 * Basic AI class
 * TODO: Make sure that if Piece is changed, BasicAI is updated to be consistent
 * TODO: More complex AI's should inherit off this class.
 *	It is recommended that only MakeMove is altered - hence the other functions are not virtual.
 */
class BasicAI
{
	public:
		BasicAI(); //Constructor
		virtual ~BasicAI(); //Destructor

		bool Setup(); //Implements setup protocol
		bool MoveCycle(); //Implements the MoveCycle protocol
		bool ReadBoard(); //Reads the board as part of the MoveCycle protocol
		bool InterpretResult(); //Interprets the result of a move
		virtual bool MakeMove(); //Makes a move - defaults to randomised moves
		bool DebugPrintBoard(); //DEBUG - Prints the board to stderr
	protected:
		int turn;
		Board * board; //The board
		std::vector<Piece*> units; //Allied units
		std::vector<Piece*> enemyUnits; //Enemy units

		bool ForgetUnit(Piece * forget); //Delete and forget about a unit

		Colour colour;
		std::string colourStr;
};

/**
 * Purely static class of Helper functions
 */
class Helper
{
	public:
		static Direction StrToDir(const std::string & str); //Helper - Convert string to a Direction enum
		static void DirToStr(const Direction & dir, std::string & buffer); //Helper - Convert Direction enum to a string
		static void MoveInDirection(int & x, int & y, const Direction & dir, int multiplier = 1); //Helper - Move a point in a direction
		static int Tokenise(std::vector<std::string> & buffer, std::string & str, char split = ' '); //Helper - Split a string into tokens
		static int Integer(std::string & fromStr); //Helper - convert a string to an integer
		static void ReadLine(std::string & buffer); //Helper - read a line from stdin
	private:
		//By making these private we ensure that no one can create an instance of Helper
		//Yes we could use namespaces instead, but I prefer this method because you can use private static member variables
		//Not that I am. But you can.
		Helper() {}
		~Helper() {}
};



#endif //BASIC_CPP_H

//EOF