diff --git a/source/benchmark.cpp b/source/benchmark.cpp index 4c24ac1..f18c0ab 100644 --- a/source/benchmark.cpp +++ b/source/benchmark.cpp @@ -65,7 +65,7 @@ void RunBenchmark() { Board board; int depth = BENCH_DEPTH; int ply = 0; - positionIndex = 0; + board.positionIndex = 0; // Clearing TT.Clear(); @@ -81,5 +81,5 @@ void RunBenchmark() { std::cout << ctx.nodes << " nodes " << int(ctx.nodes/sw.GetElapsedSec()) << " nps" << std::endl; TT.Clear(); - positionIndex = 0; + board.positionIndex = 0; } diff --git a/source/board.cpp b/source/board.cpp index eabfae5..96f8228 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -28,6 +28,8 @@ void Board::Reset() { currentMoveIndex = 0; + positionIndex = 0; + hashKey = 0ULL; } @@ -441,4 +443,29 @@ 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 5ac1cbc..ed841a9 100644 --- a/source/board.h +++ b/source/board.h @@ -10,7 +10,6 @@ constexpr int noEPTarget = -1; constexpr int MAX_MOVES = 218; -inline int positionIndex = 0; inline U64 positionHistory[1000]; class Board { @@ -40,6 +39,8 @@ public: int halfMoves = 0; int fullMoves = 1; + int positionIndex = 0; + U64 hashKey = 0ULL; void Reset(); @@ -66,4 +67,6 @@ public: bool MakeMove(Move move); bool InPossibleZug(); + + bool IsDraw(); }; diff --git a/source/datagen.cpp b/source/datagen.cpp index 573f47a..d4bd4f2 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -63,8 +63,7 @@ static void PlayRandMoves(Board &board) { } static bool IsGameOver(Board &board) { - bool isDraw = SEARCH::IsDraw(board); - if (isDraw) return true; + if (board.IsDraw()) return true; int moveSeen = 0; @@ -73,7 +72,6 @@ static bool IsGameOver(Board &board) { bool isLegal = copy.MakeMove(board.moveList[i]); if (!isLegal) continue; - positionIndex--; moveSeen++; } @@ -99,8 +97,6 @@ void PlayGame(std::atomic& positions, U64 targetPositions, std::vector= 100); -} - -static bool IsInsuffMat(Board &board) { - return (board.occupied.PopCount() <= 3 - && !(board.pieces[Pawn] | board.pieces[Queen] | board.pieces[Rook])); -} - -bool IsDraw(Board &board) { - return IsFifty(board) || IsInsuffMat(board) || IsThreefold(board); -} - static int GetReductions(Board &board, Move &move, int depth, int moveSeen, int ply) { int reduction = 0; @@ -174,7 +149,6 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply, Searc if (!isLegal) continue; ctx.nodes++; int score = -Quiescence(copy, -beta, -alpha, ply + 1, ctx).score; - positionIndex--; if (score >= beta) { return score; @@ -217,7 +191,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } ctx.pvLine.SetLength(ply); - if (ply && (IsThreefold(board) || IsFifty(board) || IsInsuffMat(board))) return 0; + if (ply && (board.IsDraw())) return 0; // if NOT PV node then we try to hit the TTable @@ -316,7 +290,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply, SearchCo } moveSeen++; - positionIndex--; if (ctx.searchStopped) return 0; diff --git a/source/uci.cpp b/source/uci.cpp index 01e131a..11b1370 100644 --- a/source/uci.cpp +++ b/source/uci.cpp @@ -125,7 +125,6 @@ void UCILoop(Board &board) { // parse UCI "position" command if (input.find("position") != std::string::npos) { ParsePosition(board, input); - positionIndex = 0; continue; } @@ -139,8 +138,6 @@ void UCILoop(Board &board) { ctx.killerMoves = {}; ctx.history.Clear(); - - positionIndex = 0; continue; }