Merge branch 'networking' of mussel.ucc.asn.au:progcomp2012
[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, bool showRevealed)
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                                         || (piece->beenRevealed && showRevealed))
173                         {
174                                 switch (piece->colour)  
175                                 {
176                                         case Piece::RED:
177                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,40);
178                                                 break;
179                                         case Piece::BLUE:
180                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,40);
181                                                 break;
182                                         default:
183                                                 break;
184                                 }
185                                 fprintf(stream, "%c", Piece::tokens[piece->type]);
186
187                         }
188                         else
189                         {
190                                 switch (piece->colour)
191                                 {
192                                         case Piece::RED:
193                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,41);
194
195                                                 break;
196                                         case Piece::BLUE:
197                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,44);
198                                                 break;
199                                         case Piece::NONE:
200                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,37,47);
201                                                 break;
202                                         case Piece::BOTH:
203                                                 //Should never see this
204                                                 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,33,43);
205                                                 break;
206
207                                 }       
208                                 fprintf(stream, "#");
209                                 
210                         }
211                         fprintf(stream, "%c[%d;%d;%dm",0x1B,0,7,0);
212                 }
213                 fprintf(stream, "\n");
214         }
215         
216 }
217
218
219 #ifdef BUILD_GRAPHICS
220 /**
221  * Draw the board state to graphics
222  * @param reveal - Pieces matching this colour will be revealed. If Piece::BOTH, all pieces will be revealed
223  * @param showRevealed - If true, then all pieces that have taken part in combat will be revealed, regardless of colour.
224  *                       If false, only pieces matching the colour reveal will be revealed
225  */
226 void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
227 {
228
229         if (!Graphics::Initialised())
230         {
231                 fprintf(stderr, "ERROR - Board::Draw called whilst graphics disabled!!!\n");
232                 exit(EXIT_FAILURE);
233                 
234         }
235
236         Graphics::ClearScreen();
237         
238         for (int y=0; y < height; ++y)
239         {
240                 for (int x=0; x < width; ++x)
241                 {
242                         Piece * piece = board[x][y];
243                         if (piece == NULL)
244                         {
245                                 //Don't display anything
246
247                         }
248                         else if ((piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
249                                         || (piece->beenRevealed && showRevealed))
250                         {
251                                 //Display the piece
252                                 Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
253                                 
254                         }
255                         else
256                         {
257                                 switch (piece->colour)
258                                 {
259                                         case Piece::RED:
260                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
261                                                 break;
262                                         case Piece::BLUE:
263                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
264                                                 break;
265                                         case Piece::NONE:
266                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
267                                                 break;
268                                         case Piece::BOTH:
269                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
270                                                 break;
271                                 }
272                         }
273                 }
274                 
275         }
276         Graphics::UpdateScreen();
277         
278 }
279 #endif //BUILD_GRAPHICS
280
281 /**
282  * Adds a piece to the board
283  * @param x - x-coord to place the piece at, starting at zero, must be less than board width
284  * @param y - y-coord to place the piece at, starting at zero, must be less than board height
285  * @param newType - the Type of the piece
286  * @param newColour - the Colour of the piece
287  * @returns true if and only if the piece could be successfully added.
288  */
289 bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
290 {
291         if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
292                 return false;
293
294         Piece * piece = new Piece(newType, newColour);
295         board[x][y] = piece;
296
297         pieces.push_back(piece);
298         return true;
299 }
300
301 /**
302  * Gets a pointer to a piece at a board location
303  * UNUSED
304  * @param x - x-coord of the piece
305  * @param y - y-coord of the piece
306  * @returns pointer to the piece, or NULL if the board location was empty
307  * @throws error if board is null or coords are invalid
308  */
309 Piece * Board::GetPiece(int x, int y)
310 {
311         assert(board != NULL);
312         assert(x >= 0 && x < width && y >= 0 && y < height);
313         return board[x][y];
314 }
315
316 /**
317  * Moves a piece at a specified position in the specified direction, handles combat if necessary, updates state of the board
318  * @param x - x-coord of the piece
319  * @param y - y-coord of the piece
320  * @param direction - Direction in which to move (UP, DOWN, LEFT or RIGHT)
321  * @param colour - Colour which the piece must match for the move to be valid
322  * @returns A MovementResult which indicates the result of the move
323  */
324 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
325 {
326         if (board == NULL) 
327         {
328                 return MovementResult(MovementResult::NO_BOARD);
329         }
330         if (!(x >= 0 && x < width && y >= 0 && y < height)) 
331         {
332                 return MovementResult(MovementResult::INVALID_POSITION);
333         }
334         Piece * target = board[x][y];
335         if (target == NULL) 
336         {
337                 return MovementResult(MovementResult::NO_SELECTION);
338         }
339         if (!(colour == Piece::NONE || target->colour == colour)) 
340         {
341                 return MovementResult(MovementResult::NOT_YOUR_UNIT);
342         }
343         if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER) 
344         {
345                 return MovementResult(MovementResult::IMMOBILE_UNIT);
346         }
347         if (multiplier < 1)
348                 return MovementResult(MovementResult::INVALID_DIRECTION); //Don't allow moves that don't actually move forward
349         if (multiplier > 1 && target->type != Piece::SCOUT)
350         {
351                 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
352         }
353         int x2 = x; int y2 = y;
354
355         for (int ii=0; ii < multiplier; ++ii)
356         {
357                 switch (direction)
358                 {
359                         case UP:
360                                 --y2;
361                                 break;
362                         case DOWN:
363                                 ++y2;
364                                 break;
365                         case LEFT:
366                                 --x2;
367                                 break;
368                         case RIGHT:
369                                 ++x2;
370                                 break;
371                 }
372                 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height)) 
373                 {
374                         return MovementResult(MovementResult::INVALID_DIRECTION);
375                 }
376                 if (ii < multiplier-1 && board[x2][y2] != NULL)
377                 {
378                         return MovementResult(MovementResult::POSITION_FULL);
379                 }
380         }
381         Piece * defender = board[x2][y2];
382         if (defender == NULL)
383         {
384                 board[x][y] = NULL;
385                 board[x2][y2] = target;
386         }
387         else if (defender->colour != target->colour)
388         {
389                 defender->beenRevealed = true;
390                 target->beenRevealed = true;
391
392                 Piece::Type defenderType = defender->type;
393                 Piece::Type attackerType = target->type;
394
395                 if (defender->colour == Piece::NONE) 
396                 {
397                         return MovementResult(MovementResult::POSITION_FULL);
398                 }
399                 if (defender->type == Piece::FLAG)
400                 {
401                         winner = target->colour;
402                         return MovementResult(MovementResult::VICTORY_FLAG);
403                 }
404                 else if (defender->type == Piece::BOMB)
405                 {
406                         if (target->type == Piece::MINER)
407                         {
408                                 RemovePiece(defender);
409                                 delete defender;
410                                 board[x][y] = NULL;
411                                 board[x2][y2] = target;
412                                 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
413                         }
414                         else
415                         {
416                                 //Use this to destroy only the attacking piece, and not the bomb
417                                 RemovePiece(target);
418                                 delete target;
419                                 board[x][y] = NULL;
420                                 return MovementResult(MovementResult::DIES, attackerType, defenderType);
421
422                                 /*
423                                 //Use this to destroy both the bomb and the attacking piece
424                                 RemovePiece(defender);
425                                 RemovePiece(target);
426                                 delete defender;
427                                 delete target;
428                                 board[x][y] = NULL;
429                                 board[x2][y2] = NULL;
430                                 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
431                                 */
432                         }
433                 }
434                 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
435                 {
436                         RemovePiece(defender);
437                         delete defender;
438                         board[x][y] = NULL;
439                         board[x2][y2] = target;
440                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
441                 }
442                 else if (target->operator > (*defender))
443                 {
444                         RemovePiece(defender);
445                         delete defender;
446                         board[x][y] = NULL;
447                         board[x2][y2] = target;
448                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
449                 }
450                 else if (target->operator==(*defender))// && rand() % 2 == 0)
451                 {
452                         RemovePiece(defender);
453                         RemovePiece(target);
454                         delete defender;
455                         delete target;
456                         board[x][y] = NULL;
457                         board[x2][y2] = NULL;   
458                         return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
459                 }
460                 else
461                 {
462                         RemovePiece(target);
463                         delete target;
464                         board[x][y] = NULL;
465                         return MovementResult(MovementResult::DIES, attackerType, defenderType);
466                 }
467         }
468         else
469         {
470                 return MovementResult(MovementResult::POSITION_FULL);
471         }
472         return MovementResult(MovementResult::OK);
473 }       
474
475 /**
476  * Removes a piece from the board
477  * @param piece The piece to remove
478  * @returns true iff the piece actually existed
479  */
480 bool Board::RemovePiece(Piece * piece)
481 {
482         bool result = false;
483         for (int x = 0; x < width; ++x)
484         {
485                 for (int y = 0; y < height; ++y)        
486                 {
487                         if (board[x][y] == piece)
488                         {
489                                 result = true;
490                                 board[x][y] = NULL;
491                         }
492                 }
493         }
494
495         vector<Piece*>::iterator i = pieces.begin();
496         while (i != pieces.end())
497         {
498                 if ((*i) == piece)
499                 {
500                         i = pieces.erase(i);
501                         result = true;
502                         continue;
503                 }
504                 ++i;
505         }
506         return result;
507 }
508
509 /**
510  * Returns the total value of pieces belonging to colour
511  * @param colour the colour
512  * @returns the total value of pieces belonging to colour.
513  *      (Redundant repetition <3)
514  */
515 int Board::TotalPieceValue(const Piece::Colour & colour) const
516 {
517         int result = 0;
518         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
519         {
520                 if ((*i)->colour == colour || colour == Piece::BOTH)
521                 {
522                         result += (*i)->PieceValue();
523                 }
524         }
525         return result;
526 }
527
528 /**
529  * Returns the total number of mobile pieces belonging to colour
530  * @param colour the colour
531  * @returns the total value of mobile pieces belonging to colour.
532  *      (Redundant repetition <3)
533  */
534 int Board::MobilePieces(const Piece::Colour & colour) const
535 {
536         int result = 0;
537         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
538         {
539                 if ((*i)->colour == colour || colour == Piece::BOTH)
540                 {
541                         if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)
542                                 result++;
543                 }
544         }
545         return result;
546 }
547
548

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