Fix fen/zobrist en-passant inconsistency

This commit is contained in:
Sebastian Lague 2023-07-23 22:34:40 +02:00
parent 085baea286
commit 4cdfc57f04

View file

@ -80,8 +80,12 @@ namespace ChessChallenge.Chess
return loadedPositionInfo; return loadedPositionInfo;
} }
// Get the fen string of the current position /// <summary>
public static string CurrentFen(Board board) /// Get the fen string of the current position
/// When alwaysIncludeEPSquare is true the en passant square will be included
/// in the fen string even if no enemy pawn is in a position to capture it.
/// </summary>
public static string CurrentFen(Board board, bool alwaysIncludeEPSquare = true)
{ {
string fen = ""; string fen = "";
for (int rank = 7; rank >= 0; rank--) for (int rank = 7; rank >= 0; rank--)
@ -161,13 +165,15 @@ namespace ChessChallenge.Chess
int epFileIndex = board.currentGameState.enPassantFile - 1; int epFileIndex = board.currentGameState.enPassantFile - 1;
int epRankIndex = (board.IsWhiteToMove) ? 5 : 2; int epRankIndex = (board.IsWhiteToMove) ? 5 : 2;
if (epFileIndex == -1 || !EnPassantCanBeCaptured(epFileIndex, epRankIndex, board)) bool isEnPassant = epFileIndex != -1;
bool includeEP = alwaysIncludeEPSquare || EnPassantCanBeCaptured(epFileIndex, epRankIndex, board);
if (isEnPassant && includeEP)
{ {
fen += '-'; fen += BoardHelper.SquareNameFromCoordinate(epFileIndex, epRankIndex);
} }
else else
{ {
fen += BoardHelper.SquareNameFromCoordinate(epFileIndex, epRankIndex); fen += '-';
} }
// 50 move counter // 50 move counter