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);
347 return MovementResult(MovementResult::INVALID_DIRECTION); //Don't allow moves that don't actually move forward
348 if (multiplier > 1 && target->type != Piece::SCOUT)
350 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
352 int x2 = x; int y2 = y;
354 for (int ii=0; ii < multiplier; ++ii)
371 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height))
373 return MovementResult(MovementResult::INVALID_DIRECTION);
375 if (ii < multiplier-1 && board[x2][y2] != NULL)
377 return MovementResult(MovementResult::POSITION_FULL);
380 Piece * defender = board[x2][y2];
381 if (defender == NULL)
384 board[x2][y2] = target;
386 else if (defender->colour != target->colour)
388 defender->beenRevealed = true;
389 target->beenRevealed = true;
391 Piece::Type defenderType = defender->type;
392 Piece::Type attackerType = target->type;
394 if (defender->colour == Piece::NONE)
396 return MovementResult(MovementResult::POSITION_FULL);
398 if (defender->type == Piece::FLAG)
400 winner = target->colour;
401 return MovementResult(MovementResult::VICTORY_FLAG);
403 else if (defender->type == Piece::BOMB)
405 if (target->type == Piece::MINER)
407 RemovePiece(defender);
410 board[x2][y2] = target;
411 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
415 //Use this to destroy only the attacking piece, and not the bomb
419 return MovementResult(MovementResult::DIES, attackerType, defenderType);
422 //Use this to destroy both the bomb and the attacking piece
423 RemovePiece(defender);
428 board[x2][y2] = NULL;
429 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
433 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
435 RemovePiece(defender);
438 board[x2][y2] = target;
439 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
441 else if (target->operator > (*defender))
443 RemovePiece(defender);
446 board[x2][y2] = target;
447 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
449 else if (target->operator==(*defender))// && rand() % 2 == 0)
451 RemovePiece(defender);
456 board[x2][y2] = NULL;
457 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
464 return MovementResult(MovementResult::DIES, attackerType, defenderType);
469 return MovementResult(MovementResult::POSITION_FULL);
471 return MovementResult(MovementResult::OK);
475 * Removes a piece from the board
476 * @param piece The piece to remove
477 * @returns true iff the piece actually existed
479 bool Board::RemovePiece(Piece * piece)
482 for (int x = 0; x < width; ++x)
484 for (int y = 0; y < height; ++y)
486 if (board[x][y] == piece)
494 vector<Piece*>::iterator i = pieces.begin();
495 while (i != pieces.end())
509 * Returns the total value of pieces belonging to colour
510 * @param colour the colour
511 * @returns the total value of pieces belonging to colour.
512 * (Redundant repetition <3)
514 int Board::TotalPieceValue(const Piece::Colour & colour) const
517 for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
519 if ((*i)->colour == colour || colour == Piece::BOTH)
521 result += (*i)->PieceValue();
528 * Returns the total number of mobile pieces belonging to colour
529 * @param colour the colour
530 * @returns the total value of mobile pieces belonging to colour.
531 * (Redundant repetition <3)
533 int Board::MobilePieces(const Piece::Colour & colour) const
536 for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
538 if ((*i)->colour == colour || colour == Piece::BOTH)
540 if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)