Fix missing capture type for en-passant

This commit is contained in:
Sebastian Lague 2023-07-25 17:52:54 +02:00
parent c5f13b9001
commit 29caae5b56
4 changed files with 15 additions and 6 deletions

View file

@ -36,7 +36,7 @@ namespace ChessChallenge.API
{ {
Chess.Move move = boardSource.AllGameMoves[i]; Chess.Move move = boardSource.AllGameMoves[i];
int movePieceType = PieceHelper.PieceType(board.Square[move.StartSquareIndex]); int movePieceType = PieceHelper.PieceType(board.Square[move.StartSquareIndex]);
int capturePieceType = PieceHelper.PieceType(board.Square[move.TargetSquareIndex]); int capturePieceType = move.IsEnPassant ? PieceHelper.Pawn : PieceHelper.PieceType(board.Square[move.TargetSquareIndex]);
GameMoveHistory[i] = new Move(move, movePieceType, capturePieceType); GameMoveHistory[i] = new Move(move, movePieceType, capturePieceType);
board.MakeMove(move, false); board.MakeMove(move, false);
} }

View file

@ -109,14 +109,16 @@ namespace ChessChallenge.Application.APIHelpers
API.Move CreateAPIMove(int startSquare, int targetSquare, int flag) API.Move CreateAPIMove(int startSquare, int targetSquare, int flag)
{ {
int movePieceType = PieceHelper.PieceType(board.Square[startSquare]); int movePieceType = PieceHelper.PieceType(board.Square[startSquare]);
int capturePieceType = PieceHelper.PieceType(board.Square[targetSquare]); return CreateAPIMove(startSquare, targetSquare, flag, movePieceType);
API.Move apiMove = new(new Move(startSquare, targetSquare, flag), movePieceType, capturePieceType);
return apiMove;
} }
API.Move CreateAPIMove(int startSquare, int targetSquare, int flag, int movePieceType) API.Move CreateAPIMove(int startSquare, int targetSquare, int flag, int movePieceType)
{ {
int capturePieceType = PieceHelper.PieceType(board.Square[targetSquare]); int capturePieceType = PieceHelper.PieceType(board.Square[targetSquare]);
if (flag == Move.EnPassantCaptureFlag)
{
capturePieceType = PieceHelper.Pawn;
}
API.Move apiMove = new(new Move(startSquare, targetSquare, flag), movePieceType, capturePieceType); API.Move apiMove = new(new Move(startSquare, targetSquare, flag), movePieceType, capturePieceType);
return apiMove; return apiMove;
} }

View file

@ -324,7 +324,7 @@ namespace ChessChallenge.Application
Assert(boardAPI.GameStartFenString == startPos, "Wrong game start fen"); Assert(boardAPI.GameStartFenString == startPos, "Wrong game start fen");
Assert(boardAPI.GetFenString() == "r1bqkbnr/pppppppp/8/4n3/8/8/PPPP1PPP/RNBQKBNR w KQkq - 0 3", "Wrong game fen"); Assert(boardAPI.GetFenString() == "r1bqkbnr/pppppppp/8/4n3/8/8/PPPP1PPP/RNBQKBNR w KQkq - 0 3", "Wrong game fen");
// Invalid target piece in capture when en passant is available // Test for bug: Invalid target piece in capture when en passant is available
string[] invalidCaptureFens = string[] invalidCaptureFens =
{ {
"r1b1r1k1/ppp2pp1/7p/2Pp4/4q3/PQ6/4PPPP/3RKB1R w K d6 0 16", // c5d6 "r1b1r1k1/ppp2pp1/7p/2Pp4/4q3/PQ6/4PPPP/3RKB1R w K d6 0 16", // c5d6
@ -332,7 +332,6 @@ namespace ChessChallenge.Application
}; };
foreach (var fen in invalidCaptureFens) foreach (var fen in invalidCaptureFens)
{ {
Console.WriteLine($"Checking captures for FEN: {fen}");
board.LoadPosition(fen); board.LoadPosition(fen);
boardAPI = new(board); boardAPI = new(board);
captures = boardAPI.GetLegalMoves(true); captures = boardAPI.GetLegalMoves(true);
@ -342,6 +341,13 @@ namespace ChessChallenge.Application
Assert(c.CapturePieceType != PieceType.None, $"Capture piece type wrong for move {c}"); Assert(c.CapturePieceType != PieceType.None, $"Capture piece type wrong for move {c}");
} }
} }
board.LoadPosition(invalidCaptureFens[0]);
board.MakeMove(MoveUtility.GetMoveFromUCIName("c5d6", board), false);
boardAPI = new(board);
Assert(boardAPI.GameMoveHistory[0].CapturePieceType == PieceType.Pawn, "Wrong capture type: game history");
Assert(boardAPI.GameMoveHistory[0].IsEnPassant, "Wrong move flag: move history");
} }
static void MoveGenTest() static void MoveGenTest()

View file

@ -50,6 +50,7 @@ namespace ChessChallenge.Chess
public int StartSquareIndex => moveValue & startSquareMask; public int StartSquareIndex => moveValue & startSquareMask;
public int TargetSquareIndex => (moveValue & targetSquareMask) >> 6; public int TargetSquareIndex => (moveValue & targetSquareMask) >> 6;
public bool IsPromotion => MoveFlag >= PromoteToQueenFlag; public bool IsPromotion => MoveFlag >= PromoteToQueenFlag;
public bool IsEnPassant => MoveFlag == EnPassantCaptureFlag;
public int MoveFlag => moveValue >> 12; public int MoveFlag => moveValue >> 12;
public int PromotionPieceType public int PromotionPieceType