Skip to content

Commit a24585b

Browse files
committed
Removed playing moves from the PV directly. Instead the PV of the last iteration is now stored again in the hash before going into SearchDeeper to make sure it's available from the TT. Less code and a bit faster, too.
1 parent 4499ec6 commit a24585b

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

MinimalChess/IterativeSearch.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,22 @@ public void SearchDeeper(Func<bool> killSwitch = null)
4545
_pv.Grow(Depth);
4646
_killers.Grow(Depth);
4747
_killSwitch = new KillSwitch(killSwitch);
48+
PrepareTranspositions(Depth);
4849
var window = SearchWindow.Infinite;
4950
Score = EvalPosition(_root, Depth, window);
5051
}
5152

53+
private void PrepareTranspositions(int depth)
54+
{
55+
//make sure the PV is in the TT
56+
Board position = new Board(_root);
57+
foreach (Move move in _pv.GetLine(depth))
58+
{
59+
Transpositions.Store(position.ZobristHash, --depth, SearchWindow.Infinite, Score, move);
60+
position.Play(move);
61+
}
62+
}
63+
5264
private int EvalPositionTT(Board position, int depth, SearchWindow window, bool isNullMove = false)
5365
{
5466
if (Transpositions.GetScore(position, depth, window, out int score))
@@ -91,7 +103,7 @@ private int EvalPosition(Board position, int depth, SearchWindow window, bool is
91103

92104
//do a regular expansion...
93105
int expandedNodes = 0;
94-
foreach ((Move move, Board child) in Playmaker.Play(position, depth, _pv, _killers))
106+
foreach ((Move move, Board child) in Playmaker.Play(position, depth, _killers))
95107
{
96108
expandedNodes++;
97109

MinimalChess/Playmaker.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,18 @@ namespace MinimalChess
55
{
66
public static class Playmaker
77
{
8-
internal static IEnumerable<(Move Move, Board Board)> Play(Board position, int depth, PrincipalVariation pv, KillerMoves killers)
8+
internal static IEnumerable<(Move Move, Board Board)> Play(Board position, int depth, KillerMoves killers)
99
{
10+
//1. Captures Mvv-Lva, PV excluded
1011
Move bestMove = Transpositions.GetBestMove(position);
1112
if (bestMove != default)
1213
{
1314
var nextPosition = new Board(position, bestMove);
1415
yield return (bestMove, nextPosition);
1516
}
1617

17-
//1. PV if available
18-
Move pvMove = pv[depth];
19-
if (pvMove != bestMove && position.IsPlayable(pvMove))
20-
{
21-
var nextPosition = new Board(position, pvMove);
22-
if (!nextPosition.IsChecked(position.SideToMove))
23-
yield return (pvMove, nextPosition);
24-
}
25-
2618
//2. Captures Mvv-Lva, PV excluded
27-
MoveList captures = MoveList.Captures(position);
28-
captures.Remove(pvMove);
29-
captures.Remove(bestMove);
30-
captures.SortMvvLva(position);
31-
foreach (var capture in captures)
19+
foreach (var capture in MoveList.SortedCaptures(position))
3220
{
3321
var nextPosition = new Board(position, capture);
3422
if (!nextPosition.IsChecked(position.SideToMove))
@@ -37,7 +25,7 @@ public static class Playmaker
3725

3826
//3. Killers if available
3927
foreach (Move killer in killers.Get(depth))
40-
if (killer != pvMove && killer != bestMove && position[killer.ToSquare] == Piece.None && position.IsPlayable(killer))
28+
if (position[killer.ToSquare] == Piece.None && position.IsPlayable(killer))
4129
{
4230
var nextPosition = new Board(position, killer);
4331
if (!nextPosition.IsChecked(position.SideToMove))
@@ -46,7 +34,7 @@ public static class Playmaker
4634

4735
//4. Play quiet moves that aren't known killers
4836
foreach (var move in MoveList.Quiets(position))
49-
if (move != pvMove && move != bestMove && !killers.Contains(depth, move))
37+
if (!killers.Contains(depth, move))
5038
{
5139
var nextPosition = new Board(position, move);
5240
if (!nextPosition.IsChecked(position.SideToMove))

MinimalChess/Transpositions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ namespace MinimalChess
66
{
77
public static class Transpositions
88
{
9+
10+
public static long HashOverwrites = 0;
11+
public static long HashWrites = 0;
12+
913
public enum ScoreType : byte
1014
{
1115
GreaterOrEqual,
@@ -53,7 +57,7 @@ public static void Store(ulong zobristHash, int depth, SearchWindow window, int
5357
ref HashEntry entry = ref _table[index];
5458
if (entry.Depth == PERSISTENT)
5559
return;
56-
60+
5761
//don't overwrite a bestmove unless it's a new position OR the new bestMove is explored to a greater depth
5862
if (entry.Hash != zobristHash || (depth >= entry.Depth && bestMove != default))
5963
_table[index].BestMove = bestMove;

MinimalChessEngine/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace MinimalChessEngine
77
{
88
public static class Program
99
{
10-
const string NAME_VERSION = "MinimalChess 0.5";
10+
const string NAME_VERSION = "MinimalChess 0.5.1";
1111

1212
static Engine _engine = new Engine();
1313
static async Task Main()

MinimalChessEngine/Properties/PublishProfiles/Windows x64.pubxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
88
<Configuration>Release</Configuration>
99
<Platform>Any CPU</Platform>
1010
<TargetFramework>net5.0</TargetFramework>
11-
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.5 Windows</PublishDir>
11+
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.5.1 Windows</PublishDir>
1212
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
1313
<SelfContained>true</SelfContained>
1414
<PublishSingleFile>True</PublishSingleFile>

0 commit comments

Comments
 (0)