Revamped manager program and added manual page
[progcomp2012.git] / manager / stratego.cpp
1
2
3 #include "stratego.h"
4
5 using namespace std;
6
7 /**
8  * Static variables
9  */
10
11 //nothing, boulder, flag, spy, scout, miner, sergeant, lietenant, captain, major, colonel, general, marshal, bomb, error
12 char  Piece::tokens[] = {'.','*','F','s','9','8','7','6','5','4','3','2','1','B','?'};
13 int Piece::maxUnits[] = {0,0,1,1,8,5,4,4,4,3,2,1,1,6,0};
14
15
16
17
18 Piece::TextureManager Piece::textures;
19
20
21
22
23 Piece::TextureManager::~TextureManager()
24 {
25         Array<Texture*>::Iterator i(*this);
26         while (i.Good())
27         {
28                 delete (*i);
29                 ++i;
30         }
31 }
32
33 Texture & Piece::TextureManager::operator[](const LUint & at)
34 {
35         while (Array<Texture*>::Size() <= at)
36         {
37                 char buffer[BUFSIZ];
38                 sprintf(buffer, "images/piece%lu.bmp", Array<Texture*>::Size());
39                 Array<Texture*>::Add(new Texture(buffer, false));
40                 
41         }
42         return *(Array<Texture*>::operator[](at));
43 }
44
45
46 /**
47  * Gets the type of a piece, based off a character token
48  * @param fromToken - character identifying the piece
49  * @returns The type of the piece
50  */
51 Piece::Type Piece::GetType(char fromToken)
52 {
53         for (int ii=0; ii <= (int)(Piece::BOMB); ++ii)
54         {
55                 if (tokens[ii] == fromToken)
56                 {
57                         return Type(Piece::NOTHING + ii);
58                 }
59         }
60         return Piece::BOULDER;
61 }
62
63 /**
64  * Construct a new, empty board
65  * @param newWidth - the width of the board
66  * @param newHeight - the height of the board
67  */
68 Board::Board(int newWidth, int newHeight) : winner(Piece::NONE), width(newWidth), height(newHeight), board(NULL)
69 {
70         board = new Piece**[width];
71         for (int x=0; x < width; ++x)
72         {
73                 board[x] = new Piece*[height];
74                 for (int y=0; y < height; ++y)
75                         board[x][y] = NULL;
76         }
77 }
78
79 /**
80  * Cleanup a board
81  */
82 Board::~Board()
83 {
84         for (int x=0; x < width; ++x)
85         {
86                 for (int y=0; y < height; ++y)
87                         delete board[x][y];
88                 delete [] board[x];
89         }
90 }
91
92 /**
93  * Print textual representation of the board to a stream
94  * @param stream - the stream to print information to
95  * @param reveal - Pieces matching this colour will have their identify revealed, other pieces will be shown as '#' or '*' for RED or BLUE respectively.
96  */
97 void Board::Print(FILE * stream, const Piece::Colour & reveal)
98 {
99         for (int y=0; y < height; ++y)
100         {
101                 for (int x=0; x < width; ++x)
102                 {
103                         Piece * piece = board[x][y];
104                         if (piece == NULL)
105                         {
106                                 fprintf(stream, ".");
107                         }
108                         else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
109                         {
110                                 fprintf(stream, "%c", Piece::tokens[piece->type]);
111                         }
112                         else
113                         {
114                                 switch (piece->colour)
115                                 {
116                                         case Piece::RED:
117                                                 fprintf(stream, "#");
118                                                 break;
119                                         case Piece::BLUE:
120                                                 fprintf(stream, "*");
121                                                 break;
122                                         case Piece::NONE:
123                                                 fprintf(stream, "+"); 
124                                                 break;
125                                         case Piece::BOTH:
126                                                 fprintf(stream, "$"); //Should never see these!
127                                                 break;
128                                 }
129                         }
130                 }
131                 fprintf(stream, "\n");
132         }
133         
134 }
135
136
137
138 /**
139  * Draw the board state to graphics
140  * @param reveal - Pieces matching this colour will be revealed. All others will be shown as blank coloured squares.
141  */
142 void Board::Draw(const Piece::Colour & reveal)
143 {
144         if (!Graphics::Initialised())
145         {
146                 fprintf(stderr, "ERROR - Board::Draw called whilst graphics disabled!!!\n");
147                 exit(EXIT_FAILURE);
148                 
149         }
150
151         Graphics::ClearScreen();
152         
153         for (int y=0; y < height; ++y)
154         {
155                 for (int x=0; x < width; ++x)
156                 {
157                         Piece * piece = board[x][y];
158                         if (piece == NULL)
159                         {
160                                 //Don't display anything
161
162                         }
163                         else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
164                         {
165                                 //Display the piece
166                                 Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
167                                 
168                         }
169                         else
170                         {
171                                 switch (piece->colour)
172                                 {
173                                         case Piece::RED:
174                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
175                                                 break;
176                                         case Piece::BLUE:
177                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
178                                                 break;
179                                         case Piece::NONE:
180                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
181                                                 break;
182                                         case Piece::BOTH:
183                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
184                                                 break;
185                                 }
186                         }
187                 }
188                 
189         }
190         Graphics::UpdateScreen();
191         
192 }
193
194 /**
195  * Adds a piece to the board
196  * @param x - x-coord to place the piece at, starting at zero, must be less than board width
197  * @param y - y-coord to place the piece at, starting at zero, must be less than board height
198  * @param newType - the Type of the piece
199  * @param newColour - the Colour of the piece
200  * @returns true if and only if the piece could be successfully added.
201  */
202 bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
203 {
204         if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
205                 return false;
206
207         Piece * piece = new Piece(newType, newColour);
208         board[x][y] = piece;
209         return true;
210 }
211
212 /**
213  * Gets a pointer to a piece at a board location
214  * UNUSED
215  * @param x - x-coord of the piece
216  * @param y - y-coord of the piece
217  * @returns pointer to the piece, or NULL if the board location was empty
218  * @throws error if board is null or coords are invalid
219  */
220 Piece * Board::GetPiece(int x, int y)
221 {
222         assert(board != NULL);
223         assert(x >= 0 && x < width && y >= 0 && y < height);
224         return board[x][y];
225 }
226
227 /**
228  * Moves a piece at a specified position in the specified direction, handles combat if necessary
229  * @param x - x-coord of the piece
230  * @param y - y-coord of the piece
231  * @param direction - Direction in which to move (UP, DOWN, LEFT or RIGHT)
232  * @param colour - Colour which the piece must match for the move to be valid
233  * @returns A MovementResult which indicates the result of the move - OK is good, VICTORY means that a flag was captured, anything else is an error
234  */
235 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
236 {
237         if (board == NULL) 
238         {
239                 return MovementResult(MovementResult::NO_BOARD);
240         }
241         if (!(x >= 0 && x < width && y >= 0 && y < height)) 
242         {
243                 return MovementResult(MovementResult::INVALID_POSITION);
244         }
245         Piece * target = board[x][y];
246         if (target == NULL) 
247         {
248                 return MovementResult(MovementResult::NO_SELECTION);
249         }
250         if (!(colour == Piece::NONE || target->colour == colour)) 
251         {
252                 return MovementResult(MovementResult::NOT_YOUR_UNIT);
253         }
254         if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER) 
255         {
256                 return MovementResult(MovementResult::IMMOBILE_UNIT);
257         }
258         if (multiplier > 1 && target->type != Piece::SCOUT)
259         {
260                 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
261         }
262         int x2 = x; int y2 = y;
263
264         for (int ii=0; ii < multiplier; ++ii)
265         {
266                 switch (direction)
267                 {
268                         case UP:
269                                 --y2;
270                                 break;
271                         case DOWN:
272                                 ++y2;
273                                 break;
274                         case LEFT:
275                                 --x2;
276                                 break;
277                         case RIGHT:
278                                 ++x2;
279                                 break;
280                 }
281                 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height)) 
282                 {
283                         return MovementResult(MovementResult::INVALID_DIRECTION);
284                 }
285                 if (ii < multiplier-1 && board[x2][y2] != NULL)
286                 {
287                         return MovementResult(MovementResult::POSITION_FULL);
288                 }
289         }
290         Piece * defender = board[x2][y2];
291         if (defender == NULL)
292         {
293                 board[x][y] = NULL;
294                 board[x2][y2] = target;
295         }
296         else if (defender->colour != target->colour)
297         {
298                 Piece::Type defenderType = defender->type;
299                 Piece::Type attackerType = target->type;
300
301                 if (defender->colour == Piece::NONE) 
302                 {
303                         return MovementResult(MovementResult::POSITION_FULL);
304                 }
305                 if (defender->type == Piece::FLAG)
306                 {
307                         winner = target->colour;
308                         return MovementResult(MovementResult::VICTORY);
309                 }
310                 else if (defender->type == Piece::BOMB)
311                 {
312                         if (target->type == Piece::MINER)
313                         {
314
315                                 delete defender;
316                                 board[x][y] = NULL;
317                                 board[x2][y2] = target;
318                                 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
319                         }
320                         else
321                         {
322                                 delete defender;
323                                 delete target;
324                                 board[x][y] = NULL;
325                                 board[x2][y2] = NULL;
326                                 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
327                         }
328                 }
329                 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
330                 {
331                         delete defender;
332                         board[x][y] = NULL;
333                         board[x2][y2] = target;
334                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
335                 }
336                 else if (target->operator > (*defender))
337                 {
338                         delete defender;
339                         board[x][y] = NULL;
340                         board[x2][y2] = target;
341                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
342                 }
343                 else if (target->operator==(*defender) && rand() % 2 == 0)
344                 {
345                         delete defender;
346                         board[x][y] = NULL;
347                         board[x2][y2] = target; 
348                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
349                 }
350                 else
351                 {
352                         delete target;
353                         board[x][y] = NULL;
354                         return MovementResult(MovementResult::DIES, attackerType, defenderType);
355                 }
356         }
357         else
358         {
359                 return MovementResult(MovementResult::POSITION_FULL);
360         }
361         return MovementResult(MovementResult::OK);
362 }       
363
364
365

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