From 2e8c23103fad7903ce9938ea002cb3eaab925c09 Mon Sep 17 00:00:00 2001 From: rektdie Date: Fri, 11 Apr 2025 19:49:03 +0200 Subject: [PATCH] encapsulated positionHistory | bench 1857933 --- source/benchmark.cpp | 2 -- source/board.cpp | 26 -------------------------- source/board.h | 4 ---- source/datagen.cpp | 6 +++--- source/search.cpp | 29 ++++++++++++++++++++++++++++- source/search.h | 4 +++- 6 files changed, 34 insertions(+), 37 deletions(-) diff --git a/source/benchmark.cpp b/source/benchmark.cpp index f18c0ab..3b1d635 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,7 +65,6 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; - board.positionIndex = 0; // Clearing TT.Clear(); @@ -81,5 +80,4 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); - board.positionIndex = 0; } diff --git a/source/board.cpp b/source/board.cpp index 96f8228..695fc0c 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -393,7 +393,6 @@ bool Board::MakeMove(Move move) { } positionIndex++; - positionHistory[positionIndex] = hashKey; return true; } @@ -444,28 +443,3 @@ void Board::ResetAccPair() { blackPieces.PopBit(square); } } - -static bool IsThreefold(Board& board) { - for (int i = 0; i < board.positionIndex; i++) { - if (positionHistory[i] == board.hashKey) { - // repetition found - return true; - } - } - - // no repetition - return false; -} - -static bool IsFifty(Board& board) { - return (board.halfMoves >= 100); -} - -static bool IsInsuffMat(Board& board) { - return (board.occupied.PopCount() <= 3 - && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); -} - -bool Board::IsDraw() { - return IsFifty(*this) || IsInsuffMat(*this) || IsThreefold(*this); -} \ No newline at end of file diff --git a/source/board.h b/source/board.h index ed841a9..8c3667e 100644 --- a/source/board.h +++ b/source/board.h @@ -10,8 +10,6 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; -inline U64 positionHistory[1000]; - class Board { public: Board() { @@ -67,6 +65,4 @@ public: bool MakeMove(Move move); bool InPossibleZug(); - - bool IsDraw(); }; diff --git a/source/datagen.cpp b/source/datagen.cpp index d4bd4f2..2e33f63 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -62,8 +62,8 @@ static void PlayRandMoves(Board &board) { MOVEGEN::GenerateMoves(board); } -static bool IsGameOver(Board &board) { - if (board.IsDraw()) return true; +static bool IsGameOver(Board &board, SEARCH::SearchContext& ctx) { + if (SEARCH::IsDraw(board, ctx)) return true; int moveSeen = 0; @@ -106,7 +106,7 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector(board); - while (!IsGameOver(board)) { + while (!IsGameOver(board, ctx)) { SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); if (!results.bestMove) break; diff --git a/source/search.cpp b/source/search.cpp index 60d7647..3104a68 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -92,6 +92,31 @@ void ListScores(Board &board, int ply, SearchContext& ctx) { } } +static bool IsThreefold(Board& board, SearchContext& ctx) { + for (int i = 0; i < board.positionIndex; i++) { + if (ctx.positionHistory[i] == board.hashKey) { + // repetition found + return true; + } + } + + // no repetition + return false; +} + +static bool IsFifty(Board& board) { + return (board.halfMoves >= 100); +} + +static bool IsInsuffMat(Board& board) { + return (board.occupied.PopCount() <= 3 + && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); +} + +static bool IsDraw(Board &board, SearchContext& ctx) { + return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board, ctx); +} + static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { int reduction = 0; @@ -147,6 +172,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc Board copy = board; int isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; + ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; int score = -Quiescence(copy, -beta, -alpha, ply + 1, ctx).score; @@ -191,7 +217,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } ctx.pvLine.SetLength(ply); - if (ply && (board.IsDraw())) return 0; + if (ply && (IsDraw(board, ctx))) return 0; // if NOT PV node then we try to hit the TTable @@ -261,6 +287,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo bool isLegal = copy.MakeMove(currMove); if (!isLegal) continue; + ctx.positionHistory[copy.positionIndex] = copy.hashKey; ctx.nodes++; int reductions = GetReductions(board, currMove, depth, moveSeen, ply); diff --git a/source/search.h b/source/search.h index dd0ce78..5eaea00 100644 --- a/source/search.h +++ b/source/search.h @@ -113,6 +113,7 @@ public: History history; std::array, 2> killerMoves{}; + std::array positionHistory; Stopwatch sw; @@ -121,6 +122,7 @@ public: history.Clear(); sw.Restart(); killerMoves = {}; + positionHistory = {}; } }; @@ -130,7 +132,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo template SearchResults SearchPosition(Board &board, SearchParams params, SearchContext& ctx); -bool IsDraw(Board &board); +bool IsDraw(Board &board, SearchContext& ctx); constexpr int moveScoreTable[6][6] = { 105, 205, 305, 405, 505, 605,