shithub: moonfish

Download patch

ref: 088335597c2f6b3acc13935811c4fbddb9814c13
parent: 5e544b4758b878cc39fea1358b756033ece854df
author: zamfofex <zamfofex@twdb.moe>
date: Sun Jun 8 03:15:33 EDT 2025

make some simplifications

--- a/scripts/check.txt
+++ b/scripts/check.txt
@@ -17,7 +17,7 @@
 info depth 2 nodes 1024 score cp 638
 bestmove d7c8q
 perft 0: 1
-info depth 2 nodes 1024 score cp 8
+info depth 2 nodes 1024 score cp 16
 bestmove e2d1
 perft 1: 20
 info depth 2 nodes 1024 score cp 13
@@ -38,7 +38,7 @@
 info depth 2 nodes 1024 score cp 638
 bestmove d7c8q
 perft 1: 46
-info depth 2 nodes 1024 score cp 8
+info depth 2 nodes 1024 score cp 16
 bestmove e2d1
 perft 2: 400
 info depth 2 nodes 1024 score cp 13
@@ -59,7 +59,7 @@
 info depth 2 nodes 1024 score cp 638
 bestmove d7c8q
 perft 2: 2079
-info depth 2 nodes 1024 score cp 8
+info depth 2 nodes 1024 score cp 16
 bestmove e2d1
 perft 3: 8902
 info depth 3 nodes 4096 score cp 26
@@ -80,16 +80,16 @@
 info depth 3 nodes 4096 score cp 615
 bestmove d7c8q
 perft 3: 89890
-info depth 3 nodes 4096 score cp 68
+info depth 3 nodes 4096 score cp 11
 bestmove f1c1
 perft 4: 197281
 info depth 4 nodes 65536 score cp -15
 bestmove g1f3
 perft 4: 4085603
-info depth 4 nodes 65536 score cp 120
+info depth 4 nodes 65536 score cp 228
 bestmove e2a6
 perft 4: 43238
-info depth 4 nodes 65536 score cp 25
+info depth 4 nodes 65536 score cp 55
 bestmove b4f4
 perft 4: 422333
 info depth 4 nodes 65536 score cp -194
@@ -98,8 +98,8 @@
 info depth 4 nodes 65536 score cp -194
 bestmove c5c4
 perft 4: 2103487
-info depth 4 nodes 65536 score cp 579
+info depth 4 nodes 65536 score cp 554
 bestmove d7c8q
 perft 4: 3894594
-info depth 4 nodes 65536 score cp -6
+info depth 4 nodes 65536 score cp 20
 bestmove c3d5
--- a/scripts/compare.sh
+++ b/scripts/compare.sh
@@ -19,6 +19,7 @@
 [[ "x$(git status --porcelain)" = x ]] || dirty=-dirty
 
 rev1="$(git rev-parse --short HEAD)"
+rm -f *.o
 make moonfish
 mv -f moonfish compare/moonfish-"$rev1$dirty"
 
@@ -26,6 +27,7 @@
 git reset --hard "${1:-main}"
 
 rev2="$(git rev-parse --short HEAD)"
+rm -f *.o
 make moonfish
 mv -f moonfish compare/moonfish-"$rev2"
 
--- a/search.c
+++ b/search.c
@@ -60,7 +60,7 @@
 	_Atomic short int score;
 	_Atomic unsigned char bounds[2];
 	_Atomic unsigned char ignored;
-	unsigned char from, index;
+	struct moonfish_move move;
 };
 
 struct moonfish_root {
@@ -75,8 +75,8 @@
 
 static short int moonfish_score(struct moonfish_chess *chess)
 {
-	static short int values0[] = {0, 0, 0, 0, 56, 96, 84, 65, 61, 92, 74, 79, 58, 88, 83, 95, 69, 102, 96, 115, 82, 132, 156, 159, 258, 226, 262, 274, 0, 0, 0, 0, 262, 317, 317, 310, 323, 314, 337, 344, 321, 350, 356, 367, 341, 370, 371, 371, 366, 368, 406, 398, 361, 399, 433, 442, 338, 329, 420, 402, 194, 295, 237, 363, 356, 375, 360, 346, 382, 394, 396, 378, 386, 393, 387, 395, 382, 390, 392, 414, 380, 397, 421, 430, 406, 429, 426, 440, 374, 391, 413, 395, 343, 339, 291, 298, 456, 467, 469, 477, 427, 460, 462, 465, 440, 462, 447, 458, 447, 457, 454, 469, 474, 484, 496, 513, 491, 529, 533, 548, 523, 521, 552, 552, 564, 550, 535, 552, 1046, 1032, 1038, 1058, 1046, 1062, 1070, 1058, 1049, 1063, 1056, 1052, 1047, 1056, 1051, 1046, 1073, 1053, 1058, 1057, 1078, 1083, 1077, 1071, 1049, 1001, 1063, 1041, 1057, 1071, 1089, 1107, 20, 37, -21, -15, 18, 4, -61, -82, -63, -50, -84, -97, -93, -68, -82, -97, -77, -40, -24, -40, -31, 28, 52, 36, 44, 13, 53, 63, 163, 141, 61, 70};
-	static short int values1[] = {0, 0, 0, 0, 137, 142, 141, 145, 132, 137, 127, 134, 139, 138, 123, 119, 156, 152, 134, 125, 223, 214, 184, 178, 255, 269, 236, 215, 0, 0, 0, 0, 320, 318, 360, 369, 345, 378, 378, 387, 360, 388, 396, 416, 384, 403, 428, 436, 388, 413, 430, 439, 376, 396, 414, 416, 362, 399, 385, 408, 317, 372, 415, 387, 389, 391, 391, 410, 393, 402, 404, 420, 402, 416, 431, 431, 410, 429, 436, 437, 420, 437, 431, 440, 412, 421, 431, 425, 399, 420, 418, 423, 409, 418, 423, 430, 699, 708, 715, 717, 706, 704, 709, 708, 705, 712, 718, 716, 724, 732, 737, 734, 737, 740, 743, 738, 744, 738, 742, 734, 742, 749, 743, 746, 727, 736, 744, 738, 1255, 1256, 1247, 1234, 1248, 1249, 1246, 1271, 1276, 1274, 1307, 1305, 1308, 1332, 1342, 1359, 1309, 1358, 1374, 1384, 1318, 1333, 1375, 1386, 1327, 1388, 1381, 1410, 1338, 1346, 1357, 1352, -71, -42, -29, -42, -27, -11, 12, 17, -3, 12, 30, 39, 8, 30, 45, 55, 21, 48, 49, 52, 29, 49, 42, 30, -7, 42, 27, 10, -95, -30, -17, -26};
+	static short int scores0[] = {0, 0, 0, 0, 56, 96, 84, 65, 61, 92, 74, 79, 58, 88, 83, 95, 69, 102, 96, 115, 82, 132, 156, 159, 258, 226, 262, 274, 0, 0, 0, 0, 262, 317, 317, 310, 323, 314, 337, 344, 321, 350, 356, 367, 341, 370, 371, 371, 366, 368, 406, 398, 361, 399, 433, 442, 338, 329, 420, 402, 194, 295, 237, 363, 356, 375, 360, 346, 382, 394, 396, 378, 386, 393, 387, 395, 382, 390, 392, 414, 380, 397, 421, 430, 406, 429, 426, 440, 374, 391, 413, 395, 343, 339, 291, 298, 456, 467, 469, 477, 427, 460, 462, 465, 440, 462, 447, 458, 447, 457, 454, 469, 474, 484, 496, 513, 491, 529, 533, 548, 523, 521, 552, 552, 564, 550, 535, 552, 1046, 1032, 1038, 1058, 1046, 1062, 1070, 1058, 1049, 1063, 1056, 1052, 1047, 1056, 1051, 1046, 1073, 1053, 1058, 1057, 1078, 1083, 1077, 1071, 1049, 1001, 1063, 1041, 1057, 1071, 1089, 1107, 20, 37, -21, -15, 18, 4, -61, -82, -63, -50, -84, -97, -93, -68, -82, -97, -77, -40, -24, -40, -31, 28, 52, 36, 44, 13, 53, 63, 163, 141, 61, 70};
+	static short int scores1[] = {0, 0, 0, 0, 137, 142, 141, 145, 132, 137, 127, 134, 139, 138, 123, 119, 156, 152, 134, 125, 223, 214, 184, 178, 255, 269, 236, 215, 0, 0, 0, 0, 320, 318, 360, 369, 345, 378, 378, 387, 360, 388, 396, 416, 384, 403, 428, 436, 388, 413, 430, 439, 376, 396, 414, 416, 362, 399, 385, 408, 317, 372, 415, 387, 389, 391, 391, 410, 393, 402, 404, 420, 402, 416, 431, 431, 410, 429, 436, 437, 420, 437, 431, 440, 412, 421, 431, 425, 399, 420, 418, 423, 409, 418, 423, 430, 699, 708, 715, 717, 706, 704, 709, 708, 705, 712, 718, 716, 724, 732, 737, 734, 737, 740, 743, 738, 744, 738, 742, 734, 742, 749, 743, 746, 727, 736, 744, 738, 1255, 1256, 1247, 1234, 1248, 1249, 1246, 1271, 1276, 1274, 1307, 1305, 1308, 1332, 1342, 1359, 1309, 1358, 1374, 1384, 1318, 1333, 1375, 1386, 1327, 1388, 1381, 1410, 1338, 1346, 1357, 1352, -71, -42, -29, -42, -27, -11, 12, 17, -3, 12, 30, 39, 8, 30, 45, 55, 21, 48, 49, 52, 29, 49, 42, 30, -7, 42, 27, 10, -95, -30, -17, -26};
 	static int values[] = {0, 1, 1, 2, 4, 0};
 	
 	int x, y;
@@ -106,9 +106,8 @@
 			if (color == 1) y1 = 7 - y1;
 			
 			i = x1 + y1 * 4 + type * 32;
-			
-			score0 += values0[i] * ((color ^ chess->white) * 2 - 1);
-			score1 += values1[i] * ((color ^ chess->white) * 2 - 1);
+			score0 += scores0[i] * ((color ^ chess->white) * 2 - 1);
+			score1 += scores1[i] * ((color ^ chess->white) * 2 - 1);
 			phase += values[type];
 		}
 	}
@@ -143,8 +142,9 @@
 	if (!a->ignored && b->ignored) return -1;
 	if (a->ignored && !b->ignored) return 1;
 	if (a->score != b->score) return a->score - b->score;
-	if (a->from != b->from) return a->from - b->from;
-	if (a->index != b->index) return a->index - b->index;
+	if (a->move.piece != b->move.piece) return a->move.piece - b->move.piece;
+	if (a->move.from != b->move.from) return a->move.from - b->move.from;
+	if (a->move.to != b->move.to) return a->move.to - b->move.to;
 	return 0;
 }
 
@@ -173,18 +173,13 @@
 			}
 			
 			for (i = 0 ; i < count ; i++) {
-				
 				other = *chess;
 				moonfish_play(&other, moves + i);
-				
 				if (!moonfish_validate(&other)) continue;
 				moonfish_node(node->children + child_count);
 				node->children[child_count].parent = node;
-				node->children[child_count].from = (x + 1) + (y + 2) * 10;
-				node->children[child_count].index = i;
-				
+				node->children[child_count].move = moves[i];
 				node->children[child_count].score = moonfish_score(&other);
-				
 				child_count++;
 			}
 		}
@@ -192,7 +187,6 @@
 	
 	if (child_count == 0 && node->children != NULL) free(node->children);
 	if (child_count > 0) qsort(node->children, child_count, sizeof *node, &moonfish_compare);
-	
 	node->count = child_count;
 }
 
@@ -202,20 +196,6 @@
 	return 1 / (1 + pow(10, node->score / 400.0)) + 1.41 / node->parent->count * sqrt(node->parent->visits) / (node->visits + 1);
 }
 
-static void moonfish_node_move(struct moonfish_node *node, struct moonfish_chess *chess, struct moonfish_move *move)
-{
-	struct moonfish_move moves[32];
-	moonfish_moves(chess, moves, node->from);
-	*move = moves[node->index];
-}
-
-static void moonfish_node_chess(struct moonfish_node *node, struct moonfish_chess *chess)
-{
-	struct moonfish_move move;
-	moonfish_node_move(node, chess, &move);
-	moonfish_play(chess, &move);
-}
-
 static struct moonfish_node *moonfish_select(struct moonfish_node *node, struct moonfish_chess *chess)
 {
 	struct moonfish_node *next;
@@ -250,7 +230,7 @@
 		}
 		
 		node = next;
-		moonfish_node_chess(node, chess);
+		moonfish_play(chess, &node->move);
 	}
 }
 
@@ -280,22 +260,22 @@
 	int i, j;
 	int bound;
 	
-	i = 1;
+	i = 0;
 	while (node != NULL) {
 		bound = 0;
 		for (j = 0 ; j < node->count ; j++) {
-			if (1 - node->children[j].bounds[1 - i] > bound) {
-				bound = 1 - node->children[j].bounds[1 - i];
+			if (1 - node->children[j].bounds[i] > bound) {
+				bound = 1 - node->children[j].bounds[i];
 			}
 		}
 		for (j = 0 ; j < node->count ; j++) {
-			if (1 - node->children[j].bounds[1 - i] < bound) {
+			if (1 - node->children[j].bounds[i] < bound) {
 				node->children[j].ignored = 1;
 			}
 		}
+		i = 1 - i;
 		node->bounds[i] = bound;
 		node = node->parent;
-		i = 1 - i;
 	}
 }
 
@@ -392,7 +372,7 @@
 			node = root->node.children + i;
 			for (j = 0 ; j < node->count ; j++) node->children[j].parent = node;
 		}
-		moonfish_node_move(root->node.children, &root->chess, &result->move);
+		result->move = root->node.children[0].move;
 		result->score = root->node.score;
 		result->node_count = root->node.visits;
 		result->time = moonfish_clock() - time0;
@@ -421,7 +401,7 @@
 	
 	for (i = 0 ; i < root->node.count ; i++) {
 		chess0 = root->chess;
-		moonfish_node_chess(&children[i], &chess0);
+		moonfish_play(&chess0, &children[i].move);
 		if (moonfish_equal(&chess0, chess)) break;
 	}
 	
@@ -504,7 +484,7 @@
 	node = root->node.children + i;
 	chess = root->chess;
 	
-	moonfish_node_move(node, &chess, &result->move);
+	result->move = node->move;
 	result->score = -node->score;
 	result->node_count = node->visits;
 	
@@ -515,8 +495,8 @@
 			break;
 		}
 		
-		moonfish_node_move(node, &chess, moves + j);
-		moonfish_play(&chess, moves + j);
+		moves[j] = node->move;
+		moonfish_play(&chess, &node->move);
 		
 		best_score = INT_MAX;
 		best_node = NULL;
--