From d2b973b869ac289961ba87f3983e3401d0d10df1 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 18 Mar 2025 11:30:12 +0100 Subject: [PATCH 1/5] threefold detection | bench 1143648 --- source/board.cpp | 3 +++ source/board.h | 3 +++ source/search.cpp | 17 +++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/source/board.cpp b/source/board.cpp index 95c9933..0e86b65 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -343,6 +343,9 @@ void Board::MakeMove(Move move) { enPassantTarget = newEpTarget; GenAttackMaps(*this); + + positionHistory[positionIndex] = hashKey; + positionIndex++; } bool Board::InPossibleZug(bool side) { diff --git a/source/board.h b/source/board.h index bb7ffdf..2cf90fd 100644 --- a/source/board.h +++ b/source/board.h @@ -7,6 +7,9 @@ constexpr int nullPieceType = 100; constexpr int noEPTarget = -1; +inline int positionIndex = 0; +inline U64 positionHistory[1000]; + class Board { public: Board() { diff --git a/source/search.cpp b/source/search.cpp index 4edf1f5..4b5f3d7 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -83,6 +83,18 @@ void ListScores(Board &board, int ply) { } } +static bool IsThreefold(Board &board) { + int count = 0; + + for (int i = positionIndex - 1; i >= 0; i--) { + if (positionHistory[i] == board.hashKey) count++; + + if (count >= 3) return true; + } + + return false; +} + static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { if (!benchStarted) { if (sw.GetElapsedMS() >= timeToSearch) { @@ -112,6 +124,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) { Board copy = board; copy.MakeMove(board.moveList[i]); int score = -Quiescence(copy, -beta, -alpha, ply + 1).score; + positionIndex--; if (score >= beta) { return score; @@ -139,6 +152,8 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } + if (!ply && IsThreefold(board)) return 0; + pvLine.SetLength(ply); // if NOT PV node then we try to hit the TTable @@ -217,6 +232,8 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } + positionIndex--; + // Fail high (beta cutoff) if (score >= beta) { if (!currMove.IsCapture()) { -- 2.47.3 From 4a8f8e1ee7e2f6089130d374687bf25eb091c044 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 18 Mar 2025 11:36:15 +0100 Subject: [PATCH 2/5] resetting positionIndex | bench 1143648 --- source/uci.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/uci.cpp b/source/uci.cpp index da89342..7c59ae8 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -107,6 +107,8 @@ void UCILoop(Board &board) { // parse UCI "position" command if (input.find("position") != std::string::npos) { ParsePosition(board, input); + positionIndex = 0; + continue; } @@ -118,6 +120,8 @@ void UCILoop(Board &board) { TT.Clear(); std::memset(killerMoves, 0, sizeof(killerMoves)); std::memset(historyMoves, 0, sizeof(historyMoves)); + positionIndex = 0; + continue; } -- 2.47.3 From 6453ffa5bd4e4593ebf9dd21fcecee91d27c1d33 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 18 Mar 2025 11:45:57 +0100 Subject: [PATCH 3/5] bugfix + resetting positionIndex in benchmark | bench 1143612 --- source/benchmark.cpp | 2 ++ source/search.cpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index c869d35..517859d 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -64,6 +64,7 @@ void RunBenchmark() { int ply = 0; benchStarted = true; searchStopped = false; + positionIndex = 0; // Clearing TT.Clear(); @@ -82,4 +83,5 @@ void RunBenchmark() { nodes = 0; TT.Clear(); benchStarted = false; + positionIndex = 0; } diff --git a/source/search.cpp b/source/search.cpp index 4b5f3d7..a7dbaad 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -152,7 +152,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } - if (!ply && IsThreefold(board)) return 0; + if (ply && IsThreefold(board)) return 0; pvLine.SetLength(ply); -- 2.47.3 From 09ab31974ba111a408b8488e3e83ba362d206ec0 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 18 Mar 2025 12:34:21 +0100 Subject: [PATCH 4/5] rewrote IsThreefold | bench 1154407 --- source/board.cpp | 2 +- source/search.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 0e86b65..be515c8 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -344,8 +344,8 @@ void Board::MakeMove(Move move) { GenAttackMaps(*this); - positionHistory[positionIndex] = hashKey; positionIndex++; + positionHistory[positionIndex] = hashKey; } bool Board::InPossibleZug(bool side) { diff --git a/source/search.cpp b/source/search.cpp index a7dbaad..7e5cec7 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -84,14 +84,14 @@ void ListScores(Board &board, int ply) { } static bool IsThreefold(Board &board) { - int count = 0; - - for (int i = positionIndex - 1; i >= 0; i--) { - if (positionHistory[i] == board.hashKey) count++; - - if (count >= 3) return true; + for (int i = 0; i < positionIndex; i++) { + if (positionHistory[i] == board.hashKey) { + // repetition found + return true; + } } + // no repetition return false; } -- 2.47.3 From a6126b1e8b83d2d5b80f27113abd4d5250192b28 Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 18 Mar 2025 14:24:04 +0100 Subject: [PATCH 5/5] small adjustment | bench 1154407 --- source/search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index 7e5cec7..3803656 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -152,9 +152,9 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { } } + pvLine.SetLength(ply); if (ply && IsThreefold(board)) return 0; - pvLine.SetLength(ply); // if NOT PV node then we try to hit the TTable if (beta - alpha == 1) { -- 2.47.3