diff --git a/Chess-Challenge/src/API/Board.cs b/Chess-Challenge/src/API/Board.cs index 9a8f585..9d09624 100644 --- a/Chess-Challenge/src/API/Board.cs +++ b/Chess-Challenge/src/API/Board.cs @@ -36,7 +36,7 @@ namespace ChessChallenge.API { Chess.Move move = boardSource.AllGameMoves[i]; 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); board.MakeMove(move, false); } diff --git a/Chess-Challenge/src/Framework/Application/Helpers/API Helpers/APIMoveGen.cs b/Chess-Challenge/src/Framework/Application/Helpers/API Helpers/APIMoveGen.cs index 2bb5240..5ad1639 100644 --- a/Chess-Challenge/src/Framework/Application/Helpers/API Helpers/APIMoveGen.cs +++ b/Chess-Challenge/src/Framework/Application/Helpers/API Helpers/APIMoveGen.cs @@ -109,14 +109,16 @@ namespace ChessChallenge.Application.APIHelpers API.Move CreateAPIMove(int startSquare, int targetSquare, int flag) { int movePieceType = PieceHelper.PieceType(board.Square[startSquare]); - int capturePieceType = PieceHelper.PieceType(board.Square[targetSquare]); - API.Move apiMove = new(new Move(startSquare, targetSquare, flag), movePieceType, capturePieceType); - return apiMove; + return CreateAPIMove(startSquare, targetSquare, flag, movePieceType); } API.Move CreateAPIMove(int startSquare, int targetSquare, int flag, int movePieceType) { 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); return apiMove; } diff --git a/Chess-Challenge/src/Framework/Application/Helpers/Tester.cs b/Chess-Challenge/src/Framework/Application/Helpers/Tester.cs index 4317160..76425e1 100644 --- a/Chess-Challenge/src/Framework/Application/Helpers/Tester.cs +++ b/Chess-Challenge/src/Framework/Application/Helpers/Tester.cs @@ -324,7 +324,7 @@ namespace ChessChallenge.Application 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"); - // 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 = { "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) { - Console.WriteLine($"Checking captures for FEN: {fen}"); board.LoadPosition(fen); boardAPI = new(board); captures = boardAPI.GetLegalMoves(true); @@ -342,6 +341,13 @@ namespace ChessChallenge.Application 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() diff --git a/Chess-Challenge/src/Framework/Chess/Board/Move.cs b/Chess-Challenge/src/Framework/Chess/Board/Move.cs index 43d7945..1be192b 100644 --- a/Chess-Challenge/src/Framework/Chess/Board/Move.cs +++ b/Chess-Challenge/src/Framework/Chess/Board/Move.cs @@ -50,6 +50,7 @@ namespace ChessChallenge.Chess public int StartSquareIndex => moveValue & startSquareMask; public int TargetSquareIndex => (moveValue & targetSquareMask) >> 6; public bool IsPromotion => MoveFlag >= PromoteToQueenFlag; + public bool IsEnPassant => MoveFlag == EnPassantCaptureFlag; public int MoveFlag => moveValue >> 12; public int PromotionPieceType