Began implementation of networking
[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 && target->type != Piece::SCOUT)
347         {
348                 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
349         }
350         int x2 = x; int y2 = y;
351
352         for (int ii=0; ii < multiplier; ++ii)
353         {
354                 switch (direction)
355                 {
356                         case UP:
357                                 --y2;
358                                 break;
359                         case DOWN:
360                                 ++y2;
361                                 break;
362                         case LEFT:
363                                 --x2;
364                                 break;
365                         case RIGHT:
366                                 ++x2;
367                                 break;
368                 }
369                 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height)) 
370                 {
371                         return MovementResult(MovementResult::INVALID_DIRECTION);
372                 }
373                 if (ii < multiplier-1 && board[x2][y2] != NULL)
374                 {
375                         return MovementResult(MovementResult::POSITION_FULL);
376                 }
377         }
378         Piece * defender = board[x2][y2];
379         if (defender == NULL)
380         {
381                 board[x][y] = NULL;
382                 board[x2][y2] = target;
383         }
384         else if (defender->colour != target->colour)
385         {
386                 defender->beenRevealed = true;
387                 target->beenRevealed = true;
388
389                 Piece::Type defenderType = defender->type;
390                 Piece::Type attackerType = target->type;
391
392                 if (defender->colour == Piece::NONE) 
393                 {
394                         return MovementResult(MovementResult::POSITION_FULL);
395                 }
396                 if (defender->type == Piece::FLAG)
397                 {
398                         winner = target->colour;
399                         return MovementResult(MovementResult::VICTORY_FLAG);
400                 }
401                 else if (defender->type == Piece::BOMB)
402                 {
403                         if (target->type == Piece::MINER)
404                         {
405                                 RemovePiece(defender);
406                                 delete defender;
407                                 board[x][y] = NULL;
408                                 board[x2][y2] = target;
409                                 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
410                         }
411                         else
412                         {
413                                 //Use this to destroy only the attacking piece, and not the bomb
414                                 RemovePiece(target);
415                                 delete target;
416                                 board[x][y] = NULL;
417                                 return MovementResult(MovementResult::DIES, attackerType, defenderType);
418
419                                 /*
420                                 //Use this to destroy both the bomb and the attacking piece
421                                 RemovePiece(defender);
422                                 RemovePiece(target);
423                                 delete defender;
424                                 delete target;
425                                 board[x][y] = NULL;
426                                 board[x2][y2] = NULL;
427                                 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
428                                 */
429                         }
430                 }
431                 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
432                 {
433                         RemovePiece(defender);
434                         delete defender;
435                         board[x][y] = NULL;
436                         board[x2][y2] = target;
437                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
438                 }
439                 else if (target->operator > (*defender))
440                 {
441                         RemovePiece(defender);
442                         delete defender;
443                         board[x][y] = NULL;
444                         board[x2][y2] = target;
445                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
446                 }
447                 else if (target->operator==(*defender))// && rand() % 2 == 0)
448                 {
449                         RemovePiece(defender);
450                         RemovePiece(target);
451                         delete defender;
452                         delete target;
453                         board[x][y] = NULL;
454                         board[x2][y2] = NULL;   
455                         return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
456                 }
457                 else
458                 {
459                         RemovePiece(target);
460                         delete target;
461                         board[x][y] = NULL;
462                         return MovementResult(MovementResult::DIES, attackerType, defenderType);
463                 }
464         }
465         else
466         {
467                 return MovementResult(MovementResult::POSITION_FULL);
468         }
469         return MovementResult(MovementResult::OK);
470 }       
471
472 /**
473  * Removes a piece from the board
474  * @param piece The piece to remove
475  * @returns true iff the piece actually existed
476  */
477 bool Board::RemovePiece(Piece * piece)
478 {
479         bool result = false;
480         for (int x = 0; x < width; ++x)
481         {
482                 for (int y = 0; y < height; ++y)        
483                 {
484                         if (board[x][y] == piece)
485                         {
486                                 result = true;
487                                 board[x][y] = NULL;
488                         }
489                 }
490         }
491
492         vector<Piece*>::iterator i = pieces.begin();
493         while (i != pieces.end())
494         {
495                 if ((*i) == piece)
496                 {
497                         i = pieces.erase(i);
498                         result = true;
499                         continue;
500                 }
501                 ++i;
502         }
503         return result;
504 }
505
506 /**
507  * Returns the total value of pieces belonging to colour
508  * @param colour the colour
509  * @returns the total value of pieces belonging to colour.
510  *      (Redundant repetition <3)
511  */
512 int Board::TotalPieceValue(const Piece::Colour & colour) const
513 {
514         int result = 0;
515         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
516         {
517                 if ((*i)->colour == colour || colour == Piece::BOTH)
518                 {
519                         result += (*i)->PieceValue();
520                 }
521         }
522         return result;
523 }
524
525 /**
526  * Returns the total number of mobile pieces belonging to colour
527  * @param colour the colour
528  * @returns the total value of mobile pieces belonging to colour.
529  *      (Redundant repetition <3)
530  */
531 int Board::MobilePieces(const Piece::Colour & colour) const
532 {
533         int result = 0;
534         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
535         {
536                 if ((*i)->colour == colour || colour == Piece::BOTH)
537                 {
538                         if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)
539                                 result++;
540                 }
541         }
542         return result;
543 }
544
545

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