From 061f2af803f7abfb4f655ef67769e79db705a3e1 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 31 Jul 2026 19:58:57 +0200 Subject: adding enpassant to fen parser --- src/board/fen.cpp | 67 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 28 deletions(-) (limited to 'src/board/fen.cpp') diff --git a/src/board/fen.cpp b/src/board/fen.cpp index ca64e47..ccd8b6f 100644 --- a/src/board/fen.cpp +++ b/src/board/fen.cpp @@ -14,18 +14,20 @@ void setBoardFen(std::string fen, Game *g) { int file = 0; int rank = 0; + Position enpassant = {}; enum parserState { POSITION, TURN, CASTLE, ENPASSANT, - CLOCK1, - CLOCK2, + HALF_MOVE_CLOCK, + FULL_MOVE_CLOCK, }; parserState state = POSITION; for (uint i = 0; i < fen.length(); i++) { - if (fen[i] == ' ') { + char c = fen[i]; + if (c == ' ') { if (state == POSITION) { state = TURN; continue; @@ -39,55 +41,63 @@ void setBoardFen(std::string fen, Game *g) { continue; } if (state == ENPASSANT) { - state = CLOCK1; + state = HALF_MOVE_CLOCK; continue; } - if (state == CLOCK1) { - state = CLOCK2; + if (state == HALF_MOVE_CLOCK) { + state = FULL_MOVE_CLOCK; continue; } } if (state == ENPASSANT) { + if (toupper(c) >= 'A' && toupper(c) <= 'H') { + enpassant.file = toupper(c) - 'A'; + g->canEnpassant = true; + } + if (c >= '1' && c <= '8') { + enpassant.rank = c - '0'; + g->canEnpassant = true; + } continue; } if (state == CASTLE) { - if (fen[i] == '-') { + if (c == '-') { g->blackCastleKing = false; g->blackCastleQueen = false; g->whiteCastleKing = false; g->whiteCastleQueen = false; continue; } - if (fen[i] == 'K') { + if (c == 'K') { g->whiteCastleKing = true; continue; } - if (fen[i] == 'Q') { + if (c == 'Q') { g->whiteCastleQueen = true; continue; } - if (fen[i] == 'k') { + if (c == 'k') { g->blackCastleKing = true; continue; } - if (fen[i] == 'q') { + if (c == 'q') { g->blackCastleQueen = true; continue; } } if (state == TURN) { - if (toupper(fen[i]) == 'W') { + if (toupper(c) == 'W') { g->turn = WHITE; continue; } - if (toupper(fen[i]) == 'B') { + if (toupper(c) == 'B') { g->turn = BLACK; continue; } } if (state == POSITION) { - if (isdigit(fen[i])) { - int move = fen[i] - '0'; + if (isdigit(c)) { + int move = c - '0'; file += move; if (file > 8) { std::println("Fatal: file was to big"); @@ -97,7 +107,7 @@ void setBoardFen(std::string fen, Game *g) { } continue; } - if (fen[i] == '/') { + if (c == '/') { file = 0; rank++; if (rank >= 8) { @@ -108,38 +118,39 @@ void setBoardFen(std::string fen, Game *g) { } continue; }; - if (toupper(fen[i]) == 'N') { - SetPiece(rank * 8 + file, KNIGHT, isupper(fen[i]), g); + if (toupper(c) == 'N') { + SetPiece(rank * 8 + file, KNIGHT, isupper(c), g); file++; continue; }; - if (toupper(fen[i]) == 'R') { - SetPiece(rank * 8 + file, ROOK, isupper(fen[i]), g); + if (toupper(c) == 'R') { + SetPiece(rank * 8 + file, ROOK, isupper(c), g); file++; continue; } - if (toupper(fen[i]) == 'Q') { - SetPiece(rank * 8 + file, QUEEN, isupper(fen[i]), g); + if (toupper(c) == 'Q') { + SetPiece(rank * 8 + file, QUEEN, isupper(c), g); file++; continue; } - if (toupper(fen[i]) == 'B') { - SetPiece(rank * 8 + file, BISHOP, isupper(fen[i]), g); + if (toupper(c) == 'B') { + SetPiece(rank * 8 + file, BISHOP, isupper(c), g); file++; continue; } - if (toupper(fen[i]) == 'P') { - SetPiece(rank * 8 + file, PAWN, isupper(fen[i]), g); + if (toupper(c) == 'P') { + SetPiece(rank * 8 + file, PAWN, isupper(c), g); file++; continue; } - if (toupper(fen[i]) == 'K') { - SetPiece(rank * 8 + file, KING, isupper(fen[i]), g); + if (toupper(c) == 'K') { + SetPiece(rank * 8 + file, KING, isupper(c), g); file++; continue; } } break; } + g->enPassant = enpassant; UpdateBitboards(g); } -- cgit v1.2.3