Changed the directory structure.
[progcomp2012.git] / home / progcomp / judge / 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), pieces()
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 '#'
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
111                                 fprintf(stream, "%c", Piece::tokens[piece->type]);
112
113
114                         }
115                         else
116                         {
117                                 switch (piece->colour)
118                                 {
119                                         case Piece::RED:
120                                         case Piece::BLUE:
121                                                 fprintf(stream, "#");
122                                                 break;
123                                         case Piece::NONE:
124                                                 fprintf(stream, "+");
125                                                 break;
126                                         case Piece::BOTH:
127                                                 fprintf(stream, "$");
128                                                 break;
129                                 }
130                         }
131                 }
132                 fprintf(stream, "\n");
133         }
134         
135 }
136
137 /**
138  * Print textual representation of the board to a stream
139  * @param stream - the stream to print information to
140  * @param reveal - Pieces matching this colour will have their identify revealed, other pieces will be shown as '#'
141  */
142 void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal)
143 {
144         for (int y=0; y < height; ++y)
145         {
146                 for (int x=0; x < width; ++x)
147                 {
148                         Piece * piece = board[x][y];
149                         if (piece == NULL)
150                         {
151                                 fprintf(stream, ".");
152                         }
153                         else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
154                         {
155                                 switch (piece->colour)  
156                                 {
157                                         case Piece::RED:
158                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,40);
159                                                 break;
160                                         case Piece::BLUE:
161                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,40);
162                                                 break;
163                                         default:
164                                                 break;
165                                 }
166                                 fprintf(stream, "%c", Piece::tokens[piece->type]);
167
168                         }
169                         else
170                         {
171                                 switch (piece->colour)
172                                 {
173                                         case Piece::RED:
174                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,41);
175
176                                                 break;
177                                         case Piece::BLUE:
178                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,44);
179                                                 break;
180                                         case Piece::NONE:
181                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,37,47);
182                                                 break;
183                                         case Piece::BOTH:
184                                                 //Should never see this
185                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,33,43);
186                                                 break;
187
188                                 }       
189                                 fprintf(stream, "#");
190                                 
191                         }
192                         fprintf(stream, "%c[%d;%d;%dm",0x1B,0,7,0);
193                 }
194                 fprintf(stream, "\n");
195         }
196         
197 }
198
199
200
201 /**
202  * Draw the board state to graphics
203  * @param reveal - Pieces matching this colour will be revealed. All others will be shown as blank coloured squares.
204  */
205 void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
206 {
207         if (!Graphics::Initialised())
208         {
209                 fprintf(stderr, "ERROR - Board::Draw called whilst graphics disabled!!!\n");
210                 exit(EXIT_FAILURE);
211                 
212         }
213
214         Graphics::ClearScreen();
215         
216         for (int y=0; y < height; ++y)
217         {
218                 for (int x=0; x < width; ++x)
219                 {
220                         Piece * piece = board[x][y];
221                         if (piece == NULL)
222                         {
223                                 //Don't display anything
224
225                         }
226                         else if ((piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
227                                         || (piece->beenRevealed && showRevealed))
228                         {
229                                 //Display the piece
230                                 Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
231                                 
232                         }
233                         else
234                         {
235                                 switch (piece->colour)
236                                 {
237                                         case Piece::RED:
238                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
239                                                 break;
240                                         case Piece::BLUE:
241                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
242                                                 break;
243                                         case Piece::NONE:
244                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
245                                                 break;
246                                         case Piece::BOTH:
247                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
248                                                 break;
249                                 }
250                         }
251                 }
252                 
253         }
254         Graphics::UpdateScreen();
255         
256 }
257
258 /**
259  * Adds a piece to the board
260  * @param x - x-coord to place the piece at, starting at zero, must be less than board width
261  * @param y - y-coord to place the piece at, starting at zero, must be less than board height
262  * @param newType - the Type of the piece
263  * @param newColour - the Colour of the piece
264  * @returns true if and only if the piece could be successfully added.
265  */
266 bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
267 {
268         if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
269                 return false;
270
271         Piece * piece = new Piece(newType, newColour);
272         board[x][y] = piece;
273
274         pieces.push_back(piece);
275         return true;
276 }
277
278 /**
279  * Gets a pointer to a piece at a board location
280  * UNUSED
281  * @param x - x-coord of the piece
282  * @param y - y-coord of the piece
283  * @returns pointer to the piece, or NULL if the board location was empty
284  * @throws error if board is null or coords are invalid
285  */
286 Piece * Board::GetPiece(int x, int y)
287 {
288         assert(board != NULL);
289         assert(x >= 0 && x < width && y >= 0 && y < height);
290         return board[x][y];
291 }
292
293 /**
294  * Moves a piece at a specified position in the specified direction, handles combat if necessary
295  * @param x - x-coord of the piece
296  * @param y - y-coord of the piece
297  * @param direction - Direction in which to move (UP, DOWN, LEFT or RIGHT)
298  * @param colour - Colour which the piece must match for the move to be valid
299  * @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
300  */
301 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
302 {
303         if (board == NULL) 
304         {
305                 return MovementResult(MovementResult::NO_BOARD);
306         }
307         if (!(x >= 0 && x < width && y >= 0 && y < height)) 
308         {
309                 return MovementResult(MovementResult::INVALID_POSITION);
310         }
311         Piece * target = board[x][y];
312         if (target == NULL) 
313         {
314                 return MovementResult(MovementResult::NO_SELECTION);
315         }
316         if (!(colour == Piece::NONE || target->colour == colour)) 
317         {
318                 return MovementResult(MovementResult::NOT_YOUR_UNIT);
319         }
320         if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER) 
321         {
322                 return MovementResult(MovementResult::IMMOBILE_UNIT);
323         }
324         if (multiplier > 1 && target->type != Piece::SCOUT)
325         {
326                 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
327         }
328         int x2 = x; int y2 = y;
329
330         for (int ii=0; ii < multiplier; ++ii)
331         {
332                 switch (direction)
333                 {
334                         case UP:
335                                 --y2;
336                                 break;
337                         case DOWN:
338                                 ++y2;
339                                 break;
340                         case LEFT:
341                                 --x2;
342                                 break;
343                         case RIGHT:
344                                 ++x2;
345                                 break;
346                 }
347                 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height)) 
348                 {
349                         return MovementResult(MovementResult::INVALID_DIRECTION);
350                 }
351                 if (ii < multiplier-1 && board[x2][y2] != NULL)
352                 {
353                         return MovementResult(MovementResult::POSITION_FULL);
354                 }
355         }
356         Piece * defender = board[x2][y2];
357         if (defender == NULL)
358         {
359                 board[x][y] = NULL;
360                 board[x2][y2] = target;
361         }
362         else if (defender->colour != target->colour)
363         {
364                 defender->beenRevealed = true;
365                 target->beenRevealed = true;
366
367                 Piece::Type defenderType = defender->type;
368                 Piece::Type attackerType = target->type;
369
370                 if (defender->colour == Piece::NONE) 
371                 {
372                         return MovementResult(MovementResult::POSITION_FULL);
373                 }
374                 if (defender->type == Piece::FLAG)
375                 {
376                         winner = target->colour;
377                         return MovementResult(MovementResult::VICTORY);
378                 }
379                 else if (defender->type == Piece::BOMB)
380                 {
381                         if (target->type == Piece::MINER)
382                         {
383                                 RemovePiece(defender);
384                                 delete defender;
385                                 board[x][y] = NULL;
386                                 board[x2][y2] = target;
387                                 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
388                         }
389                         else
390                         {
391                                 RemovePiece(defender);
392                                 RemovePiece(target);
393                                 delete defender;
394                                 delete target;
395                                 board[x][y] = NULL;
396                                 board[x2][y2] = NULL;
397                                 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
398                         }
399                 }
400                 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
401                 {
402                         RemovePiece(defender);
403                         delete defender;
404                         board[x][y] = NULL;
405                         board[x2][y2] = target;
406                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
407                 }
408                 else if (target->operator > (*defender))
409                 {
410                         RemovePiece(defender);
411                         delete defender;
412                         board[x][y] = NULL;
413                         board[x2][y2] = target;
414                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
415                 }
416                 else if (target->operator==(*defender) && rand() % 2 == 0)
417                 {
418                         RemovePiece(defender);
419                         delete defender;
420                         board[x][y] = NULL;
421                         board[x2][y2] = target; 
422                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
423                 }
424                 else
425                 {
426                         RemovePiece(target);
427                         delete target;
428                         board[x][y] = NULL;
429                         return MovementResult(MovementResult::DIES, attackerType, defenderType);
430                 }
431         }
432         else
433         {
434                 return MovementResult(MovementResult::POSITION_FULL);
435         }
436         return MovementResult(MovementResult::OK);
437 }       
438
439 /**
440  * Removes a piece from the board
441  * @param piece The piece to remove
442  * @returns true iff the piece actually existed
443  */
444 bool Board::RemovePiece(Piece * piece)
445 {
446         bool result = false;
447         for (int x = 0; x < width; ++x)
448         {
449                 for (int y = 0; y < height; ++y)        
450                 {
451                         if (board[x][y] == piece)
452                         {
453                                 result = true;
454                                 board[x][y] = NULL;
455                         }
456                 }
457         }
458
459         vector<Piece*>::iterator i = pieces.begin();
460         while (i != pieces.end())
461         {
462                 if ((*i) == piece)
463                 {
464                         i = pieces.erase(i);
465                         result = true;
466                         continue;
467                 }
468                 ++i;
469         }
470         return result;
471 }
472
473 /**
474  * Returns the total value of pieces belonging to colour
475  * @param colour the colour
476  * @returns the total value of pieces belonging to colour.
477  *      (Redundant repetition <3)
478  */
479 int Board::TotalPieceValue(const Piece::Colour & colour) const
480 {
481         int result = 0;
482         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
483         {
484                 if ((*i)->colour == colour || colour == Piece::BOTH)
485                 {
486                         result += (*i)->PieceValue();
487                 }
488         }
489         return result;
490 }
491
492 /**
493  * Returns the total number of mobile pieces belonging to colour
494  * @param colour the colour
495  * @returns the total value of mobile pieces belonging to colour.
496  *      (Redundant repetition <3)
497  */
498 int Board::MobilePieces(const Piece::Colour & colour) const
499 {
500         int result = 0;
501         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
502         {
503                 if ((*i)->colour == colour || colour == Piece::BOTH)
504                 {
505                         if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)
506                                 result++;
507                 }
508         }
509         return result;
510 }
511
512

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