ed38e356ebf2924da05980b586fa07d74acf66c6
[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), 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)
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                         {
228                                 //Display the piece
229                                 Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
230                                 
231                         }
232                         else
233                         {
234                                 switch (piece->colour)
235                                 {
236                                         case Piece::RED:
237                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
238                                                 break;
239                                         case Piece::BLUE:
240                                                 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
241                                                 break;
242                                         case Piece::NONE:
243                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
244                                                 break;
245                                         case Piece::BOTH:
246                                                 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
247                                                 break;
248                                 }
249                         }
250                 }
251                 
252         }
253         Graphics::UpdateScreen();
254         
255 }
256
257 /**
258  * Adds a piece to the board
259  * @param x - x-coord to place the piece at, starting at zero, must be less than board width
260  * @param y - y-coord to place the piece at, starting at zero, must be less than board height
261  * @param newType - the Type of the piece
262  * @param newColour - the Colour of the piece
263  * @returns true if and only if the piece could be successfully added.
264  */
265 bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
266 {
267         if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
268                 return false;
269
270         Piece * piece = new Piece(newType, newColour);
271         board[x][y] = piece;
272
273         pieces.push_back(piece);
274         return true;
275 }
276
277 /**
278  * Gets a pointer to a piece at a board location
279  * UNUSED
280  * @param x - x-coord of the piece
281  * @param y - y-coord of the piece
282  * @returns pointer to the piece, or NULL if the board location was empty
283  * @throws error if board is null or coords are invalid
284  */
285 Piece * Board::GetPiece(int x, int y)
286 {
287         assert(board != NULL);
288         assert(x >= 0 && x < width && y >= 0 && y < height);
289         return board[x][y];
290 }
291
292 /**
293  * Moves a piece at a specified position in the specified direction, handles combat if necessary
294  * @param x - x-coord of the piece
295  * @param y - y-coord of the piece
296  * @param direction - Direction in which to move (UP, DOWN, LEFT or RIGHT)
297  * @param colour - Colour which the piece must match for the move to be valid
298  * @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
299  */
300 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
301 {
302         if (board == NULL) 
303         {
304                 return MovementResult(MovementResult::NO_BOARD);
305         }
306         if (!(x >= 0 && x < width && y >= 0 && y < height)) 
307         {
308                 return MovementResult(MovementResult::INVALID_POSITION);
309         }
310         Piece * target = board[x][y];
311         if (target == NULL) 
312         {
313                 return MovementResult(MovementResult::NO_SELECTION);
314         }
315         if (!(colour == Piece::NONE || target->colour == colour)) 
316         {
317                 return MovementResult(MovementResult::NOT_YOUR_UNIT);
318         }
319         if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER) 
320         {
321                 return MovementResult(MovementResult::IMMOBILE_UNIT);
322         }
323         if (multiplier > 1 && target->type != Piece::SCOUT)
324         {
325                 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
326         }
327         int x2 = x; int y2 = y;
328
329         for (int ii=0; ii < multiplier; ++ii)
330         {
331                 switch (direction)
332                 {
333                         case UP:
334                                 --y2;
335                                 break;
336                         case DOWN:
337                                 ++y2;
338                                 break;
339                         case LEFT:
340                                 --x2;
341                                 break;
342                         case RIGHT:
343                                 ++x2;
344                                 break;
345                 }
346                 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height)) 
347                 {
348                         return MovementResult(MovementResult::INVALID_DIRECTION);
349                 }
350                 if (ii < multiplier-1 && board[x2][y2] != NULL)
351                 {
352                         return MovementResult(MovementResult::POSITION_FULL);
353                 }
354         }
355         Piece * defender = board[x2][y2];
356         if (defender == NULL)
357         {
358                 board[x][y] = NULL;
359                 board[x2][y2] = target;
360         }
361         else if (defender->colour != target->colour)
362         {
363                 Piece::Type defenderType = defender->type;
364                 Piece::Type attackerType = target->type;
365
366                 if (defender->colour == Piece::NONE) 
367                 {
368                         return MovementResult(MovementResult::POSITION_FULL);
369                 }
370                 if (defender->type == Piece::FLAG)
371                 {
372                         winner = target->colour;
373                         return MovementResult(MovementResult::VICTORY);
374                 }
375                 else if (defender->type == Piece::BOMB)
376                 {
377                         if (target->type == Piece::MINER)
378                         {
379                                 RemovePiece(defender);
380                                 delete defender;
381                                 board[x][y] = NULL;
382                                 board[x2][y2] = target;
383                                 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
384                         }
385                         else
386                         {
387                                 RemovePiece(defender);
388                                 RemovePiece(target);
389                                 delete defender;
390                                 delete target;
391                                 board[x][y] = NULL;
392                                 board[x2][y2] = NULL;
393                                 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
394                         }
395                 }
396                 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
397                 {
398                         RemovePiece(defender);
399                         delete defender;
400                         board[x][y] = NULL;
401                         board[x2][y2] = target;
402                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
403                 }
404                 else if (target->operator > (*defender))
405                 {
406                         RemovePiece(defender);
407                         delete defender;
408                         board[x][y] = NULL;
409                         board[x2][y2] = target;
410                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
411                 }
412                 else if (target->operator==(*defender) && rand() % 2 == 0)
413                 {
414                         RemovePiece(defender);
415                         delete defender;
416                         board[x][y] = NULL;
417                         board[x2][y2] = target; 
418                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
419                 }
420                 else
421                 {
422                         RemovePiece(target);
423                         delete target;
424                         board[x][y] = NULL;
425                         return MovementResult(MovementResult::DIES, attackerType, defenderType);
426                 }
427         }
428         else
429         {
430                 return MovementResult(MovementResult::POSITION_FULL);
431         }
432         return MovementResult(MovementResult::OK);
433 }       
434
435 /**
436  * Removes a piece from the board
437  * @param piece The piece to remove
438  * @returns true iff the piece actually existed
439  */
440 bool Board::RemovePiece(Piece * piece)
441 {
442         bool result = false;
443         for (int x = 0; x < width; ++x)
444         {
445                 for (int y = 0; y < height; ++y)        
446                 {
447                         if (board[x][y] == piece)
448                         {
449                                 result = true;
450                                 board[x][y] = NULL;
451                         }
452                 }
453         }
454
455         vector<Piece*>::iterator i = pieces.begin();
456         while (i != pieces.end())
457         {
458                 if ((*i) == piece)
459                 {
460                         i = pieces.erase(i);
461                         result = true;
462                         continue;
463                 }
464                 ++i;
465         }
466         return result;
467 }
468
469 /**
470  * Returns the total value of pieces belonging to colour
471  * @param colour the colour
472  * @returns the total value of pieces belonging to colour.
473  *      (Redundant repetition <3)
474  */
475 int Board::TotalPieceValue(const Piece::Colour & colour) const
476 {
477         int result = 0;
478         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
479         {
480                 if ((*i)->colour == colour || colour == Piece::BOTH)
481                 {
482                         result += (*i)->PieceValue();
483                 }
484         }
485         return result;
486 }
487
488 /**
489  * Returns the total number of mobile pieces belonging to colour
490  * @param colour the colour
491  * @returns the total value of mobile pieces belonging to colour.
492  *      (Redundant repetition <3)
493  */
494 int Board::MobilePieces(const Piece::Colour & colour) const
495 {
496         int result = 0;
497         for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
498         {
499                 if ((*i)->colour == colour || colour == Piece::BOTH)
500                 {
501                         if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)
502                                 result++;
503                 }
504         }
505         return result;
506 }
507
508

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