Skip to content

Commit afe34c7

Browse files
committed
Standard hashsize reduced to 50 MB, size can now be set via UCI option. Changed the target Framework to .Net5. Bumped version to 0.5 for the next public release.
1 parent 4c5b8ea commit afe34c7

File tree

10 files changed

+51
-42
lines changed

10 files changed

+51
-42
lines changed

MinimalChess/MinimalChess.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
</PropertyGroup>
66

77
</Project>

MinimalChess/Transpositions.cs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,38 @@ public struct HashEntry
2525
}
2626

2727
public static short PERSISTENT = 9999;
28-
28+
public const int DEFAULT_SIZE_MB = 50;
2929
const int ENTRY_SIZE = 16; //BYTES
30-
const int HASH_MEMORY = 256; //Megabytes
31-
const int TT_SIZE = (HASH_MEMORY * 1024 * 1024) / ENTRY_SIZE;
30+
static HashEntry[] _table;
31+
32+
static int Index(in ulong hash) => (int)(hash % (ulong)_table.Length);
33+
34+
static Transpositions()
35+
{
36+
Resize(DEFAULT_SIZE_MB);
37+
}
3238

33-
static HashEntry[] _table = new HashEntry[TT_SIZE];
39+
public static void Resize(int hashSizeMBytes)
40+
{
41+
int length = (hashSizeMBytes * 1024 * 1024) / ENTRY_SIZE;
42+
_table = new HashEntry[length];
43+
}
44+
45+
public static void Clear()
46+
{
47+
Array.Clear(_table, 0, _table.Length);
48+
}
3449

3550
public static void Store(ulong zobristHash, int depth, SearchWindow window, int score, Move bestMove)
3651
{
37-
int slot = (int)(zobristHash % TT_SIZE);
38-
ref HashEntry entry = ref _table[slot];
52+
int index = Index(zobristHash);
53+
ref HashEntry entry = ref _table[index];
3954
if (entry.Depth == PERSISTENT)
4055
return;
4156

4257
//don't overwrite a bestmove unless it's a new position OR the new bestMove is explored to a greater depth
4358
if (entry.Hash != zobristHash || (depth >= entry.Depth && bestMove != default))
44-
_table[slot].BestMove = bestMove;
59+
_table[index].BestMove = bestMove;
4560

4661
entry.Hash = zobristHash;
4762
entry.Depth = (short)Math.Max(0, depth);
@@ -66,18 +81,18 @@ public static void Store(ulong zobristHash, int depth, SearchWindow window, int
6681
internal static Move GetBestMove(Board position)
6782
{
6883
ulong zobristHash = position.ZobristHash;
69-
int slot = (int)(zobristHash % TT_SIZE);
70-
if (_table[slot].Hash == zobristHash)
71-
return _table[slot].BestMove;
84+
int index = Index(zobristHash);
85+
if (_table[index].Hash == zobristHash)
86+
return _table[index].BestMove;
7287
else
7388
return default;
7489
}
7590

7691
public static bool GetScore(Board position, int depth, SearchWindow window, out int score)
7792
{
7893
ulong zobristHash = position.ZobristHash;
79-
int slot = (int)(zobristHash % TT_SIZE);
80-
ref HashEntry entry = ref _table[slot];
94+
int index = Index(zobristHash);
95+
ref HashEntry entry = ref _table[index];
8196

8297
score = entry.Score;
8398
if (entry.Hash != zobristHash || entry.Depth < depth)
@@ -95,10 +110,5 @@ public static bool GetScore(Board position, int depth, SearchWindow window, out
95110

96111
return false;
97112
}
98-
99-
public static void Clear()
100-
{
101-
Array.Clear(_table, 0, TT_SIZE);
102-
}
103113
}
104114
}

MinimalChessBoard/MinimalChessBoard.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net5.0</TargetFramework>
66
</PropertyGroup>
77

88
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
9-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
1010
</PropertyGroup>
1111

1212
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

MinimalChessBoard/Program.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ static void Main()
1414
{
1515
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
1616

17-
unsafe{
18-
Console.WriteLine($"Move: {sizeof(Move)} bytes");
19-
Console.WriteLine($"Transpositions.HashEntry: {sizeof(Transpositions.HashEntry)} bytes");
20-
}
21-
2217
bool running = true;
2318
Board board = new Board(Board.STARTING_POS_FEN);
2419
Move move = default;
@@ -46,7 +41,7 @@ static void Main()
4641
string command = tokens[0];
4742

4843
long t0 = Stopwatch.GetTimestamp();
49-
//try
44+
try
5045
{
5146
if (command == "reset")
5247
{
@@ -94,7 +89,7 @@ static void Main()
9489
int depth = tokens.Length > 1 ? int.Parse(tokens[1]) : 0;
9590
ListMoves(board, depth);
9691
}
97-
else if(command == "m")
92+
else if (command == "m")
9893
{
9994
PrintMobility(board);
10095
}
@@ -108,10 +103,10 @@ static void Main()
108103
if (dt > 0.01)
109104
Console.WriteLine($" Operation took {dt:0.####}s");
110105
}
111-
//catch (Exception error)
112-
//{
113-
// Console.WriteLine("ERROR: " + error.Message);
114-
//}
106+
catch (Exception error)
107+
{
108+
Console.WriteLine("ERROR: " + error.Message);
109+
}
115110
}
116111
}
117112

MinimalChessEngine/MinimalChessEngine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<TargetFramework>net5.0</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

MinimalChessEngine/Program.cs

Lines changed: 4 additions & 2 deletions
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.4.9";
10+
const string NAME_VERSION = "MinimalChess 0.5";
1111

1212
static Engine _engine = new Engine();
1313
static async Task Main()
@@ -36,6 +36,7 @@ private static void ParseUciCommand(string input)
3636
case "uci":
3737
Console.WriteLine($"id name {NAME_VERSION}");
3838
Console.WriteLine($"id author Thomas Jahn");
39+
Console.WriteLine($"option name Hash type spin default {Transpositions.DEFAULT_SIZE_MB} min 1 max 2047");//consider gcAllowVeryLargeObjects if larger TT is needed
3940
Console.WriteLine("uciok");
4041
break;
4142
case "isready":
@@ -67,7 +68,8 @@ private static void ParseUciCommand(string input)
6768

6869
private static void UciSetOption(string[] tokens)
6970
{
70-
//No options currently supported
71+
if (tokens[1] == "name" && tokens[2] == "Hash" && tokens[3] == "value" && int.TryParse(tokens[4], out int hashSizeMBytes))
72+
Transpositions.Resize(hashSizeMBytes);
7173
}
7274

7375
private static void UciPosition(string[] tokens)

MinimalChessEngine/Properties/PublishProfiles/Linux ARM.pubxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
77
<PublishProtocol>FileSystem</PublishProtocol>
88
<Configuration>Release</Configuration>
99
<Platform>Any CPU</Platform>
10-
<TargetFramework>netcoreapp3.1</TargetFramework>
11-
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.4.1 Linux ARM</PublishDir>
10+
<TargetFramework>net5.0</TargetFramework>
11+
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.5 Linux ARM</PublishDir>
1212
<RuntimeIdentifier>linux-arm</RuntimeIdentifier>
1313
<SelfContained>true</SelfContained>
1414
<PublishSingleFile>False</PublishSingleFile>

MinimalChessEngine/Properties/PublishProfiles/Linux x64.pubxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
77
<PublishProtocol>FileSystem</PublishProtocol>
88
<Configuration>Release</Configuration>
99
<Platform>Any CPU</Platform>
10-
<TargetFramework>netcoreapp3.1</TargetFramework>
11-
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.4.9 Linux</PublishDir>
10+
<TargetFramework>net5.0</TargetFramework>
11+
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.5 Linux</PublishDir>
1212
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
1313
<SelfContained>true</SelfContained>
1414
<PublishSingleFile>True</PublishSingleFile>

MinimalChessEngine/Properties/PublishProfiles/Mac x64.pubxml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
77
<PublishProtocol>FileSystem</PublishProtocol>
88
<Configuration>Release</Configuration>
99
<Platform>Any CPU</Platform>
10-
<TargetFramework>netcoreapp3.1</TargetFramework>
11-
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.4.1 Mac</PublishDir>
10+
<TargetFramework>net5.0</TargetFramework>
11+
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.5 Mac</PublishDir>
1212
<RuntimeIdentifier>osx-x64</RuntimeIdentifier>
1313
<SelfContained>true</SelfContained>
1414
<PublishSingleFile>True</PublishSingleFile>
1515
<PublishReadyToRun>False</PublishReadyToRun>
1616
<PublishTrimmed>True</PublishTrimmed>
17+
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
1718
</PropertyGroup>
1819
</Project>

MinimalChessEngine/Properties/PublishProfiles/Windows x64.pubxml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
77
<PublishProtocol>FileSystem</PublishProtocol>
88
<Configuration>Release</Configuration>
99
<Platform>Any CPU</Platform>
10-
<TargetFramework>netcoreapp3.1</TargetFramework>
11-
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.4.9 Windows</PublishDir>
10+
<TargetFramework>net5.0</TargetFramework>
11+
<PublishDir>D:\Projekte\Chess\Builds\MinimalChess 0.5 Windows</PublishDir>
1212
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
1313
<SelfContained>true</SelfContained>
1414
<PublishSingleFile>True</PublishSingleFile>
1515
<PublishReadyToRun>False</PublishReadyToRun>
1616
<PublishTrimmed>True</PublishTrimmed>
17+
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
1718
</PropertyGroup>
1819
</Project>

0 commit comments

Comments
 (0)