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)
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. All others will be shown as blank coloured squares.
205 void Board::Draw(const Piece::Colour & reveal)
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))
229 Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
234 switch (piece->colour)
237 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
240 Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
243 Piece::textures[(int)(Piece::BOULDER)].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));
253 Graphics::UpdateScreen();
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.
265 bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
267 if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
270 Piece * piece = new Piece(newType, newColour);
276 * Gets a pointer to a piece at a board location
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
283 Piece * Board::GetPiece(int x, int y)
285 assert(board != NULL);
286 assert(x >= 0 && x < width && y >= 0 && y < height);
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
298 MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
302 return MovementResult(MovementResult::NO_BOARD);
304 if (!(x >= 0 && x < width && y >= 0 && y < height))
306 return MovementResult(MovementResult::INVALID_POSITION);
308 Piece * target = board[x][y];
311 return MovementResult(MovementResult::NO_SELECTION);
313 if (!(colour == Piece::NONE || target->colour == colour))
315 return MovementResult(MovementResult::NOT_YOUR_UNIT);
317 if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER)
319 return MovementResult(MovementResult::IMMOBILE_UNIT);
321 if (multiplier > 1 && target->type != Piece::SCOUT)
323 return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
325 int x2 = x; int y2 = y;
327 for (int ii=0; ii < multiplier; ++ii)
344 if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height))
346 return MovementResult(MovementResult::INVALID_DIRECTION);
348 if (ii < multiplier-1 && board[x2][y2] != NULL)
350 return MovementResult(MovementResult::POSITION_FULL);
353 Piece * defender = board[x2][y2];
354 if (defender == NULL)
357 board[x2][y2] = target;
359 else if (defender->colour != target->colour)
361 Piece::Type defenderType = defender->type;
362 Piece::Type attackerType = target->type;
364 if (defender->colour == Piece::NONE)
366 return MovementResult(MovementResult::POSITION_FULL);
368 if (defender->type == Piece::FLAG)
370 winner = target->colour;
371 return MovementResult(MovementResult::VICTORY);
373 else if (defender->type == Piece::BOMB)
375 if (target->type == Piece::MINER)
380 board[x2][y2] = target;
381 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
388 board[x2][y2] = NULL;
389 return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
392 else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
396 board[x2][y2] = target;
397 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
399 else if (target->operator > (*defender))
403 board[x2][y2] = target;
404 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
406 else if (target->operator==(*defender) && rand() % 2 == 0)
410 board[x2][y2] = target;
411 return MovementResult(MovementResult::KILLS, attackerType, defenderType);
417 return MovementResult(MovementResult::DIES, attackerType, defenderType);
422 return MovementResult(MovementResult::POSITION_FULL);
424 return MovementResult(MovementResult::OK);