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;
23 Piece::TextureManager::~TextureManager()
25 Array<Texture*>::Iterator i(*this);
33 Texture & Piece::TextureManager::operator[](const LUint & at)
35 while (Array<Texture*>::Size() <= at)
38 sprintf(buffer, "images/piece%lu.bmp", Array<Texture*>::Size());
39 Array<Texture*>::Add(new Texture(buffer, false));
42 return *(Array<Texture*>::operator[](at));
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
51 Piece::Type Piece::GetType(char fromToken)
53 for (int ii=0; ii <= (int)(Piece::BOMB); ++ii)
55 if (tokens[ii] == fromToken)
57 return Type(Piece::NOTHING + ii);
60 return Piece::BOULDER;
64 * Construct a new, empty board
65 * @param newWidth - the width of the board
66 * @param newHeight - the height of the board
68 Board::Board(int newWidth, int newHeight) : winner(Piece::NONE), width(newWidth), height(newHeight), board(NULL), pieces()
70 board = new Piece**[width];
71 for (int x=0; x < width; ++x)
73 board[x] = new Piece*[height];
74 for (int y=0; y < height; ++y)
84 for (int x=0; x < width; ++x)
86 for (int y=0; y < height; ++y)
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 '#'
97 void Board::Print(FILE * stream, const Piece::Colour & reveal)
99 for (int y=0; y < height; ++y)
101 for (int x=0; x < width; ++x)
103 Piece * piece = board[x][y];
106 fprintf(stream, ".");
108 else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
111 fprintf(stream, "%c", Piece::tokens[piece->type]);
117 switch (piece->colour)
121 fprintf(stream, "#");
124 fprintf(stream, "+");
127 fprintf(stream, "$");
132 fprintf(stream, "\n");
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 '#'
142 void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal)
144 for (int y=0; y < height; ++y)
146 for (int x=0; x < width; ++x)
148 Piece * piece = board[x][y];
151 fprintf(stream, ".");
153 else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
155 switch (piece->colour)
158 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,40);
161 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,40);
166 fprintf(stream, "%c", Piece::tokens[piece->type]);
171 switch (piece->colour)
174 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,41);
178 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,44);
181 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,37,47);
184 //Should never see this
185 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,33,43);
189 fprintf(stream, "#");
192 fprintf(stream, "%c[%d;%d;%dm",0x1B,0,7,0);
194 fprintf(stream, "\n");
202 * Draw the board state to graphics
203 * @param reveal - Pieces matching this colour will be revealed. If Piece::BOTH, all pieces will be revealed
204 * @param showRevealed - If true, then all pieces that have taken part in combat will be revealed, regardless of colour.
205 * If false, only pieces matching the colour reveal will be revealed
207 void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
209 if (!Graphics::Initialised())
211 fprintf(stderr, "ERROR - Board::Draw called whilst graphics disabled!!!\n");
216 Graphics::ClearScreen();
218 for (int y=0; y < height; ++y)
220 for (int x=0; x < width; ++x)
222 Piece * piece = board[x][y];
225 //Don't display anything
228 else if ((piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
229 || (piece->beenRevealed && showRevealed))
232 Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
237 switch (piece->colour)
240 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
243 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
246 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
249 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
256 Graphics::UpdateScreen();
261 * Adds a piece to the board
262 * @param x - x-coord to place the piece at, starting at zero, must be less than board width
263 * @param y - y-coord to place the piece at, starting at zero, must be less than board height
264 * @param newType - the Type of the piece
265 * @param newColour - the Colour of the piece
266 * @returns true if and only if the piece could be successfully added.
268 bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
270 if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
273 Piece * piece = new Piece(newType, newColour);
276 pieces.push_back(piece);
281 * Gets a pointer to a piece at a board location
283 * @param x - x-coord of the piece
284 * @param y - y-coord of the piece
285 * @returns pointer to the piece, or NULL if the board location was empty
286 * @throws error if board is null or coords are invalid
288 Piece * Board::GetPiece(int x, int y)
290 assert(board != NULL);
291 assert(x >= 0 && x < width && y >= 0 && y < height);
296 * Moves a piece at a specified position in the specified direction, handles combat if necessary, updates state of the board
297 * @param x - x-coord of the piece
298 * @param y - y-coord of the piece
299 * @param direction - Direction in which to move (UP, DOWN, LEFT or RIGHT)
300 * @param colour - Colour which the piece must match for the move to be valid
301 * @returns A MovementResult which indicates the result of the move
303 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
307 return MovementResult(MovementResult::NO_BOARD);
309 if (!(x >= 0 && x < width && y >= 0 && y < height))
311 return MovementResult(MovementResult::INVALID_POSITION);
313 Piece * target = board[x][y];
316 return MovementResult(MovementResult::NO_SELECTION);
318 if (!(colour == Piece::NONE || target->colour == colour))
320 return MovementResult(MovementResult::NOT_YOUR_UNIT);
322 if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER)
324 return MovementResult(MovementResult::IMMOBILE_UNIT);
326 if (multiplier > 1 && target->type != Piece::SCOUT)
328 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
330 int x2 = x; int y2 = y;
332 for (int ii=0; ii < multiplier; ++ii)
349 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height))
351 return MovementResult(MovementResult::INVALID_DIRECTION);
353 if (ii < multiplier-1 && board[x2][y2] != NULL)
355 return MovementResult(MovementResult::POSITION_FULL);
358 Piece * defender = board[x2][y2];
359 if (defender == NULL)
362 board[x2][y2] = target;
364 else if (defender->colour != target->colour)
366 defender->beenRevealed = true;
367 target->beenRevealed = true;
369 Piece::Type defenderType = defender->type;
370 Piece::Type attackerType = target->type;
372 if (defender->colour == Piece::NONE)
374 return MovementResult(MovementResult::POSITION_FULL);
376 if (defender->type == Piece::FLAG)
378 winner = target->colour;
379 return MovementResult(MovementResult::VICTORY);
381 else if (defender->type == Piece::BOMB)
383 if (target->type == Piece::MINER)
385 RemovePiece(defender);
388 board[x2][y2] = target;
389 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
393 //Use this to destroy only the attacking piece, and not the bomb
397 return MovementResult(MovementResult::DIES, attackerType, defenderType);
400 //Use this to destroy both the bomb and the attacking piece
401 RemovePiece(defender);
406 board[x2][y2] = NULL;
407 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
411 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
413 RemovePiece(defender);
416 board[x2][y2] = target;
417 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
419 else if (target->operator > (*defender))
421 RemovePiece(defender);
424 board[x2][y2] = target;
425 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
427 else if (target->operator==(*defender))// && rand() % 2 == 0)
429 RemovePiece(defender);
434 board[x2][y2] = NULL;
435 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
442 return MovementResult(MovementResult::DIES, attackerType, defenderType);
447 return MovementResult(MovementResult::POSITION_FULL);
449 return MovementResult(MovementResult::OK);
453 * Removes a piece from the board
454 * @param piece The piece to remove
455 * @returns true iff the piece actually existed
457 bool Board::RemovePiece(Piece * piece)
460 for (int x = 0; x < width; ++x)
462 for (int y = 0; y < height; ++y)
464 if (board[x][y] == piece)
472 vector<Piece*>::iterator i = pieces.begin();
473 while (i != pieces.end())
487 * Returns the total value of pieces belonging to colour
488 * @param colour the colour
489 * @returns the total value of pieces belonging to colour.
490 * (Redundant repetition <3)
492 int Board::TotalPieceValue(const Piece::Colour & colour) const
495 for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
497 if ((*i)->colour == colour || colour == Piece::BOTH)
499 result += (*i)->PieceValue();
506 * Returns the total number of mobile pieces belonging to colour
507 * @param colour the colour
508 * @returns the total value of mobile pieces belonging to colour.
509 * (Redundant repetition <3)
511 int Board::MobilePieces(const Piece::Colour & colour) const
514 for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
516 if ((*i)->colour == colour || colour == Piece::BOTH)
518 if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)