aboutsummaryrefslogtreecommitdiff
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
parent9cbe5a0fbd3ca3a10703452d2aaf3a5c3759c704 (diff)
adding time management
-rw-r--r--src/bot.cpp68
-rw-r--r--src/bot.hpp9
-rw-r--r--src/uci.cpp22
3 files changed, 83 insertions, 16 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;
}
}
diff --git a/src/bot.hpp b/src/bot.hpp
index c271882..9f97e2c 100644
--- a/src/bot.hpp
+++ b/src/bot.hpp
@@ -3,6 +3,13 @@
#include "board/board.hpp"
-Move GetBestMove(Game *b, int depth);
+struct move_options {
+ int wtime;
+ int btime;
+ int wncr;
+ int bncr;
+};
+
+Move GetBestMove(Game *b, int depth, move_options options);
#endif /* SRC_BOT_H_ */
diff --git a/src/uci.cpp b/src/uci.cpp
index 37a68b8..6d2f652 100644
--- a/src/uci.cpp
+++ b/src/uci.cpp
@@ -191,19 +191,37 @@ void Uci() {
string token;
int depth = -1;
+ int wtime = -1;
+ int btime = -1;
+
+ int wncr = 0;
+ int bncr = 0;
+
ss >> token; // go
while (ss >> token) {
if (token == "depth") {
ss >> depth;
- break;
+ }
+ if (token == "wtime") {
+ ss >> wtime;
+ }
+ if (token == "btime") {
+ ss >> btime;
+ }
+ if (token == "incr") {
+ ss >> wncr;
+ bncr = wncr;
}
}
+
if (depth != -1 && depth <= 0) {
cout << "Fatal: Depth must be positive integer";
exit(1);
}
- Move best = GetBestMove(&game, depth);
+ move_options options = {
+ .wtime = wtime, .btime = btime, .wncr = wncr, .bncr = bncr};
+ Move best = GetBestMove(&game, depth, options);
cout << "bestmove ";
cout << static_cast<char>(best.From.file + 'a') << 1 + best.From.rank