implemented the en passant target support for FEN

strings
This commit is contained in:
Karma Riuk 2025-02-02 22:52:23 +01:00
parent 2493892b05
commit 1ff4b6b1f4
4 changed files with 24 additions and 6 deletions

View File

@ -73,6 +73,16 @@ Board Board::setup_fen_position(std::string fen) {
}
}
// -- En passant target
if (fen[index] != '-') {
Coords c = Coords::from_algebraic(fen.substr(index, index + 2));
index += 2;
board.en_passant_target = c.to_index();
} else {
index++;
}
return board;
}
@ -131,6 +141,12 @@ std::string Board::to_fen() const {
if (b_castle_rights & CastleSide::QueenSide)
ret += 'q';
}
ret += ' ';
// En passant target
ret += en_passant_target == -1
? "-"
: Coords::from_index(en_passant_target).to_algebraic();
return ret;
}

View File

@ -15,6 +15,7 @@ struct Board {
bool white_to_play = true;
int8_t w_castle_rights = CastleSide::NeitherSide;
int8_t b_castle_rights = CastleSide::NeitherSide;
int8_t en_passant_target = -1;
static Board setup_fen_position(std::string fen);

View File

@ -6,5 +6,6 @@ int main(int argc, char* argv[]) {
std::string pos =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
Board b = Board::setup_fen_position(pos);
sizeof(b);
return 0;
}

View File

@ -2,21 +2,21 @@
#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 -";
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 -";
ASSERT_EQUALS(pos, Board::setup_fen_position(pos).to_fen());
pos = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq";
pos = "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3";
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 -";
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());
}