37 lines
1.6 KiB
C++

#include "Knight.hpp"
/**
* Creates a new knight with a specified color and position.
* @param color The color of the knight.
* @param position The position of the knight.
*/
Knight::Knight(ChessPieceColor color, ChessPiecePosition position) : ChessPiece(color, position) {
this->_char = this->_CHAR_KNIGHT;
this->_unicode = {"\u265E", "\u2658"};
}
/**
* Gets all chess piece positions the knight can move next.
* The set of chess piece positions will be ignored, because the knight 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 knight can move next.
*/
std::set<ChessPiecePosition> Knight::GetNextPositions(Chessboard* chessboard, std::set<ChessPiecePosition> ignore) {
std::set<ChessPiecePosition> positions;
// 3.6. The knight may move to one of the squares nearest to that on which it stands but not on the same rank, file or diagonal.
for (std::string move : {"T2L1", "T2R1", "L2T1", "L2B1", "B2L1", "B2R1", "R2T1", "R2B1"}) {
ChessPiecePosition* nextPosition = this->GetPosition().NextFromString(move);
if (nextPosition == nullptr) continue;
// 3.1. It is not permitted to move a piece to a square occupied by a piece of the same colour.
// 3.1.1. If a piece moves to a square occupied by an opponent's piece the latter is captured and removed from the chessboard as part of the same move.
if (chessboard->IsEmptyField(nextPosition) || chessboard->GetChessPiece(nextPosition)->GetColor() != this->GetColor()) {
positions.insert(*nextPosition);
}
}
return positions;
}