fe73aa13ff0b4311c3d10acea02c493f9b6dd610
[progcomp2012.git] / home / progcomp / judge / manager / stratego.h
1 #ifndef STRATEGO_H
2 #define STRATEGO_H
3
4 #include <stdlib.h>
5 #include <stdio.h>
6
7 #include <assert.h>
8
9
10         #include "graphics.h"
11         #include "array.h"
12
13 #include <vector>
14
15 /**
16  * Contains classes for a game of Stratego
17  */
18
19
20 /**
21  * Class for a game piece
22  */
23 class Piece
24 {
25         public:
26                 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
27                 
28
29         
30                 typedef enum {RED=0, BLUE=1, NONE=2, BOTH=3} Colour; //Used for the allegiance of the pieces - terrain counts as NONE.
31
32                 Piece(const Type & newType, const Colour & newColour) : type(newType), colour(newColour), beenRevealed(false) {}
33                 virtual ~Piece() {}
34
35
36                 //Operators compare the strength of two pieces
37                 bool operator==(const Piece & equ) const {return type == equ.type;}
38                 bool operator<(const Piece & equ) const {return type < equ.type;}
39                 bool operator>(const Piece & equ) const {return type > equ.type;}
40                 
41                 bool operator!=(const Piece & equ) const {return !operator==(equ);}
42                 bool operator<=(const Piece & equ) const {return (operator<(equ) || operator==(equ));}
43                 bool operator>=(const Piece & equ) const {return (operator>(equ) || operator==(equ));}
44
45                 //Contains the characters used to identify piece types when the board is printed to a stream
46                 static  char tokens[];
47                 static int maxUnits[];
48
49                 static Type GetType(char fromToken);
50
51                 int PieceValue() const {if (type == BOMB || type == FLAG) {return 0;} return (int)(type) - (int)(SPY) + 1;}
52
53                 //Attributes of the piece
54                 const Type type; 
55                 const Colour colour;
56
57                 bool beenRevealed;
58
59                 public:
60
61                         class TextureManager : public Graphics::TextureManager<LUint>, private Array<Texture*>
62                         {
63                                 public:
64                                         TextureManager() : Graphics::TextureManager<LUint>(), Array<Texture*>() {}
65                                         virtual ~TextureManager();
66
67                                         virtual Texture & operator[](const LUint & at);
68                         };
69                         static TextureManager textures;
70
71                         static Graphics::Colour GetGraphicsColour(const Piece::Colour & colour)
72                         {
73                                 switch (colour)
74                                 {
75                                         case RED:
76                                                 return Graphics::Colour(1,0,0);
77                                                 break;
78                                         case BLUE:
79                                                 return Graphics::Colour(0,0,1);
80                                                 break;
81                                         case NONE:
82                                                 return Graphics::Colour(0.5,0.5,0.5);
83                                                 break;
84                                         case BOTH:
85                                                 return Graphics::Colour(1,0,1);
86                                                 break;
87                                 }
88                         }
89
90                         
91                         
92
93                 
94 };
95
96 #include "movementresult.h"
97
98 /**
99  * A Stratego board
100  */
101 class Board
102 {
103         public:
104                 Board(int newWidth, int newHeight); //Constructor
105
106                 virtual ~Board(); //Destructor
107
108                 void Print(FILE * stream, const Piece::Colour & reveal=Piece::BOTH); //Print board
109                 void PrintPretty(FILE * stream, const Piece::Colour & reveal=Piece::BOTH); //Print board using colour
110                 
111                 void Draw(const Piece::Colour & reveal=Piece::BOTH, bool showRevealed = true); //Draw board
112                 
113
114                 bool AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour); //Add piece to board
115
116
117                 Piece * GetPiece(int x, int y); //Retrieve piece from a location on the board
118
119
120                 typedef enum {UP, DOWN, LEFT, RIGHT} Direction;
121
122                 
123                 
124                 static bool LegalResult(const MovementResult & result)
125                 {
126                         return (result == MovementResult::OK || result == MovementResult::DIES || result == MovementResult::KILLS || result == MovementResult::BOTH_DIE || result == MovementResult::VICTORY || result == MovementResult::DRAW || result == MovementResult::DRAW_DEFAULT || result == MovementResult::SURRENDER);
127                 }
128
129                 static bool HaltResult(const MovementResult & result)
130                 {
131                         return (result == MovementResult::VICTORY || result == MovementResult::DRAW || result == MovementResult::DRAW_DEFAULT || result == MovementResult::SURRENDER || !LegalResult(result));
132                 }               
133
134                 MovementResult MovePiece(int x, int y, const Direction & direction, int multiplier=1,const Piece::Colour & colour=Piece::NONE); //Move piece from position in direction
135         
136
137                 Piece::Colour winner;
138
139                 int Width() const {return width;}
140                 int Height() const {return height;}
141
142                 int MobilePieces(const Piece::Colour & colour) const;
143                 int TotalPieceValue(const Piece::Colour & colour) const;
144                 bool RemovePiece(Piece * piece);
145         private:
146                 int width;
147                 int height;
148                 Piece ** * board;
149                 std::vector<Piece*> pieces;
150 };
151
152 #endif //STRATEGO_H
153
154 //EOF
155
156

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