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 * Construct a new, empty board
62 * @param newWidth - the width of the board
63 * @param newHeight - the height of the board
65 Board::Board(int newWidth, int newHeight) : winner(Piece::NONE), width(newWidth), height(newHeight), board(NULL), pieces()
67 board = new Piece**[width];
68 for (int x=0; x < width; ++x)
70 board[x] = new Piece*[height];
71 for (int y=0; y < height; ++y)
81 for (int x=0; x < width; ++x)
83 for (int y=0; y < height; ++y)
90 * Print textual representation of the board to a stream
91 * @param stream - the stream to print information to
92 * @param reveal - Pieces matching this colour will have their identify revealed, other pieces will be shown as '#'
94 void Board::Print(FILE * stream, const Piece::Colour & reveal)
96 for (int y=0; y < height; ++y)
98 for (int x=0; x < width; ++x)
100 Piece * piece = board[x][y];
103 fprintf(stream, ".");
105 else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
108 fprintf(stream, "%c", Piece::tokens[piece->type]);
114 switch (piece->colour)
118 fprintf(stream, "#");
121 fprintf(stream, "+");
124 fprintf(stream, "$");
129 fprintf(stream, "\n");
135 * Print textual representation of the board to a stream
136 * @param stream - the stream to print information to
137 * @param reveal - Pieces matching this colour will have their identify revealed, other pieces will be shown as '#'
139 void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal)
141 for (int y=0; y < height; ++y)
143 for (int x=0; x < width; ++x)
145 Piece * piece = board[x][y];
148 fprintf(stream, ".");
150 else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
152 switch (piece->colour)
155 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,40);
158 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,40);
163 fprintf(stream, "%c", Piece::tokens[piece->type]);
168 switch (piece->colour)
171 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,41);
175 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,44);
178 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,37,47);
181 //Should never see this
182 fprintf(stream, "%c[%d;%d;%dm",0x1B,1,33,43);
186 fprintf(stream, "#");
189 fprintf(stream, "%c[%d;%d;%dm",0x1B,0,7,0);
191 fprintf(stream, "\n");
197 #ifdef BUILD_GRAPHICS
199 * Draw the board state to graphics
200 * @param reveal - Pieces matching this colour will be revealed. If Piece::BOTH, all pieces will be revealed
201 * @param showRevealed - If true, then all pieces that have taken part in combat will be revealed, regardless of colour.
202 * If false, only pieces matching the colour reveal will be revealed
204 void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
207 if (!Graphics::Initialised())
209 fprintf(stderr, "ERROR - Board::Draw called whilst graphics disabled!!!\n");
214 Graphics::ClearScreen();
216 for (int y=0; y < height; ++y)
218 for (int x=0; x < width; ++x)
220 Piece * piece = board[x][y];
223 //Don't display anything
226 else if ((piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
227 || (piece->beenRevealed && showRevealed))
230 Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
235 switch (piece->colour)
238 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
241 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
244 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
247 Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
254 Graphics::UpdateScreen();
257 #endif //BUILD_GRAPHICS
260 * Adds a piece to the board
261 * @param x - x-coord to place the piece at, starting at zero, must be less than board width
262 * @param y - y-coord to place the piece at, starting at zero, must be less than board height
263 * @param newType - the Type of the piece
264 * @param newColour - the Colour of the piece
265 * @returns true if and only if the piece could be successfully added.
267 bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
269 if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
272 Piece * piece = new Piece(newType, newColour);
275 pieces.push_back(piece);
280 * Gets a pointer to a piece at a board location
282 * @param x - x-coord of the piece
283 * @param y - y-coord of the piece
284 * @returns pointer to the piece, or NULL if the board location was empty
285 * @throws error if board is null or coords are invalid
287 Piece * Board::GetPiece(int x, int y)
289 assert(board != NULL);
290 assert(x >= 0 && x < width && y >= 0 && y < height);
295 * Moves a piece at a specified position in the specified direction, handles combat if necessary, updates state of the board
296 * @param x - x-coord of the piece
297 * @param y - y-coord of the piece
298 * @param direction - Direction in which to move (UP, DOWN, LEFT or RIGHT)
299 * @param colour - Colour which the piece must match for the move to be valid
300 * @returns A MovementResult which indicates the result of the move
302 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
306 return MovementResult(MovementResult::NO_BOARD);
308 if (!(x >= 0 && x < width && y >= 0 && y < height))
310 return MovementResult(MovementResult::INVALID_POSITION);
312 Piece * target = board[x][y];
315 return MovementResult(MovementResult::NO_SELECTION);
317 if (!(colour == Piece::NONE || target->colour == colour))
319 return MovementResult(MovementResult::NOT_YOUR_UNIT);
321 if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER)
323 return MovementResult(MovementResult::IMMOBILE_UNIT);
325 if (multiplier > 1 && target->type != Piece::SCOUT)
327 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
329 int x2 = x; int y2 = y;
331 for (int ii=0; ii < multiplier; ++ii)
348 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height))
350 return MovementResult(MovementResult::INVALID_DIRECTION);
352 if (ii < multiplier-1 && board[x2][y2] != NULL)
354 return MovementResult(MovementResult::POSITION_FULL);
357 Piece * defender = board[x2][y2];
358 if (defender == NULL)
361 board[x2][y2] = target;
363 else if (defender->colour != target->colour)
365 defender->beenRevealed = true;
366 target->beenRevealed = true;
368 Piece::Type defenderType = defender->type;
369 Piece::Type attackerType = target->type;
371 if (defender->colour == Piece::NONE)
373 return MovementResult(MovementResult::POSITION_FULL);
375 if (defender->type == Piece::FLAG)
377 winner = target->colour;
378 return MovementResult(MovementResult::VICTORY);
380 else if (defender->type == Piece::BOMB)
382 if (target->type == Piece::MINER)
384 RemovePiece(defender);
387 board[x2][y2] = target;
388 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
392 //Use this to destroy only the attacking piece, and not the bomb
396 return MovementResult(MovementResult::DIES, attackerType, defenderType);
399 //Use this to destroy both the bomb and the attacking piece
400 RemovePiece(defender);
405 board[x2][y2] = NULL;
406 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
410 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
412 RemovePiece(defender);
415 board[x2][y2] = target;
416 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
418 else if (target->operator > (*defender))
420 RemovePiece(defender);
423 board[x2][y2] = target;
424 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
426 else if (target->operator==(*defender))// && rand() % 2 == 0)
428 RemovePiece(defender);
433 board[x2][y2] = NULL;
434 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
441 return MovementResult(MovementResult::DIES, attackerType, defenderType);
446 return MovementResult(MovementResult::POSITION_FULL);
448 return MovementResult(MovementResult::OK);
452 * Removes a piece from the board
453 * @param piece The piece to remove
454 * @returns true iff the piece actually existed
456 bool Board::RemovePiece(Piece * piece)
459 for (int x = 0; x < width; ++x)
461 for (int y = 0; y < height; ++y)
463 if (board[x][y] == piece)
471 vector<Piece*>::iterator i = pieces.begin();
472 while (i != pieces.end())
486 * Returns the total value of pieces belonging to colour
487 * @param colour the colour
488 * @returns the total value of pieces belonging to colour.
489 * (Redundant repetition <3)
491 int Board::TotalPieceValue(const Piece::Colour & colour) const
494 for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
496 if ((*i)->colour == colour || colour == Piece::BOTH)
498 result += (*i)->PieceValue();
505 * Returns the total number of mobile pieces belonging to colour
506 * @param colour the colour
507 * @returns the total value of mobile pieces belonging to colour.
508 * (Redundant repetition <3)
510 int Board::MobilePieces(const Piece::Colour & colour) const
513 for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
515 if ((*i)->colour == colour || colour == Piece::BOTH)
517 if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)