shithub: moonfish

Download patch

ref: bcd7911d40334fb07b3c0e6782a9ad4604486b1f
parent: f2a11a990f128c1ee0fe5536f45a5cbb850a79cf
author: zamfofex <zamfofex@twdb.moe>
date: Wed Apr 24 17:16:39 EDT 2024

tweak time management

--- a/main.c
+++ b/main.c
@@ -145,13 +145,15 @@
 			if (depth >= 0)
 				score = moonfish_best_move_depth(analysis, &move, depth);
 			else if (time >= 0)
-				score = moonfish_best_move_time(analysis, &move, &depth, time);
+				score = moonfish_best_move_time(analysis, &move, time);
 			else
 #endif
 			if (chess.white)
-				score = moonfish_best_move_clock(analysis, &move, &depth, wtime, btime);
+				score = moonfish_best_move_clock(analysis, &move, wtime, btime);
 			else
-				score = moonfish_best_move_clock(analysis, &move, &depth, btime, wtime);
+				score = moonfish_best_move_clock(analysis, &move, btime, wtime);
+			
+			if (depth < 0) depth = 4;
 			
 			printf("info depth %d ", depth);
 			if (score >= moonfish_omega || score <= -moonfish_omega)
--- a/moonfish.h
+++ b/moonfish.h
@@ -70,8 +70,8 @@
 void moonfish_play(struct moonfish_chess *chess, struct moonfish_move *move);
 void moonfish_unplay(struct moonfish_chess *chess, struct moonfish_move *move);
 
-int moonfish_best_move_time(struct moonfish_analysis *analysis, struct moonfish_move *move, int *depth, long int time);
-int moonfish_best_move_clock(struct moonfish_analysis *analysis, struct moonfish_move *move, int *depth, long int our_time, long int their_time);
+int moonfish_best_move_time(struct moonfish_analysis *analysis, struct moonfish_move *move, long int time);
+int moonfish_best_move_clock(struct moonfish_analysis *analysis, struct moonfish_move *move, long int our_time, long int their_time);
 int moonfish_countdown(int score);
 
 struct moonfish_analysis *moonfish_analysis(char *argv0);
--- a/search.c
+++ b/search.c
@@ -130,7 +130,7 @@
 	struct moonfish_move moves[256];
 	int x, y;
 	int count;
-	long int t1;
+	long int t1, c;
 	
 	if (depth < 0)
 	{
@@ -143,9 +143,7 @@
 	}
 	else if (info->analysis->time >= 0 && time < 0)
 	{
-		score = info->chess.score;
-		if (!info->chess.white) score *= -1;
-		return score;
+		depth = 0;
 	}
 	
 	count = 0;
@@ -171,9 +169,10 @@
 			continue;
 		
 		t1 = moonfish_clock(info->analysis);
+		c = time * i / count - t1 + t0;
 		
 		moonfish_play(&info->chess, moves + i);
-		score = -moonfish_search(info, -beta, -alpha, depth - 1, t1, (time - t1 + t0) / (count - i));
+		score = -moonfish_search(info, -beta, -alpha, depth - 1, t1, time / count + c);
 		moonfish_unplay(&info->chess, moves + i);
 		
 		if (score >= beta) return beta;
@@ -271,22 +270,21 @@
 
 #endif
 
-int moonfish_best_move_time(struct moonfish_analysis *analysis, struct moonfish_move *best_move, int *depth, long int time)
+int moonfish_best_move_time(struct moonfish_analysis *analysis, struct moonfish_move *best_move, long int time)
 {
 	time -= 125;
 	if (time < 10) time = 10;
-	analysis->depth = 8;
+	analysis->depth = 16;
 	analysis->time = time;
 	moonfish_iteration(analysis, best_move);
-	*depth = 4;
 	return analysis->score;
 }
 
-int moonfish_best_move_clock(struct moonfish_analysis *analysis, struct moonfish_move *best_move, int *depth, long int our_time, long int their_time)
+int moonfish_best_move_clock(struct moonfish_analysis *analysis, struct moonfish_move *best_move, long int our_time, long int their_time)
 {
-	long int time;
-	time = our_time * 3 / 4 - their_time;
-	if (time < 0) time = 0;
-	time += our_time / 8;
-	return moonfish_best_move_time(analysis, best_move, depth, time);
+	long int time0, time1;
+	time0 = our_time / 16;
+	time1 = our_time - time0 - their_time * 7 / 8;
+	if (time1 < 0) time1 = 0;
+	return moonfish_best_move_time(analysis, best_move, time0 + time1);
 }
--