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};
18 Piece::TextureManager Piece::textures;
20 Piece::TextureManager::~TextureManager()
22 Array<Texture*>::Iterator i(*this);
30 Texture & Piece::TextureManager::operator[](const LUint & at)
32 while (Array<Texture*>::Size() <= at)
35 sprintf(buffer, "images/piece%lu.bmp", Array<Texture*>::Size());
36 Array<Texture*>::Add(new Texture(buffer, false));
39 return *(Array<Texture*>::operator[](at));
41 #endif //BUILD_GRAPHICS
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
48 Piece::Type Piece::GetType(char fromToken)
50 for (int ii=0; ii <= (int)(Piece::BOMB); ++ii)
52 if (tokens[ii] == fromToken)
54 return Type(Piece::NOTHING + ii);
57 return Piece::BOULDER;
61 * Gets the opposite to the indicated colour
63 Piece::Colour Piece::OppositeColour(const Colour & colour)
82 * Construct a new, empty board
83 * @param newWidth - the width of the board
84 * @param newHeight - the height of the board
86 Board::Board(int newWidth, int newHeight) : winner(Piece::NONE), width(newWidth), height(newHeight), board(NULL), pieces()
88 board = new Piece**[width];
89 for (int x=0; x < width; ++x)
91 board[x] = new Piece*[height];
92 for (int y=0; y < height; ++y)
102 for (int x=0; x < width; ++x)
104 for (int y=0; y < height; ++y)
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 '#'
115 void Board::Print(FILE * stream, const Piece::Colour & reveal)
117 for (int y=0; y < height; ++y)
119 for (int x=0; x < width; ++x)
121 Piece * piece = board[x][y];
124 fprintf(stream, ".");
126 else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
129 fprintf(stream, "%c", Piece::tokens[piece->type]);
135 switch (piece->colour)
139 fprintf(stream, "#");
142 fprintf(stream, "+");
145 fprintf(stream, "$");
150 fprintf(stream, "\n");
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 '#'
160 void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal)
162 for (int y=0; y < height; ++y)
164 for (int x=0; x < width; ++x)
166 Piece * piece = board[x][y];
169 fprintf(stream, ".");
171 else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
173 switch (piece->colour)
176 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,40);
179 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,40);
184 fprintf(stream, "%c", Piece::tokens[piece->type]);
189 switch (piece->colour)
192 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,41);
196 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,44);
199 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,37,47);
202 //Should never see this
203 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,33,43);
207 fprintf(stream, "#");
210 fprintf(stream, "%c[%d;%d;%dm",0x1B,0,7,0);
212 fprintf(stream, "\n");
218 #ifdef BUILD_GRAPHICS
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
225 void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
228 if (!Graphics::Initialised())
230 fprintf(stderr, "ERROR - Board::Draw called whilst graphics disabled!!!\n");
235 Graphics::ClearScreen();
237 for (int y=0; y < height; ++y)
239 for (int x=0; x < width; ++x)
241 Piece * piece = board[x][y];
244 //Don't display anything
247 else if ((piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
248 || (piece->beenRevealed && showRevealed))
251 Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
256 switch (piece->colour)
259 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
262 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
265 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
268 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
275 Graphics::UpdateScreen();
278 #endif //BUILD_GRAPHICS
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.
288 bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
290 if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
293 Piece * piece = new Piece(newType, newColour);
296 pieces.push_back(piece);
301 * Gets a pointer to a piece at a board location
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
308 Piece * Board::GetPiece(int x, int y)
310 assert(board != NULL);
311 assert(x >= 0 && x < width && y >= 0 && y < height);
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
323 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
327 return MovementResult(MovementResult::NO_BOARD);
329 if (!(x >= 0 && x < width && y >= 0 && y < height))
331 return MovementResult(MovementResult::INVALID_POSITION);
333 Piece * target = board[x][y];
336 return MovementResult(MovementResult::NO_SELECTION);
338 if (!(colour == Piece::NONE || target->colour == colour))
340 return MovementResult(MovementResult::NOT_YOUR_UNIT);
342 if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER)
344 return MovementResult(MovementResult::IMMOBILE_UNIT);
346 if (multiplier > 1 && target->type != Piece::SCOUT)
348 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
350 int x2 = x; int y2 = y;
352 for (int ii=0; ii < multiplier; ++ii)
369 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height))
371 return MovementResult(MovementResult::INVALID_DIRECTION);
373 if (ii < multiplier-1 && board[x2][y2] != NULL)
375 return MovementResult(MovementResult::POSITION_FULL);
378 Piece * defender = board[x2][y2];
379 if (defender == NULL)
382 board[x2][y2] = target;
384 else if (defender->colour != target->colour)
386 defender->beenRevealed = true;
387 target->beenRevealed = true;
389 Piece::Type defenderType = defender->type;
390 Piece::Type attackerType = target->type;
392 if (defender->colour == Piece::NONE)
394 return MovementResult(MovementResult::POSITION_FULL);
396 if (defender->type == Piece::FLAG)
398 winner = target->colour;
399 return MovementResult(MovementResult::VICTORY_FLAG);
401 else if (defender->type == Piece::BOMB)
403 if (target->type == Piece::MINER)
405 RemovePiece(defender);
408 board[x2][y2] = target;
409 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
413 //Use this to destroy only the attacking piece, and not the bomb
417 return MovementResult(MovementResult::DIES, attackerType, defenderType);
420 //Use this to destroy both the bomb and the attacking piece
421 RemovePiece(defender);
426 board[x2][y2] = NULL;
427 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
431 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
433 RemovePiece(defender);
436 board[x2][y2] = target;
437 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
439 else if (target->operator > (*defender))
441 RemovePiece(defender);
444 board[x2][y2] = target;
445 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
447 else if (target->operator==(*defender))// && rand() % 2 == 0)
449 RemovePiece(defender);
454 board[x2][y2] = NULL;
455 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
462 return MovementResult(MovementResult::DIES, attackerType, defenderType);
467 return MovementResult(MovementResult::POSITION_FULL);
469 return MovementResult(MovementResult::OK);
473 * Removes a piece from the board
474 * @param piece The piece to remove
475 * @returns true iff the piece actually existed
477 bool Board::RemovePiece(Piece * piece)
480 for (int x = 0; x < width; ++x)
482 for (int y = 0; y < height; ++y)
484 if (board[x][y] == piece)
492 vector<Piece*>::iterator i = pieces.begin();
493 while (i != pieces.end())
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)
512 int Board::TotalPieceValue(const Piece::Colour & colour) const
515 for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
517 if ((*i)->colour == colour || colour == Piece::BOTH)
519 result += (*i)->PieceValue();
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)
531 int Board::MobilePieces(const Piece::Colour & colour) const
534 for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
536 if ((*i)->colour == colour || colour == Piece::BOTH)
538 if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)