Skip to content

Commit

Permalink
Added threefold repetition detection, released v1.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Timmoth committed Oct 6, 2024
1 parent 0174df7 commit 736ae2e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
working-directory: Sapling
id: get_version
run: |
VERSION=1.1.6
VERSION=1.1.7
echo "Application version: $VERSION"
echo "::set-output name=version::$VERSION"
Expand Down
28 changes: 28 additions & 0 deletions Sapling.Engine/RepetitionDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ ulong AttackMask(int idx, int piece)
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsThreefoldRepetition(ushort turnCount, ushort halfMoveClock, ulong* hashHistory)
{
if (halfMoveClock < 3)
return false;

var currHash = hashHistory + turnCount - 1;

var initialHash = *(currHash);

var count = 1;
for (var i = 2; i < halfMoveClock; i+=2)
{
currHash -= 2;
if (*(currHash) != initialHash)
{
continue;
}

if (++count >= 3)
{
return true;
}
}

return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasRepetition(this ref BoardStateData pos, ulong* hashHistory, int depthFromRoot)
{
Expand Down
7 changes: 5 additions & 2 deletions Sapling.Engine/Search/NegaMaxSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public unsafe int
return alpha;
}

if (currentBoardState->HalfMoveClock >= 100 || currentBoardState->InsufficientMatingMaterial())
if (currentBoardState->HalfMoveClock >= 100 ||
currentBoardState->InsufficientMatingMaterial() ||
RepetitionDetector.IsThreefoldRepetition(currentBoardState->TurnCount, currentBoardState->HalfMoveClock, HashHistory))
{
// Detect draw by Fifty move counter or repetition
return 0;
Expand Down Expand Up @@ -248,6 +250,7 @@ public unsafe int
counterMove);
}

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

// Probcut
Expand Down Expand Up @@ -316,6 +319,7 @@ public unsafe int
{
newBoardState->FinishApplyBlack(ref *newAccumulatorState, m, currentBoardState->EnPassantFile, currentBoardState->CastleRights);
}
*nextHashHistoryEntry = newBoardState->Hash;

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

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

var bestScore = int.MinValue;
// Evaluate each move
Expand Down
7 changes: 4 additions & 3 deletions Sapling.Engine/Search/QuiescenceSearch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics.X86;
using Sapling.Engine.MoveGen;
Expand Down Expand Up @@ -27,7 +26,9 @@ public unsafe int QuiescenceSearch(BoardStateData* boardState, AccumulatorState*
return Evaluate(boardState, accumulatorState, depthFromRoot);
}

if (boardState->InsufficientMatingMaterial())
if (boardState->HalfMoveClock >= 100 ||
boardState->InsufficientMatingMaterial() ||
RepetitionDetector.IsThreefoldRepetition(boardState->TurnCount, boardState->HalfMoveClock, HashHistory))
{
// Detect draw by Fifty move counter or repetition
return 0;
Expand Down
6 changes: 3 additions & 3 deletions Sapling/Sapling.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<Nullable>enable</Nullable>
<ApplicationIcon>logo.ico</ApplicationIcon>
<Title>Sapling</Title>
<AssemblyVersion>1.1.6.0</AssemblyVersion>
<FileVersion>1.1.6.0</FileVersion>
<Version>1.1.6.0</Version>
<AssemblyVersion>1.1.7.0</AssemblyVersion>
<FileVersion>1.1.7.0</FileVersion>
<Version>1.1.7.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down

0 comments on commit 736ae2e

Please sign in to comment.