now support the castling sides of the FEN string

This commit is contained in:
Karma Riuk 2025-02-02 22:05:33 +01:00
parent 5549c77177
commit bdd8011577
2 changed files with 51 additions and 6 deletions

View File

@ -1,6 +1,7 @@
#include "board.hpp" #include "board.hpp"
#include "coords.hpp" #include "coords.hpp"
#include "move.hpp"
#include "pieces/piece.hpp" #include "pieces/piece.hpp"
#include <algorithm> #include <algorithm>
@ -45,6 +46,33 @@ Board Board::setup_fen_position(std::string fen) {
int index = fen.find(' '); int index = fen.find(' ');
index++; index++;
board.white_to_play = fen[index] == 'w'; 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; return board;
} }
@ -87,6 +115,23 @@ std::string Board::to_fen() const {
// -- Active colour // -- Active colour
ret += white_to_play ? 'w' : 'b'; 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; return ret;
} }

View File

@ -2,21 +2,21 @@
#include "lib.hpp" #include "lib.hpp"
int main() { 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()); 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()); 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()); 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()); 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()); 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()); ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen());
} }