diff --git a/cpp/src/board.cpp b/cpp/src/board.cpp index 1331409..dc1a656 100644 --- a/cpp/src/board.cpp +++ b/cpp/src/board.cpp @@ -1,6 +1,7 @@ #include "board.hpp" #include "coords.hpp" +#include "move.hpp" #include "pieces/piece.hpp" #include @@ -45,6 +46,33 @@ Board Board::setup_fen_position(std::string fen) { int index = fen.find(' '); index++; board.white_to_play = fen[index] == 'w'; + + // Castling Rights + index += 2; + for (char symbol : fen.substr(index, fen.find(' ', index + 1))) { + index++; + if (symbol == ' ' || symbol == '-') { + if (symbol == '-') + index++; + break; + } + + switch (symbol) { + case 'K': + board.w_castle_rights |= CastleSide::KingSide; + break; + case 'Q': + board.w_castle_rights |= CastleSide::QueenSide; + break; + case 'k': + board.b_castle_rights |= CastleSide::KingSide; + break; + case 'q': + board.b_castle_rights |= CastleSide::QueenSide; + break; + } + } + return board; } @@ -87,6 +115,23 @@ std::string Board::to_fen() const { // -- Active colour ret += white_to_play ? 'w' : 'b'; + ret += " "; + + // Castling Rights + if (w_castle_rights == CastleSide::NeitherSide + && b_castle_rights == CastleSide::NeitherSide) + ret += '-'; + else { + if (w_castle_rights & CastleSide::KingSide) + ret += 'K'; + if (w_castle_rights & CastleSide::QueenSide) + ret += 'Q'; + if (b_castle_rights & CastleSide::KingSide) + ret += 'k'; + if (b_castle_rights & CastleSide::QueenSide) + ret += 'q'; + } + return ret; } diff --git a/cpp/tests/fen.cpp b/cpp/tests/fen.cpp index 9a5030f..2a45dcd 100644 --- a/cpp/tests/fen.cpp +++ b/cpp/tests/fen.cpp @@ -2,21 +2,21 @@ #include "lib.hpp" int main() { - std::string pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w"; + std::string pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); - pos = "r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1 b"; + pos = "r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1 b Qk"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); - pos = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b"; + pos = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); - pos = "4k2r/6r1/8/8/8/8/3R4/R3K3 w"; + pos = "4k2r/6r1/8/8/8/8/3R4/R3K3 w Qk"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); - pos = "8/8/8/4p1K1/2k1P3/8/8/8 b"; + pos = "8/8/8/4p1K1/2k1P3/8/8/8 b -"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); - pos = "8/5k2/3p4/1p1Pp2p/pP2Pp1P/P4P1K/8/8 b"; + pos = "8/5k2/3p4/1p1Pp2p/pP2Pp1P/P4P1K/8/8 b -"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); }