Added check for rubbish move multipliers
[progcomp2012.git] / 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 #ifdef BUILD_GRAPHICS
18 Piece::TextureManager Piece::textures;
19
20 Piece::TextureManager::~TextureManager()
21 {
22         Array<Texture*>::Iterator i(*this);
23         while (i.Good())
24         {
25                 delete (*i);
26                 ++i;
27         }
28 }
29
30 Texture & Piece::TextureManager::operator[](const LUint & at)
31 {
32         while (Array<Texture*>::Size() <= at)
33         {
34                 char buffer[BUFSIZ];
35                 sprintf(buffer, "images/piece%lu.bmp", Array<Texture*>::Size());
36                 Array<Texture*>::Add(new Texture(buffer, false));
37                 
38         }
39         return *(Array<Texture*>::operator[](at));
40 }
41 #endif //BUILD_GRAPHICS
42
43 /**
44  * Gets the type of a piece, based off a character token
45  * @param fromToken - character identifying the piece
46  * @returns The type of the piece
47  */
48 Piece::Type Piece::GetType(char fromToken)
49 {
50         for (int ii=0; ii <= (int)(Piece::BOMB); ++ii)
51         {
52                 if (tokens[ii] == fromToken)
53                 {
54                         return Type(Piece::NOTHING + ii);
55                 }
56         }
57         return Piece::BOULDER;
58 }
59
60 /**
61  * Gets the opposite to the indicated colour
62  */
63 Piece::Colour Piece::OppositeColour(const Colour & colour)
64 {
65         switch (colour)
66         {
67                 case Piece::RED:
68                         return Piece::BLUE;
69                         break;
70                 case Piece::BLUE:
71                         return Piece::RED;
72                         break;
73                 case Piece::BOTH:
74                         return Piece::BOTH;
75                         break;
76                 case Piece::NONE:
77                         return Piece::NONE;
78         }
79 }
80
81 /**
82  * Construct a new, empty board
83  * @param newWidth - the width of the board
84  * @param newHeight - the height of the board
85  */
86 Board::Board(int newWidth, int newHeight) : winner(Piece::NONE), width(newWidth), height(newHeight), board(NULL), pieces()
87 {
88         board = new Piece**[width];
89         for (int x=0; x < width; ++x)
90         {
91                 board[x] = new Piece*[height];
92                 for (int y=0; y < height; ++y)
93                         board[x][y] = NULL;
94         }
95 }
96
97 /**
98  * Cleanup a board
99  */
100 Board::~Board()
101 {
102         for (int x=0; x < width; ++x)
103         {
104                 for (int y=0; y < height; ++y)
105                         delete board[x][y];
106                 delete [] board[x];
107         }
108 }
109
110 /**
111  * Print textual representation of the board to a stream
112  * @param stream - the stream to print information to
113  * @param reveal - Pieces matching this colour will have their identify revealed, other pieces will be shown as '#'
114  */
115 void Board::Print(FILE * stream, const Piece::Colour & reveal)
116 {
117         for (int y=0; y < height; ++y)
118         {
119                 for (int x=0; x < width; ++x)
120                 {
121                         Piece * piece = board[x][y];
122                         if (piece == NULL)
123                         {
124                                 fprintf(stream, ".");
125                         }
126                         else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
127                         {
128
129                                 fprintf(stream, "%c", Piece::tokens[piece->type]);
130
131
132                         }
133                         else
134                         {
135                                 switch (piece->colour)
136                                 {
137                                         case Piece::RED:
138                                         case Piece::BLUE:
139                                                 fprintf(stream, "#");
140                                                 break;
141                                         case Piece::NONE:
142                                                 fprintf(stream, "+");
143                                                 break;
144                                         case Piece::BOTH:
145                                                 fprintf(stream, "$");
146                                                 break;
147                                 }
148                         }
149                 }
150                 fprintf(stream, "\n");
151         }
152         
153 }
154
155 /**
156  * Print textual representation of the board to a stream
157  * @param stream - the stream to print information to
158  * @param reveal - Pieces matching this colour will have their identify revealed, other pieces will be shown as '#'
159  */
160 void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal)
161 {
162         for (int y=0; y < height; ++y)
163         {
164                 for (int x=0; x < width; ++x)
165                 {
166                         Piece * piece = board[x][y];
167                         if (piece == NULL)
168                         {
169                                 fprintf(stream, ".");
170                         }
171                         else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
172                         {
173                                 switch (piece->colour)  
174                                 {
175                                         case Piece::RED:
176                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,40);
177                                                 break;
178                                         case Piece::BLUE:
179                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,40);
180                                                 break;
181                                         default:
182                                                 break;
183                                 }
184                                 fprintf(stream, "%c", Piece::tokens[piece->type]);
185
186                         }
187                         else
188                         {
189                                 switch (piece->colour)
190                                 {
191                                         case Piece::RED:
192                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,41);
193
194                                                 break;
195                                         case Piece::BLUE:
196                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,44);
197                                                 break;
198                                         case Piece::NONE:
199                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,37,47);
200                                                 break;
201                                         case Piece::BOTH:
202                                                 //Should never see this
203                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,33,43);
204                                                 break;
205
206                                 }       
207                                 fprintf(stream, "#");
208                                 
209                         }
210                         fprintf(stream, "%c[%d;%d;%dm",0x1B,0,7,0);
211                 }
212                 fprintf(stream, "\n");
213         }
214         
215 }
216
217
218 #ifdef BUILD_GRAPHICS
219 /**
220  * Draw the board state to graphics
221  * @param reveal - Pieces matching this colour will be revealed. If Piece::BOTH, all pieces will be revealed
222  * @param showRevealed - If true, then all pieces that have taken part in combat will be revealed, regardless of colour.
223  *                       If false, only pieces matching the colour reveal will be revealed
224  */
225 void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
226 {
227
228         if (!Graphics::Initialised())
229         {
230                 fprintf(stderr, "ERROR - Board::Draw called whilst graphics disabled!!!\n");
231                 exit(EXIT_FAILURE);
232                 
233         }
234
235         Graphics::ClearScreen();
236         
237         for (int y=0; y < height; ++y)
238         {
239                 for (int x=0; x < width; ++x)
240                 {
241                         Piece * piece = board[x][y];
242                         if (piece == NULL)
243                         {
244                                 //Don't display anything
245
246                         }
247                         else if ((piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
248                                         || (piece->beenRevealed && showRevealed))
249                         {
250                                 //Display the piece
251                                 Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
252                                 
253                         }
254                         else
255                         {
256                                 switch (piece->colour)
257                                 {
258                                         case Piece::RED:
259                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
260                                                 break;
261                                         case Piece::BLUE:
262                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
263                                                 break;
264                                         case Piece::NONE:
265                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
266                                                 break;
267                                         case Piece::BOTH:
268                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
269                                                 break;
270                                 }
271                         }
272                 }
273                 
274         }
275         Graphics::UpdateScreen();
276         
277 }
278 #endif //BUILD_GRAPHICS
279
280 /**
281  * Adds a piece to the board
282  * @param x - x-coord to place the piece at, starting at zero, must be less than board width
283  * @param y - y-coord to place the piece at, starting at zero, must be less than board height
284  * @param newType - the Type of the piece
285  * @param newColour - the Colour of the piece
286  * @returns true if and only if the piece could be successfully added.
287  */
288 bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
289 {
290         if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
291                 return false;
292
293         Piece * piece = new Piece(newType, newColour);
294         board[x][y] = piece;
295
296         pieces.push_back(piece);
297         return true;
298 }
299
300 /**
301  * Gets a pointer to a piece at a board location
302  * UNUSED
303  * @param x - x-coord of the piece
304  * @param y - y-coord of the piece
305  * @returns pointer to the piece, or NULL if the board location was empty
306  * @throws error if board is null or coords are invalid
307  */
308 Piece * Board::GetPiece(int x, int y)
309 {
310         assert(board != NULL);
311         assert(x >= 0 && x < width && y >= 0 && y < height);
312         return board[x][y];
313 }
314
315 /**
316  * Moves a piece at a specified position in the specified direction, handles combat if necessary, updates state of the board
317  * @param x - x-coord of the piece
318  * @param y - y-coord of the piece
319  * @param direction - Direction in which to move (UP, DOWN, LEFT or RIGHT)
320  * @param colour - Colour which the piece must match for the move to be valid
321  * @returns A MovementResult which indicates the result of the move
322  */
323 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
324 {
325         if (board == NULL) 
326         {
327                 return MovementResult(MovementResult::NO_BOARD);
328         }
329         if (!(x >= 0 && x < width && y >= 0 && y < height)) 
330         {
331                 return MovementResult(MovementResult::INVALID_POSITION);
332         }
333         Piece * target = board[x][y];
334         if (target == NULL) 
335         {
336                 return MovementResult(MovementResult::NO_SELECTION);
337         }
338         if (!(colour == Piece::NONE || target->colour == colour)) 
339         {
340                 return MovementResult(MovementResult::NOT_YOUR_UNIT);
341         }
342         if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER) 
343         {
344                 return MovementResult(MovementResult::IMMOBILE_UNIT);
345         }
346         if (multiplier < 1)
347                 return MovementResult(MovementResult::INVALID_DIRECTION); //Don't allow moves that don't actually move forward
348         if (multiplier > 1 && target->type != Piece::SCOUT)
349         {
350                 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
351         }
352         int x2 = x; int y2 = y;
353
354         for (int ii=0; ii < multiplier; ++ii)
355         {
356                 switch (direction)
357                 {
358                         case UP:
359                                 --y2;
360                                 break;
361                         case DOWN:
362                                 ++y2;
363                                 break;
364                         case LEFT:
365                                 --x2;
366                                 break;
367                         case RIGHT:
368                                 ++x2;
369                                 break;
370                 }
371                 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height)) 
372                 {
373                         return MovementResult(MovementResult::INVALID_DIRECTION);
374                 }
375                 if (ii < multiplier-1 && board[x2][y2] != NULL)
376                 {
377                         return MovementResult(MovementResult::POSITION_FULL);
378                 }
379         }
380         Piece * defender = board[x2][y2];
381         if (defender == NULL)
382         {
383                 board[x][y] = NULL;
384                 board[x2][y2] = target;
385         }
386         else if (defender->colour != target->colour)
387         {
388                 defender->beenRevealed = true;
389                 target->beenRevealed = true;
390
391                 Piece::Type defenderType = defender->type;
392                 Piece::Type attackerType = target->type;
393
394                 if (defender->colour == Piece::NONE) 
395                 {
396                         return MovementResult(MovementResult::POSITION_FULL);
397                 }
398                 if (defender->type == Piece::FLAG)
399                 {
400                         winner = target->colour;
401                         return MovementResult(MovementResult::VICTORY_FLAG);
402                 }
403                 else if (defender->type == Piece::BOMB)
404                 {
405                         if (target->type == Piece::MINER)
406                         {
407                                 RemovePiece(defender);
408                                 delete defender;
409                                 board[x][y] = NULL;
410                                 board[x2][y2] = target;
411                                 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
412                         }
413                         else
414                         {
415                                 //Use this to destroy only the attacking piece, and not the bomb
416                                 RemovePiece(target);
417                                 delete target;
418                                 board[x][y] = NULL;
419                                 return MovementResult(MovementResult::DIES, attackerType, defenderType);
420
421                                 /*
422                                 //Use this to destroy both the bomb and the attacking piece
423                                 RemovePiece(defender);
424                                 RemovePiece(target);
425                                 delete defender;
426                                 delete target;
427                                 board[x][y] = NULL;
428                                 board[x2][y2] = NULL;
429                                 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
430                                 */
431                         }
432                 }
433                 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
434                 {
435                         RemovePiece(defender);
436                         delete defender;
437                         board[x][y] = NULL;
438                         board[x2][y2] = target;
439                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
440                 }
441                 else if (target->operator > (*defender))
442                 {
443                         RemovePiece(defender);
444                         delete defender;
445                         board[x][y] = NULL;
446                         board[x2][y2] = target;
447                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
448                 }
449                 else if (target->operator==(*defender))// && rand() % 2 == 0)
450                 {
451                         RemovePiece(defender);
452                         RemovePiece(target);
453                         delete defender;
454                         delete target;
455                         board[x][y] = NULL;
456                         board[x2][y2] = NULL;   
457                         return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
458                 }
459                 else
460                 {
461                         RemovePiece(target);
462                         delete target;
463                         board[x][y] = NULL;
464                         return MovementResult(MovementResult::DIES, attackerType, defenderType);
465                 }
466         }
467         else
468         {
469                 return MovementResult(MovementResult::POSITION_FULL);
470         }
471         return MovementResult(MovementResult::OK);
472 }       
473
474 /**
475  * Removes a piece from the board
476  * @param piece The piece to remove
477  * @returns true iff the piece actually existed
478  */
479 bool Board::RemovePiece(Piece * piece)
480 {
481         bool result = false;
482         for (int x = 0; x < width; ++x)
483         {
484                 for (int y = 0; y < height; ++y)        
485                 {
486                         if (board[x][y] == piece)
487                         {
488                                 result = true;
489                                 board[x][y] = NULL;
490                         }
491                 }
492         }
493
494         vector<Piece*>::iterator i = pieces.begin();
495         while (i != pieces.end())
496         {
497                 if ((*i) == piece)
498                 {
499                         i = pieces.erase(i);
500                         result = true;
501                         continue;
502                 }
503                 ++i;
504         }
505         return result;
506 }
507
508 /**
509  * Returns the total value of pieces belonging to colour
510  * @param colour the colour
511  * @returns the total value of pieces belonging to colour.
512  *      (Redundant repetition <3)
513  */
514 int Board::TotalPieceValue(const Piece::Colour & colour) const
515 {
516         int result = 0;
517         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
518         {
519                 if ((*i)->colour == colour || colour == Piece::BOTH)
520                 {
521                         result += (*i)->PieceValue();
522                 }
523         }
524         return result;
525 }
526
527 /**
528  * Returns the total number of mobile pieces belonging to colour
529  * @param colour the colour
530  * @returns the total value of mobile pieces belonging to colour.
531  *      (Redundant repetition <3)
532  */
533 int Board::MobilePieces(const Piece::Colour & colour) const
534 {
535         int result = 0;
536         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
537         {
538                 if ((*i)->colour == colour || colour == Piece::BOTH)
539                 {
540                         if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)
541                                 result++;
542                 }
543         }
544         return result;
545 }
546
547

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