aboutsummaryrefslogtreecommitdiff
path: root/src/bot.cpp
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-08-08 17:37:52 +0200
committerAdam <adammegarules1@gmail.com>2026-08-08 17:37:52 +0200
commitf4e219a591d0265e95f173c1da5c9563c9680adc (patch)
treecbe46bfbf1165b9a809d3d6a3672b2933b6f1dcf /src/bot.cpp
parent9cbe5a0fbd3ca3a10703452d2aaf3a5c3759c704 (diff)
adding time management
Diffstat (limited to 'src/bot.cpp')
-rw-r--r--src/bot.cpp68
1 files changed, 55 insertions, 13 deletions
diff --git a/src/bot.cpp b/src/bot.cpp
index 3567ef3..4fb140e 100644
--- a/src/bot.cpp
+++ b/src/bot.cpp
@@ -10,9 +10,9 @@
#include <cmath>
#include <cstdint>
#include <cstdlib>
-#include <ctime>
#include <iostream>
#include <iterator>
+#include <ratio>
#include <vector>
constexpr int MAXIMUM_DEPTH = 7;
@@ -28,6 +28,10 @@ constexpr int INF = 100000000;
uint64_t Nodes = 0;
+double timeToThingMS = -1;
+std::chrono::time_point<std::chrono::steady_clock> searchStartTime;
+bool searchStopped = false;
+
static int ScoreMove(const Game *board, const Move &move,
const Move *bestMove) {
// Indexed by PieceType (NONE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING).
@@ -125,6 +129,20 @@ 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++;
+ if ((Nodes & 2047) == 0) {
+ double elapsedMiliseconds =
+ std::chrono::duration<double, std::milli>(
+ std::chrono::steady_clock::now() - searchStartTime)
+ .count();
+ if (timeToThingMS == -1) {
+ assert(false && "Expected set time");
+ exit(1);
+ }
+ if (elapsedMiliseconds >= timeToThingMS) {
+ searchStopped = true;
+ return 0;
+ }
+ }
const uint64_t gameHash = GenerateZobristKey(b);
auto repIt = b->ThreeFoldMap.find(gameHash);
@@ -177,7 +195,9 @@ static int search(int depth, Game *b, int alpha, int beta, int ply) {
int score = -search(depth - 1, b, -beta, -alpha, ply + 1);
UnMakeMove(undo, b);
-
+ if (searchStopped) {
+ break;
+ }
if (score > bestScore) {
bestScore = score;
bestMove = move;
@@ -187,12 +207,14 @@ static int search(int depth, Game *b, int alpha, int beta, int ply) {
break;
}
}
+ if (!searchStopped) {
+ const Flag flag = bestScore <= alphaOrig ? UPPERBOUND
+ : bestScore >= beta ? LOWERBOUND
+ : EXACT;
- const Flag flag = bestScore <= alphaOrig ? UPPERBOUND
- : bestScore >= beta ? LOWERBOUND
- : EXACT;
- (*b->Transpositions)[gameHash] = {
- .depth = depth, .Eval = bestScore, .flag = flag, .bestMove = bestMove};
+ (*b->Transpositions)[gameHash] = {
+ .depth = depth, .Eval = bestScore, .flag = flag, .bestMove = bestMove};
+ }
return bestScore;
}
@@ -223,6 +245,9 @@ static SearchResult SearchDepth(Game *b, int depth, const Move *previousBest) {
UnMakeMove(undo, b);
+ if (searchStopped) {
+ break;
+ }
if (eval > bestEval) {
bestEval = eval;
bestMove = move;
@@ -250,7 +275,8 @@ static void PrintInfo(const int depth, const int engineScore,
}
}
-Move GetBestMove(Game *b, const int maxDepth) {
+Move GetBestMove(Game *b, int maxDepth, move_options options) {
+ searchStopped = false;
Nodes = 0;
int actualDepth = maxDepth > 0 ? maxDepth : MAXIMUM_DEPTH;
@@ -263,8 +289,17 @@ Move GetBestMove(Game *b, const int maxDepth) {
Move bestMove = legalMoves[0];
- time_t start = time(0);
- auto startMili = std::chrono::steady_clock::now();
+ searchStartTime = std::chrono::steady_clock::now();
+
+ timeToThingMS = INF; // to big number to ever achiave
+
+ if (options.wtime != -1 && options.btime != -1) {
+ usingDefaultDepth = false;
+ int increment = b->turn ? options.wncr : options.bncr;
+ double time_remaning = b->turn ? options.wtime : options.btime;
+
+ timeToThingMS = (time_remaning / 20 + increment * 0.8);
+ }
bool continueSearching = true;
int depth = 1;
@@ -275,7 +310,8 @@ Move GetBestMove(Game *b, const int maxDepth) {
auto now = std::chrono::steady_clock::now();
- double seconds = std::chrono::duration<double>(now - startMili).count();
+ double seconds =
+ std::chrono::duration<double>(now - searchStartTime).count();
uint64_t nps =
seconds > 0
@@ -291,8 +327,14 @@ Move GetBestMove(Game *b, const int maxDepth) {
if (depth > actualDepth) {
continueSearching = false;
}
- int since_start = static_cast<int>(difftime(time(0), start));
- if (since_start >= MAXIMUM_TIME_PER_MOVE && usingDefaultDepth) {
+ double elapsedSeconds =
+ std::chrono::duration<double>(std::chrono::steady_clock::now() -
+ searchStartTime)
+ .count();
+ if (elapsedSeconds >= MAXIMUM_TIME_PER_MOVE && usingDefaultDepth) {
+ continueSearching = false;
+ }
+ if (searchStopped) {
continueSearching = false;
}
}