44 lines
2.0 KiB
C++
44 lines
2.0 KiB
C++
#include "King.hpp"
|
|
|
|
/**
|
|
* Creates a new king with a specified color and position.
|
|
* @param color The color of the king.
|
|
* @param position The position of the king.
|
|
*/
|
|
King::King(ChessPieceColor color, ChessPiecePosition position) : ChessPiece(color, position) {
|
|
this->_char = this->_CHAR_KING;
|
|
this->_unicode = {"\u265A", "\u2654"};
|
|
}
|
|
|
|
/**
|
|
* Gets all chess piece positions the king can move next.
|
|
* The set of chess piece positions will be ignored, because the king has a fixed movement.
|
|
* @param chessboard A pointer to the chessboard.
|
|
* @param ignore A set of chess piece positions to be ignored.
|
|
* @return A set of chess piece positions the rook can move next.
|
|
*/
|
|
std::set<ChessPiecePosition> King::GetNextPositions(Chessboard* chessboard, std::set<ChessPiecePosition> ignore) {
|
|
std::set<ChessPiecePosition> positions;
|
|
|
|
// There are two different ways of moving the king:
|
|
// 3.8.1. by moving to an adjoining square.
|
|
// 3.8.2. by 'castling'. This is a move of the king and either rook of the same colour along the player's first rank,
|
|
// counting as a single move of the king and executed as follows: the king is transferred from its original square
|
|
// two squares towards the rook on its original square, then that rook is transferred to the square the king
|
|
// has just crossed. -> is implemented directly on the move function.
|
|
for (std::string move : {"T1", "T1L1", "T1R1", "B1", "B1L1", "B1R1", "L1", "R1"}) {
|
|
ChessPiecePosition* nextPosition = this->GetPosition().NextFromString(move);
|
|
if (nextPosition == nullptr) continue;
|
|
|
|
if (chessboard->IsEmptyField(nextPosition) || chessboard->GetChessPiece(nextPosition)->GetColor() != this->GetColor()) {
|
|
ChessPieceColor opponentColor = (this->GetColor() == ChessPieceColor::Black) ? ChessPieceColor::White : ChessPieceColor::Black;
|
|
|
|
if (chessboard->IsPositionUnderAttack(*nextPosition, opponentColor) == false) {
|
|
positions.insert(*nextPosition);
|
|
}
|
|
}
|
|
}
|
|
|
|
return positions;
|
|
}
|