* @param newWidth - the width of the board
* @param newHeight - the height of the board
*/
-Board::Board(int newWidth, int newHeight) : winner(Piece::NONE), width(newWidth), height(newHeight), board(NULL)
+Board::Board(int newWidth, int newHeight) : winner(Piece::NONE), width(newWidth), height(newHeight), board(NULL), pieces()
{
board = new Piece**[width];
for (int x=0; x < width; ++x)
Piece * piece = new Piece(newType, newColour);
board[x][y] = piece;
+
+ pieces.push_back(piece);
return true;
}
{
if (target->type == Piece::MINER)
{
-
+ RemovePiece(defender);
delete defender;
board[x][y] = NULL;
board[x2][y2] = target;
}
else
{
+ RemovePiece(defender);
+ RemovePiece(target);
delete defender;
delete target;
board[x][y] = NULL;
}
else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
{
+ RemovePiece(defender);
delete defender;
board[x][y] = NULL;
board[x2][y2] = target;
}
else if (target->operator > (*defender))
{
+ RemovePiece(defender);
delete defender;
board[x][y] = NULL;
board[x2][y2] = target;
}
else if (target->operator==(*defender) && rand() % 2 == 0)
{
+ RemovePiece(defender);
delete defender;
board[x][y] = NULL;
board[x2][y2] = target;
}
else
{
+ RemovePiece(target);
delete target;
board[x][y] = NULL;
return MovementResult(MovementResult::DIES, attackerType, defenderType);
return MovementResult(MovementResult::OK);
}
+/**
+ * Removes a piece from the board
+ * @param piece The piece to remove
+ * @returns true iff the piece actually existed
+ */
+bool Board::RemovePiece(Piece * piece)
+{
+ bool result = false;
+ for (int x = 0; x < width; ++x)
+ {
+ for (int y = 0; y < height; ++y)
+ {
+ if (board[x][y] == piece)
+ {
+ result = true;
+ board[x][y] = NULL;
+ }
+ }
+ }
+
+ vector<Piece*>::iterator i = pieces.begin();
+ while (i != pieces.end())
+ {
+ if ((*i) == piece)
+ {
+ i = pieces.erase(i);
+ result = true;
+ continue;
+ }
+ ++i;
+ }
+ return result;
+}
+
+/**
+ * Returns the total value of pieces belonging to colour
+ * @param colour the colour
+ * @returns the total value of pieces belonging to colour.
+ * (Redundant repetition <3)
+ */
+int Board::TotalPieceValue(const Piece::Colour & colour) const
+{
+ int result = 0;
+ for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
+ {
+ if ((*i)->colour == colour || colour == Piece::BOTH)
+ {
+ result += (*i)->PieceValue();
+ }
+ }
+ return result;
+}
+
+/**
+ * Returns the total number of mobile pieces belonging to colour
+ * @param colour the colour
+ * @returns the total value of mobile pieces belonging to colour.
+ * (Redundant repetition <3)
+ */
+int Board::MobilePieces(const Piece::Colour & colour) const
+{
+ int result = 0;
+ for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
+ {
+ if ((*i)->colour == colour || colour == Piece::BOTH)
+ {
+ if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)
+ result++;
+ }
+ }
+ return result;
+}