ref: e020be111c634b4d2cc5e13178feae9527171be4
parent: 3e86752a094eee5acfda25e65f699a3cb4ab7aeb
author: zamfofex <zamfofex@twdb.moe>
date: Fri Oct 13 14:00:53 EDT 2023
consider the time remaining when making moves
--- a/README.md
+++ b/README.md
@@ -32,7 +32,6 @@
- no good move ordering heuristic
- no support for `go infinite` or `go mate`
- no move name or FEN validation (may lead to potential exploits)
-- the bot will ignore time control information
building
---
--- a/main.c
+++ b/main.c
@@ -12,6 +12,7 @@
char *arg;
struct moonfish_move move;
char name[6];
+ int time;
if (argc < 1) return 1;
@@ -98,7 +99,24 @@
if (!strcmp(arg, "go"))
{- moonfish_best_move(ctx, &move);
+ if (ctx->chess.white) arg = strstr(arg, " wtime ");
+ else arg = strstr(arg, " btime ");
+
+ if (arg == NULL)
+ {+ time = 120000;
+ }
+ else
+ {+ if (sscanf(arg + 6, "%d", &time) != 1)
+ {+ free(ctx);
+ fprintf(stderr, "%s: malformed 'go' command\n", argv[0]);
+ return 1;
+ }
+ }
+
+ moonfish_best_move(ctx, &move, time / 1000);
moonfish_to_uci(name, &move, ctx->chess.white);
printf("bestmove %s\n", name);}
--- a/moonfish.h
+++ b/moonfish.h
@@ -78,7 +78,7 @@
int moonfish_tanh(int value);
-int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *move);
+int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *move, int time);
void moonfish_play_uci(struct moonfish_chess *chess, char *name);
void moonfish_to_uci(char *name, struct moonfish_move *move, int white);
--- a/search.c
+++ b/search.c
@@ -1,3 +1,5 @@
+#include <time.h>
+
#include "moonfish.h"
static int moonfish_evaluate(struct moonfish_chess *chess, struct moonfish_nnue *nnue)
@@ -109,7 +111,7 @@
return NULL;
}
-int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *best_move)
+static int moonfish_best_move_depth(struct moonfish *ctx, struct moonfish_move *best_move, int depth)
{int x, y;
struct moonfish_move *move, moves[32];
@@ -140,7 +142,8 @@
infos[count].move = *move;
infos[count].chess = ctx->chess;
infos[count].nnue = &ctx->nnue;
- infos[count].depth = 3;
+ infos[count].depth = depth;
+
result = pthread_create(&infos[count].thread, NULL, &moonfish_start_search, infos + count);
if (result)
{@@ -178,7 +181,7 @@
#else
-int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *best_move)
+static int moonfish_best_move_depth(struct moonfish *ctx, struct moonfish_move *best_move, int depth)
{int x, y;
struct moonfish_move moves[32];
@@ -202,7 +205,7 @@
continue;
}
- score = -moonfish_search(&ctx->chess, &ctx->nnue, -100 * moonfish_omega, 100 * moonfish_omega, 3);
+ score = -moonfish_search(&ctx->chess, &ctx->nnue, -100 * moonfish_omega, 100 * moonfish_omega, depth);
moonfish_unplay(&ctx->chess, move);
if (score > best_score)
@@ -217,3 +220,24 @@
}
#endif
+
+int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *best_move, int d)
+{+ time_t t, t0, t1;
+ int i;
+ int score;
+
+ d /= 4;
+ t0 = time(NULL);
+
+ for (i = 0 ; i < 8 ; i++)
+ {+ t = time(NULL);
+ score = moonfish_best_move_depth(ctx, best_move, i);
+
+ t1 = time(NULL);
+ if ((t1 - t) * 32 > d - (t1 - t0)) break;
+ }
+
+ return score;
+}
--
⑨