More adding of pointless crap to manager
[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)
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         return true;
273 }
274
275 /**
276  * Gets a pointer to a piece at a board location
277  * UNUSED
278  * @param x - x-coord of the piece
279  * @param y - y-coord of the piece
280  * @returns pointer to the piece, or NULL if the board location was empty
281  * @throws error if board is null or coords are invalid
282  */
283 Piece * Board::GetPiece(int x, int y)
284 {
285         assert(board != NULL);
286         assert(x >= 0 && x < width && y >= 0 && y < height);
287         return board[x][y];
288 }
289
290 /**
291  * Moves a piece at a specified position in the specified direction, handles combat if necessary
292  * @param x - x-coord of the piece
293  * @param y - y-coord of the piece
294  * @param direction - Direction in which to move (UP, DOWN, LEFT or RIGHT)
295  * @param colour - Colour which the piece must match for the move to be valid
296  * @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
297  */
298 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
299 {
300         if (board == NULL) 
301         {
302                 return MovementResult(MovementResult::NO_BOARD);
303         }
304         if (!(x >= 0 && x < width && y >= 0 && y < height)) 
305         {
306                 return MovementResult(MovementResult::INVALID_POSITION);
307         }
308         Piece * target = board[x][y];
309         if (target == NULL) 
310         {
311                 return MovementResult(MovementResult::NO_SELECTION);
312         }
313         if (!(colour == Piece::NONE || target->colour == colour)) 
314         {
315                 return MovementResult(MovementResult::NOT_YOUR_UNIT);
316         }
317         if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER) 
318         {
319                 return MovementResult(MovementResult::IMMOBILE_UNIT);
320         }
321         if (multiplier > 1 && target->type != Piece::SCOUT)
322         {
323                 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
324         }
325         int x2 = x; int y2 = y;
326
327         for (int ii=0; ii < multiplier; ++ii)
328         {
329                 switch (direction)
330                 {
331                         case UP:
332                                 --y2;
333                                 break;
334                         case DOWN:
335                                 ++y2;
336                                 break;
337                         case LEFT:
338                                 --x2;
339                                 break;
340                         case RIGHT:
341                                 ++x2;
342                                 break;
343                 }
344                 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height)) 
345                 {
346                         return MovementResult(MovementResult::INVALID_DIRECTION);
347                 }
348                 if (ii < multiplier-1 && board[x2][y2] != NULL)
349                 {
350                         return MovementResult(MovementResult::POSITION_FULL);
351                 }
352         }
353         Piece * defender = board[x2][y2];
354         if (defender == NULL)
355         {
356                 board[x][y] = NULL;
357                 board[x2][y2] = target;
358         }
359         else if (defender->colour != target->colour)
360         {
361                 Piece::Type defenderType = defender->type;
362                 Piece::Type attackerType = target->type;
363
364                 if (defender->colour == Piece::NONE) 
365                 {
366                         return MovementResult(MovementResult::POSITION_FULL);
367                 }
368                 if (defender->type == Piece::FLAG)
369                 {
370                         winner = target->colour;
371                         return MovementResult(MovementResult::VICTORY);
372                 }
373                 else if (defender->type == Piece::BOMB)
374                 {
375                         if (target->type == Piece::MINER)
376                         {
377
378                                 delete defender;
379                                 board[x][y] = NULL;
380                                 board[x2][y2] = target;
381                                 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
382                         }
383                         else
384                         {
385                                 delete defender;
386                                 delete target;
387                                 board[x][y] = NULL;
388                                 board[x2][y2] = NULL;
389                                 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
390                         }
391                 }
392                 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
393                 {
394                         delete defender;
395                         board[x][y] = NULL;
396                         board[x2][y2] = target;
397                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
398                 }
399                 else if (target->operator > (*defender))
400                 {
401                         delete defender;
402                         board[x][y] = NULL;
403                         board[x2][y2] = target;
404                         return MovementResult(MovementResult::KILLS, attackerType, defenderType);
405                 }
406                 else if (target->operator==(*defender) && rand() % 2 == 0)
407                 {
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                         delete target;
416                         board[x][y] = NULL;
417                         return MovementResult(MovementResult::DIES, attackerType, defenderType);
418                 }
419         }
420         else
421         {
422                 return MovementResult(MovementResult::POSITION_FULL);
423         }
424         return MovementResult(MovementResult::OK);
425 }       
426
427
428

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