shithub: moonfish

Download patch

ref: a03b6067b6a3064f49b7f99b60bec83129fb4884
parent: bb6c11a2064f2ee8d5853b7402bd5b65a7966f61
author: zamfofex <zamfofex@twdb.moe>
date: Wed Oct 9 00:37:38 EDT 2024

refactor and fix analysis TUI

--- a/tools/analyse.c
+++ b/tools/analyse.c
@@ -58,15 +58,15 @@
 	if (piece == moonfish_empty)
 	{
 		printf("  ");
-		return;	
+		return;
 	}
 	
-	if (piece >> 4 == 1)
+	if (piece / 16 == 1)
 		printf("\x1B[38;5;253m");
 	else
 		printf("\x1B[38;5;240m");
 	
-	switch (piece & 0xF)
+	switch (piece % 16)
 	{
 	case 1:
 		printf("\xE2\x99\x9F ");
@@ -140,22 +140,30 @@
 	draw = 16 - white - black;
 	
 	while (black > 1)
-		printf("\x1B[48;5;240m \x1B[0m\x1B[B\x08"),
+	{
+		printf("\x1B[48;5;240m \x1B[0m\x1B[B\x08");
 		black -= 2;
+	}
 	
 	if (black)
 	{
 		if (draw)
-			printf("\x1B[38;5;67m\x1B[48;5;240m\xE2\x96\x84\x1B[0m\x1B[B\x08"),
+		{
+			printf("\x1B[38;5;67m\x1B[48;5;240m\xE2\x96\x84\x1B[0m\x1B[B\x08");
 			draw--;
+		}
 		else
-			printf("\x1B[38;5;253m\x1B[48;5;240m\xE2\x96\x84\x1B[0m\x1B[B\x08"),
+		{
+			printf("\x1B[38;5;253m\x1B[48;5;240m\xE2\x96\x84\x1B[0m\x1B[B\x08");
 			white--;
+		}
 	}
 	
 	while (draw > 1)
-		printf("\x1B[48;5;67m \x1B[0m\x1B[B\x08"),
+	{
+		printf("\x1B[48;5;67m \x1B[0m\x1B[B\x08");
 		draw -= 2;
+	}
 	
 	if (draw)
 	{
@@ -210,7 +218,7 @@
 	else if (i > 0)
 	{
 		score = ply->score + fancy->plies[i - 1].score;
-		if (fancy->plies[i - 1].checkmate || score > 200) printf("\x1B[38;5;124m?? ");
+		if (fancy->plies[i - 1].checkmate != 0 || score > 200) printf("\x1B[38;5;124m?? ");
 		else if (score > 100) printf("\x1B[38;5;173m?  ");
 		else printf("   ");
 	}
@@ -660,8 +668,50 @@
 		if (command_count <= 0) moonfish_usage(args, format, argv[0]);
 	}
 	
-	/* set up terminal for displaying the user interface */
+	/* initialise data structures */
 	
+	fancy = malloc(sizeof *fancy);
+	if (fancy == NULL)
+	{
+		perror(argv[0]);
+		return 1;
+	}
+	
+	fancy->argv0 = argv[0];
+	fancy->mutex = &mutex;
+	fancy->offset = 0;
+	fancy->pv[0] = 0;
+	fancy->idle = 1;
+	fancy->stop = 0;
+	
+	fancy->x = 0;
+	fancy->y = 0;
+	
+	fancy->i = 0;
+	fancy->count = 1;
+	
+	strcpy(fancy->plies[0].san, "...");
+	
+	fancy->plies[0].white = 0;
+	fancy->plies[0].black = 0;
+	fancy->plies[0].draw = 0;
+	fancy->plies[0].checkmate = 0;
+	fancy->plies[0].depth = 0;
+	fancy->plies[0].score = 0;
+	
+	moonfish_chess(&fancy->plies[0].chess);
+	if (args[0].value == NULL)
+	{
+		fancy->fen = NULL;
+	}
+	else
+	{
+		fancy->fen = args[0].value;
+		moonfish_from_fen(&fancy->plies[0].chess, fancy->fen);
+	}
+	
+	/* configure the terminal for displaying the user interface */
+	
 	if (tcgetattr(0, &moonfish_termios))
 	{
 		perror(argv[0]);
@@ -711,52 +761,10 @@
 	printf("\x1B[?1000h");
 	fflush(stdout);
 	
-	/* initialise data structures */
+	/* begin setting up UCI bot */
 	
-	fancy = malloc(sizeof *fancy);
-	if (fancy == NULL)
-	{
-		perror(argv[0]);
-		return 1;
-	}
-	
-	fancy->argv0 = argv[0];
-	fancy->mutex = &mutex;
-	fancy->offset = 0;
-	fancy->pv[0] = 0;
-	fancy->idle = 1;
-	fancy->stop = 0;
-	
-	fancy->x = 0;
-	fancy->y = 0;
-	
 	moonfish_spawn(command, &fancy->in, &fancy->out, NULL);
 	
-	fancy->i = 0;
-	fancy->count = 1;
-	
-	strcpy(fancy->plies[0].san, "...");
-	
-	fancy->plies[0].white = 0;
-	fancy->plies[0].black = 0;
-	fancy->plies[0].draw = 0;
-	fancy->plies[0].checkmate = 0;
-	fancy->plies[0].depth = 0;
-	fancy->plies[0].score = 0;
-	
-	moonfish_chess(&fancy->plies[0].chess);
-	if (args[0].value == NULL)
-	{
-		fancy->fen = NULL;
-	}
-	else
-	{
-		fancy->fen = args[0].value;
-		moonfish_from_fen(&fancy->plies[0].chess, fancy->fen);
-	}
-	
-	/* begin setting up UCI bot */
-	
 	fprintf(fancy->in, "uci\n");
 	moonfish_wait(fancy->out, "uciok");
 	
@@ -943,7 +951,7 @@
 			continue;
 		}
 		
-		/* only handle cases where there a square selected henceforth */
+		/* only handle cases where a square is selected henceforth */
 		if (fancy->x == 0)
 		{
 			pthread_mutex_unlock(fancy->mutex);
--