From 1315551493fca3f3fcfc137e4ee1124e16e46f47 Mon Sep 17 00:00:00 2001 From: Sebastian Lague Date: Tue, 1 Aug 2023 17:49:40 +0200 Subject: [PATCH] Fix stalemate detection after generating capture moves in a position with no captures available #418 --- Chess-Challenge/src/API/Board.cs | 8 ++++++-- .../src/Framework/Application/Helpers/Tester.cs | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Chess-Challenge/src/API/Board.cs b/Chess-Challenge/src/API/Board.cs index c5418f8..aac1899 100644 --- a/Chess-Challenge/src/API/Board.cs +++ b/Chess-Challenge/src/API/Board.cs @@ -174,8 +174,12 @@ namespace ChessChallenge.API { bool includeQuietMoves = !capturesOnly; moveGen.GenerateMoves(ref moveList, board, includeQuietMoves); - hasCachedMoveCount = true; - cachedMoveCount = moveList.Length; + + if (!capturesOnly) + { + hasCachedMoveCount = true; + cachedMoveCount = moveList.Length; + } } diff --git a/Chess-Challenge/src/Framework/Application/Helpers/Tester.cs b/Chess-Challenge/src/Framework/Application/Helpers/Tester.cs index e732b86..fae228f 100644 --- a/Chess-Challenge/src/Framework/Application/Helpers/Tester.cs +++ b/Chess-Challenge/src/Framework/Application/Helpers/Tester.cs @@ -17,12 +17,12 @@ namespace ChessChallenge.Application public static void Run(bool runPerft) { anyFailed = false; - + new SearchTest().Run(false); new SearchTest().Run(true); new SearchTest2().Run(); new SearchTest3().Run(); - + RepetitionTest(); DrawTest(); MoveGenTest(); @@ -368,6 +368,19 @@ namespace ChessChallenge.Application Assert(!boardAPI.IsDraw(), "Draw wrong"); boardAPI.MakeMove(new API.Move("f5f7", boardAPI)); Assert(boardAPI.IsDraw(), "Draw wrong"); + // Test stalemate bug when generating captures + board.LoadPosition("8/8/3k4/8/8/3K4/4Q3/8 w - - 0 1"); + boardAPI = new API.Board(board); + var captures = boardAPI.GetLegalMoves(capturesOnly: true); + Assert(captures.Length == 0, "Wrong capture count"); + Assert(!boardAPI.IsInStalemate(), "Stalemate wrong"); + // Test stalemate bug when generating captures (non alloc) + board.LoadPosition("8/8/3k4/8/8/3K4/4Q3/8 w - - 0 1"); + boardAPI = new API.Board(board); + Span captureSpan = stackalloc API.Move[64]; + boardAPI.GetLegalMovesNonAlloc(ref captureSpan, capturesOnly: true); + Assert(captureSpan.Length == 0, "Wrong capture count"); + Assert(!boardAPI.IsInStalemate(), "Stalemate wrong"); // Insufficient material board = new Chess.Board();