diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 3b1d635..f18c0ab 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,6 +65,7 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; + board.positionIndex = 0; // Clearing TT.Clear(); @@ -80,4 +81,5 @@ 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 695fc0c..96f8228 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -393,6 +393,7 @@ bool Board::MakeMove(Move move) { } positionIndex++; + positionHistory[positionIndex] = hashKey; return true; } @@ -443,3 +444,28 @@ 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 8c3667e..ed841a9 100644 --- a/source/board.h +++ b/source/board.h @@ -10,6 +10,8 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; +inline U64 positionHistory[1000]; + class Board { public: Board() { @@ -65,4 +67,6 @@ public: bool MakeMove(Move move); bool InPossibleZug(); + + bool IsDraw(); }; diff --git a/source/datagen.cpp b/source/datagen.cpp index 2e33f63..d4bd4f2 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, SEARCH::SearchContext& ctx) { - if (SEARCH::IsDraw(board, ctx)) return true; +static bool IsGameOver(Board &board) { + if (board.IsDraw()) return true; int moveSeen = 0; @@ -106,7 +106,7 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector(board); - while (!IsGameOver(board, ctx)) { + while (!IsGameOver(board)) { SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams(), ctx); if (!results.bestMove) break; diff --git a/source/search.cpp b/source/search.cpp index 3104a68..60d7647 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -92,31 +92,6 @@ 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; @@ -172,7 +147,6 @@ 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; @@ -217,7 +191,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } ctx.pvLine.SetLength(ply); - if (ply && (IsDraw(board, ctx))) return 0; + if (ply && (board.IsDraw())) return 0; // if NOT PV node then we try to hit the TTable @@ -287,7 +261,6 @@ 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 5eaea00..dd0ce78 100644 --- a/source/search.h +++ b/source/search.h @@ -113,7 +113,6 @@ public: History history; std::array, 2> killerMoves{}; - std::array positionHistory; Stopwatch sw; @@ -122,7 +121,6 @@ public: history.Clear(); sw.Restart(); killerMoves = {}; - positionHistory = {}; } }; @@ -132,7 +130,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, SearchContext& ctx); +bool IsDraw(Board &board); constexpr int moveScoreTable[6][6] = { 105, 205, 305, 405, 505, 605,