diff options
| -rw-r--r-- | src/bot.cpp | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/src/bot.cpp b/src/bot.cpp index 71c9277..5238315 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -8,12 +8,14 @@ #include <cassert> #include <cmath> #include <cstdint> +#include <ctime> #include <iostream> #include <iterator> #include <limits> #include <vector> -constexpr int DEFAULT_DEPTH_LIMIT = 4; +constexpr int MAXIMUM_DEPTH = 10; +constexpr int MAXIMUM_TIME_PER_MOVE = 10; constexpr int Q_DEPTH_LIMIT = 4; constexpr int MATE = 10000; @@ -295,8 +297,8 @@ static void PrintInfo(const int depth, const int engineScore) { } Move GetBestMove(Game *b, const int maxDepth) { - const int depthLimit = maxDepth > 0 ? maxDepth : DEFAULT_DEPTH_LIMIT; - + int actualDepth = maxDepth > 0 ? maxDepth : MAXIMUM_DEPTH; + bool usingDefaultDepth = actualDepth == MAXIMUM_DEPTH; auto legalMoves = GetSortedLegalMoves(b, true, nullptr); if (legalMoves.empty()) { assert(false && "GetBestMove called with no legal moves"); @@ -305,7 +307,12 @@ Move GetBestMove(Game *b, const int maxDepth) { Move bestMove = legalMoves[0]; - for (int depth = 1; depth <= depthLimit; depth++) { + time_t start = time(0); + + bool continueSearching = true; + int depth = 1; + + while (continueSearching) { SearchResult result = SearchDepth(b, depth, &bestMove); bestMove = result.bestMove; @@ -315,6 +322,14 @@ Move GetBestMove(Game *b, const int maxDepth) { if (std::abs(result.score) >= MATE_THRESHOLD) { break; } + depth++; + if (depth >= actualDepth) { + continueSearching = false; + } + int seconds_since_start = difftime(time(0), start); + if (seconds_since_start >= MAXIMUM_TIME_PER_MOVE && usingDefaultDepth) { + continueSearching = false; + } } return bestMove; |
