Skip to content

Commit 736ae2e

Browse files
committed
Added threefold repetition detection, released v1.1.7
1 parent 0174df7 commit 736ae2e

5 files changed

Lines changed: 41 additions & 9 deletions

File tree

.github/workflows/release-pipeline.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
working-directory: Sapling
2626
id: get_version
2727
run: |
28-
VERSION=1.1.6
28+
VERSION=1.1.7
2929
echo "Application version: $VERSION"
3030
echo "::set-output name=version::$VERSION"
3131

Sapling.Engine/RepetitionDetector.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ ulong AttackMask(int idx, int piece)
7676
}
7777
}
7878

79+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
80+
public static bool IsThreefoldRepetition(ushort turnCount, ushort halfMoveClock, ulong* hashHistory)
81+
{
82+
if (halfMoveClock < 3)
83+
return false;
84+
85+
var currHash = hashHistory + turnCount - 1;
86+
87+
var initialHash = *(currHash);
88+
89+
var count = 1;
90+
for (var i = 2; i < halfMoveClock; i+=2)
91+
{
92+
currHash -= 2;
93+
if (*(currHash) != initialHash)
94+
{
95+
continue;
96+
}
97+
98+
if (++count >= 3)
99+
{
100+
return true;
101+
}
102+
}
103+
104+
return false;
105+
}
106+
79107
[MethodImpl(MethodImplOptions.AggressiveInlining)]
80108
public static bool HasRepetition(this ref BoardStateData pos, ulong* hashHistory, int depthFromRoot)
81109
{

Sapling.Engine/Search/NegaMaxSearch.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public unsafe int
5454
return alpha;
5555
}
5656

57-
if (currentBoardState->HalfMoveClock >= 100 || currentBoardState->InsufficientMatingMaterial())
57+
if (currentBoardState->HalfMoveClock >= 100 ||
58+
currentBoardState->InsufficientMatingMaterial() ||
59+
RepetitionDetector.IsThreefoldRepetition(currentBoardState->TurnCount, currentBoardState->HalfMoveClock, HashHistory))
5860
{
5961
// Detect draw by Fifty move counter or repetition
6062
return 0;
@@ -248,6 +250,7 @@ public unsafe int
248250
counterMove);
249251
}
250252

253+
var nextHashHistoryEntry = HashHistory + currentBoardState->TurnCount;
251254
var probCutSortedUpTo = 0;
252255

253256
// Probcut
@@ -316,6 +319,7 @@ public unsafe int
316319
{
317320
newBoardState->FinishApplyBlack(ref *newAccumulatorState, m, currentBoardState->EnPassantFile, currentBoardState->CastleRights);
318321
}
322+
*nextHashHistoryEntry = newBoardState->Hash;
319323

320324
NodesVisited--;
321325
var score = -QuiescenceSearch(newBoardState, newAccumulatorState, depthFromRoot + 1, -probBeta, -probBeta + 1);
@@ -344,7 +348,6 @@ public unsafe int
344348

345349
uint bestMove = default;
346350
var evaluationBound = TranspositionTableFlag.Alpha;
347-
var nextHashHistoryEntry = HashHistory + currentBoardState->TurnCount;
348351

349352
var bestScore = int.MinValue;
350353
// Evaluate each move

Sapling.Engine/Search/QuiescenceSearch.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.IO;
2-
using System.Runtime.CompilerServices;
1+
using System.Runtime.CompilerServices;
32
using System.Runtime.InteropServices;
43
using System.Runtime.Intrinsics.X86;
54
using Sapling.Engine.MoveGen;
@@ -27,7 +26,9 @@ public unsafe int QuiescenceSearch(BoardStateData* boardState, AccumulatorState*
2726
return Evaluate(boardState, accumulatorState, depthFromRoot);
2827
}
2928

30-
if (boardState->InsufficientMatingMaterial())
29+
if (boardState->HalfMoveClock >= 100 ||
30+
boardState->InsufficientMatingMaterial() ||
31+
RepetitionDetector.IsThreefoldRepetition(boardState->TurnCount, boardState->HalfMoveClock, HashHistory))
3132
{
3233
// Detect draw by Fifty move counter or repetition
3334
return 0;

Sapling/Sapling.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<Nullable>enable</Nullable>
88
<ApplicationIcon>logo.ico</ApplicationIcon>
99
<Title>Sapling</Title>
10-
<AssemblyVersion>1.1.6.0</AssemblyVersion>
11-
<FileVersion>1.1.6.0</FileVersion>
12-
<Version>1.1.6.0</Version>
10+
<AssemblyVersion>1.1.7.0</AssemblyVersion>
11+
<FileVersion>1.1.7.0</FileVersion>
12+
<Version>1.1.7.0</Version>
1313
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1414
</PropertyGroup>
1515

0 commit comments

Comments
 (0)