Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Prevent illegal moves when low in time #657

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/Lynx/Search/IDDFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ public SearchResult IDDFS(int? maxDepth, int? decisionTime)
int depth = 1;
bool isCancelled = false;
bool isMateDetected = false;
Move firstLegalMove = default;

try
{
_stopWatch.Start();

if (OnlyOneLegalMove(out var onlyOneLegalMoveSearchResult))
if (OnlyOneLegalMove(ref firstLegalMove, out var onlyOneLegalMoveSearchResult))
{
_engineWriter.TryWrite(InfoCommand.SearchResultInfo(onlyOneLegalMoveSearchResult));

Expand Down Expand Up @@ -166,7 +167,7 @@ public SearchResult IDDFS(int? maxDepth, int? decisionTime)
_stopWatch.Stop();
}

var finalSearchResult = GenerateFinalSearchResult(lastSearchResult, bestEvaluation, alpha, beta, depth, isCancelled);
var finalSearchResult = GenerateFinalSearchResult(lastSearchResult, bestEvaluation, alpha, beta, depth, firstLegalMove, isCancelled);

if (isMateDetected && finalSearchResult.Mate + Game.HalfMovesWithoutCaptureOrPawnMove < 96)
{
Expand Down Expand Up @@ -211,10 +212,9 @@ private bool StopSearchCondition(int depth, int? maxDepth, bool isMateDetected,
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool OnlyOneLegalMove([NotNullWhen(true)] out SearchResult? result)
private bool OnlyOneLegalMove(ref Move firstLegalMove, [NotNullWhen(true)] out SearchResult? result)
{
bool onlyOneLegalMove = false;
Move firstLegalMove = default;

Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
foreach (var move in MoveGenerator.GenerateAllMoves(Game.CurrentPosition, moves))
Expand Down Expand Up @@ -296,12 +296,13 @@ private bool OnlyOneLegalMove([NotNullWhen(true)] out SearchResult? result)

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private SearchResult GenerateFinalSearchResult(SearchResult? lastSearchResult,
int bestEvaluation, int alpha, int beta, int depth, bool isCancelled)
int bestEvaluation, int alpha, int beta, int depth, Move firstLegalMove, bool isCancelled)
{
SearchResult finalSearchResult;
if (lastSearchResult is null)
{
finalSearchResult = new(default, bestEvaluation, depth, [], alpha, beta);
_logger.Warn("Search cancelled at depth 1, choosing first found legal move as best one");
finalSearchResult = new(firstLegalMove, 0, 0, [firstLegalMove], alpha, beta);
}
else
{
Expand Down