Skip to content

Commit 2f3713c

Browse files
committed
when reporting the 'info' to the GUI and the PV contains less then 'depth' moves due to a TT hit the engine now tries to restore the rest of the PV from the TT.
1 parent 55a907c commit 2f3713c

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

MinimalChess/Playmaker.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ public static class Playmaker
77
internal static IEnumerable<(Move Move, Board Board)> Play(Board position, int depth, KillerMoves killers, History history)
88
{
99
//1. Captures Mvv-Lva, PV excluded
10-
Move bestMove = Transpositions.GetBestMove(position);
11-
if (bestMove != default)
10+
if (Transpositions.GetBestMove(position, out Move bestMove))
1211
{
1312
var nextPosition = new Board(position, bestMove);
1413
yield return (bestMove, nextPosition);

MinimalChess/Transpositions.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,10 @@ public static void Store(ulong zobristHash, int depth, SearchWindow window, int
103103
}
104104
}
105105

106-
internal static Move GetBestMove(Board position)
106+
public static bool GetBestMove(Board position, out Move bestMove)
107107
{
108-
if (Index(position.ZobristHash, out int index))
109-
return _table[index].BestMove;
110-
111-
return default;
108+
bestMove = Index(position.ZobristHash, out int index) ? _table[index].BestMove : default;
109+
return bestMove != default;
112110
}
113111

114112
public static bool GetScore(ulong zobristHash, int depth, SearchWindow window, out int score)

MinimalChessEngine/Engine.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MinimalChess;
1+
using System;
2+
using MinimalChess;
23
using System.Collections.Generic;
34
using System.Threading;
45

@@ -133,8 +134,34 @@ private bool CanSearchDeeper()
133134

134135
private void Collect()
135136
{
136-
Uci.Info(_search.Depth, (int)SideToMove * _search.Score, _search.NodesVisited, _time.Elapsed, _search.PrincipalVariation);
137137
_best = _search.PrincipalVariation[0];
138+
139+
Uci.Info(
140+
depth: _search.Depth,
141+
score: (int)SideToMove * _search.Score,
142+
nodes: _search.NodesVisited,
143+
timeMs: _time.Elapsed,
144+
pv: GetPrintablePV(_search.PrincipalVariation, _search.Depth)
145+
);
146+
}
147+
148+
private Move[] GetPrintablePV(Move[] pv, int depth)
149+
{
150+
List<Move> result = new(pv);
151+
//Try to extend from TT to reach the desired depth?
152+
if (result.Count < depth)
153+
{
154+
Board position = new Board(_board);
155+
foreach (Move move in pv)
156+
position.Play(move);
157+
158+
while (result.Count < depth && Transpositions.GetBestMove(position, out Move move))
159+
{
160+
position.Play(move);
161+
result.Add(move);
162+
}
163+
}
164+
return result.ToArray();
138165
}
139166
}
140167
}

0 commit comments

Comments
 (0)