From 84c3af2bb13e8de1b3634548694bee3c59231989 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sun, 2 Feb 2025 23:05:42 +0100 Subject: [PATCH] fully support FEN strings now --- cpp/src/board.cpp | 21 +++++++++++++++++++-- cpp/src/board.hpp | 2 ++ cpp/tests/fen.cpp | 13 +++++++------ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/cpp/src/board.cpp b/cpp/src/board.cpp index 440aafb..5203d82 100644 --- a/cpp/src/board.cpp +++ b/cpp/src/board.cpp @@ -82,6 +82,15 @@ Board Board::setup_fen_position(std::string fen) { index++; } + // -- Half move clock + index = fen.find(' ', index) + 1; + board.n_half_moves = + std::stoi(fen.substr(index, fen.find(' ', index + 1) - index)); + + // -- Full move number + index = fen.find(' ', index) + 1; + board.n_full_moves = std::stoi(fen.substr(index)); + return board; } @@ -127,7 +136,7 @@ std::string Board::to_fen() const { ret += white_to_play ? 'w' : 'b'; ret += " "; - // Castling Rights + // -- Castling Rights if (w_castle_rights == CastleSide::NeitherSide && b_castle_rights == CastleSide::NeitherSide) ret += '-'; @@ -143,10 +152,18 @@ std::string Board::to_fen() const { } ret += ' '; - // En passant target + // -- En passant target ret += en_passant_target == -1 ? "-" : Coords::from_index(en_passant_target).to_algebraic(); + ret += ' '; + + // -- Half move clock + ret += std::to_string(n_half_moves); + ret += ' '; + + // -- Full moves number + ret += std::to_string(n_full_moves); return ret; } diff --git a/cpp/src/board.hpp b/cpp/src/board.hpp index 750a7b0..9a04afb 100644 --- a/cpp/src/board.hpp +++ b/cpp/src/board.hpp @@ -16,6 +16,8 @@ struct Board { int8_t w_castle_rights = CastleSide::NeitherSide; int8_t b_castle_rights = CastleSide::NeitherSide; int8_t en_passant_target = -1; + uint8_t n_half_moves = 0; + uint8_t n_full_moves = 0; static Board setup_fen_position(std::string fen); diff --git a/cpp/tests/fen.cpp b/cpp/tests/fen.cpp index 68ff519..d9d51bd 100644 --- a/cpp/tests/fen.cpp +++ b/cpp/tests/fen.cpp @@ -2,21 +2,22 @@ #include "lib.hpp" int main() { - std::string pos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -"; + std::string pos = + "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); - pos = "r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1 b Qk -"; + pos = "r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1 b Qk - 0 1"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); - pos = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3"; + pos = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); - pos = "4k2r/6r1/8/8/8/8/3R4/R3K3 w Qk -"; + pos = "4k2r/6r1/8/8/8/8/3R4/R3K3 w Qk - 0 1"; 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 - - 0 1"; 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 - - 99 50"; ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen()); }