diff options
| author | Adam <adammegarules1@gmail.com> | 2026-08-01 09:45:31 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-08-01 09:45:31 +0200 |
| commit | 25af8ae92ffd1a4c2dd0007b155ddb3cbd0d6e89 (patch) | |
| tree | 222cfabffac888d193152950348ffa1b8aa48035 | |
| parent | 61bbd36293516e122297dc9d157b3b8ee886fab6 (diff) | |
adding node couting
| -rw-r--r-- | src/bot.cpp | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/src/bot.cpp b/src/bot.cpp index b5d2b57..03636f9 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -6,6 +6,7 @@ #include <algorithm> #include <cassert> +#include <chrono> #include <cmath> #include <cstdint> #include <ctime> @@ -24,6 +25,8 @@ constexpr int MATE = 10000; constexpr int MATE_THRESHOLD = MATE - 1000; constexpr int INF = 100000000; +uint64_t Nodes = 0; + static int ScoreMove(const Game *board, const Move &move, const Move *bestMove) { // Indexed by PieceType (NONE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING). @@ -77,6 +80,7 @@ static std::vector<Move> GetSortedLegalMoves(Game *g, bool generateQuietMoves, // mate distances stay consistent (a mate found inside quiescence must not look // faster than a real mate-in-1). White perspective throughout. static int quiescenceSearch(Game *b, int qdepth, int alpha, int beta, int ply) { + Nodes++; // Runs GetNewGameState internally, so b->state is up to date afterwards. const int standPat = EvaluateBoard(b); @@ -119,6 +123,7 @@ static int quiescenceSearch(Game *b, int qdepth, int alpha, int beta, int ply) { }; static int search(int depth, Game *b, int alpha, int beta, int ply) { + Nodes++; const uint64_t gameHash = GenerateZobristKey(b); auto repIt = b->ThreeFoldMap.find(gameHash); @@ -208,10 +213,12 @@ static SearchResult SearchDepth(Game *b, int depth, const Move *previousBest) { Move bestMove = moves[0]; int bestEval = -INF; + int alpha = -INF; + int beta = INF; for (Move move : moves) { UndoMove undo = MakeMove(move, b); - int eval = -search(depth - 1, b, -INF, INF, 1); + int eval = -search(depth - 1, b, -beta, -alpha, 1); UnMakeMove(undo, b); @@ -219,12 +226,14 @@ static SearchResult SearchDepth(Game *b, int depth, const Move *previousBest) { bestEval = eval; bestMove = move; } + alpha = std::max(alpha, eval); } return {bestMove, bestEval}; } -static void PrintInfo(const int depth, const int engineScore) { +static void PrintInfo(const int depth, const int engineScore, + const uint64_t nps) { if (std::abs(engineScore) >= MATE_THRESHOLD) { int movesToMate = (MATE - std::abs(engineScore) + 1) / 2; if (movesToMate < 1) { @@ -234,12 +243,14 @@ static void PrintInfo(const int depth, const int engineScore) { << (engineScore > 0 ? movesToMate : -movesToMate) << "\n" << std::flush; } else { - std::cout << "info depth " << depth << " score cp " << engineScore << "\n" + std::cout << "info depth " << depth << " score cp " << engineScore + << " nodes " << Nodes << " nps " << nps << "\n" << std::flush; } } Move GetBestMove(Game *b, const int maxDepth) { + Nodes = 0; int actualDepth = maxDepth > 0 ? maxDepth : MAXIMUM_DEPTH; bool usingDefaultDepth = actualDepth == MAXIMUM_DEPTH; auto legalMoves = GetSortedLegalMoves(b, true, nullptr); @@ -251,6 +262,7 @@ Move GetBestMove(Game *b, const int maxDepth) { Move bestMove = legalMoves[0]; time_t start = time(0); + auto startMili = std::chrono::steady_clock::now(); bool continueSearching = true; int depth = 1; @@ -259,7 +271,15 @@ Move GetBestMove(Game *b, const int maxDepth) { SearchResult result = SearchDepth(b, depth, &bestMove); bestMove = result.bestMove; - PrintInfo(depth, b->turn ? result.score : -result.score); + auto now = std::chrono::steady_clock::now(); + + double seconds = std::chrono::duration<double>(now - startMili).count(); + + uint64_t nps = + seconds > 0 + ? static_cast<uint64_t>(static_cast<double>(Nodes) / seconds) + : Nodes; + PrintInfo(depth, result.score, nps); // A mate was found; deeper searches can only find a faster one. if (std::abs(result.score) >= MATE_THRESHOLD) { @@ -269,8 +289,8 @@ Move GetBestMove(Game *b, const int maxDepth) { if (depth >= actualDepth) { continueSearching = false; } - int seconds_since_start = static_cast<int>(difftime(time(0), start)); - if (seconds_since_start >= MAXIMUM_TIME_PER_MOVE && usingDefaultDepth) { + int since_start = static_cast<int>(difftime(time(0), start)); + if (since_start >= MAXIMUM_TIME_PER_MOVE && usingDefaultDepth) { continueSearching = false; } } |
