shithub: moonfish

Download patch

ref: 87b8953e39b313610e4bf8ec5d86d97accbdadba
parent: 17f40e1f4a9a191984b24ead216309f0bd890ecf
author: zamfofex <zamfofex@twdb.moe>
date: Sat Oct 26 09:13:22 EDT 2024

refactor 'main.c' slightly

--- a/main.c
+++ b/main.c
@@ -8,21 +8,145 @@
 
 #include "moonfish.h"
 
-int main(int argc, char **argv)
+static void moonfish_go(struct moonfish_chess *chess)
 {
-	static char line[2048];
+	static struct moonfish_move move;
+	static struct moonfish_options options;
 	
-	char *arg, *arg0;
-	struct moonfish_move move;
-	char name[6];
 	long int our_time, their_time, *xtime, time;
-	struct moonfish_chess chess;
-	char *end;
+	char *arg, *end;
 	long int count;
-	struct moonfish_options options;
+	char name[6];
 	
+	our_time = -1;
+	their_time = -1;
+	time = -1;
+	
+	for (;;) {
+		
+		arg = strtok(NULL, "\r\n\t ");
+		if (arg == NULL) break;
+		
+		if (!strcmp(arg, "wtime") || !strcmp(arg, "btime")) {
+			
+			if (chess->white) {
+				if (!strcmp(arg, "wtime")) xtime = &our_time;
+				else xtime = &their_time;
+			}
+			else {
+				if (!strcmp(arg, "wtime")) xtime = &their_time;
+				else xtime = &our_time;
+			}
+			
+			arg = strtok(NULL, "\r\n\t ");
+			if (arg == NULL) {
+				fprintf(stderr, "missing time in 'go' command\n");
+				exit(1);
+			}
+			
+			errno = 0;
+			*xtime = strtol(arg, &end, 10);
+			if (errno || *end != 0 || *xtime < 0) {
+				fprintf(stderr, "malformed time in 'go' command\n");
+				exit(1);
+			}
+			
+			continue;
+		}
+		
+		if (!strcmp(arg, "movetime")) {
+			
+			arg = strtok(NULL, "\r\n\t ");
+			if (arg == NULL) {
+				fprintf(stderr, "malformed 'go movetime' command\n");
+				exit(1);
+			}
+			
+			errno = 0;
+			time = strtol(arg, &end, 10);
+			if (errno || *end != 0 || time < 0) {
+				fprintf(stderr, "malformed 'movetime' in 'go' command\n");
+				exit(1);
+			}
+			
+			continue;
+		}
+	}
+	
+	options.max_time = time;
+	options.our_time = our_time;
+	count = moonfish_best_move(chess, &move, &options);
+	moonfish_to_uci(chess, &move, name);
+	
+	printf("info nodes %ld\n", count);
+	printf("bestmove %s\n", name);
+}
+
+static void moonfish_position(struct moonfish_chess *chess)
+{
+	static struct moonfish_move move;
+	static char line[2048];
+	
+	char *arg;
+	
+	arg = strtok(NULL, "\r\n\t ");
+	if (arg == NULL) {
+		fprintf(stderr, "incomplete 'position' command\n");
+		exit(1);
+	}
+	
+	if (!strcmp(arg, "fen")) {
+		
+		arg = strtok(NULL, "\r\n");
+		if (arg == NULL) {
+			fprintf(stderr, "incomplete 'position fen' command\n");
+			exit(1);
+		}
+		
+		moonfish_from_fen(chess, arg);
+		
+		arg = strstr(arg, "moves");
+		if (arg == NULL) return;
+		
+		do arg--;
+		while (*arg == '\t' || *arg == ' ');
+		
+		strcpy(line, arg);
+		strtok(line, "\r\n\t ");
+	}
+	else {
+		if (!strcmp(arg, "startpos")) {
+			moonfish_chess(chess);
+		}
+		else {
+			fprintf(stderr, "malformed 'position' command\n");
+			exit(1);
+		}
+	}
+	
+	arg = strtok(NULL, "\r\n\t ");
+	if (arg == NULL || strcmp(arg, "moves")) return;
+	
+	for (;;) {
+		arg = strtok(NULL, "\r\n\t ");
+		if (arg == NULL) break;
+		if (moonfish_from_uci(chess, &move, arg)) {
+			fprintf(stderr, "malformed move '%s'\n", arg);
+			exit(1);
+		}
+		*chess = move.chess;
+	}
+}
+
+int main(int argc, char **argv)
+{
+	static char line[2048];
+	static struct moonfish_chess chess;
+	
+	char *arg;
+	
 	if (argc > 1) {
-		fprintf(stderr, "usage: %s\n", argv[0]);
+		fprintf(stderr, "usage: %s (no arguments)\n", argv[0]);
 		return 1;
 	}
 	
@@ -32,13 +156,10 @@
 		
 		fflush(stdout);
 		
-		errno = 0;
 		if (fgets(line, sizeof line, stdin) == NULL) {
-			if (errno) {
-				perror(argv[0]);
-				return 1;
-			}
-			break;
+			if (feof(stdin)) break;
+			perror("fgets");
+			return 1;
 		}
 		
 		arg = strtok(line, "\r\n\t ");
@@ -45,71 +166,7 @@
 		if (arg == NULL) continue;
 		
 		if (!strcmp(arg, "go")) {
-			
-			our_time = -1;
-			their_time = -1;
-			time = -1;
-			
-			for (;;) {
-				
-				arg0 = strtok(NULL, "\r\n\t ");
-				if (arg0 == NULL) break;
-				
-				if (!strcmp(arg0, "wtime") || !strcmp(arg0, "btime")) {
-					
-					if (chess.white) {
-						if (!strcmp(arg0, "wtime")) xtime = &our_time;
-						else xtime = &their_time;
-					}
-					else {
-						if (!strcmp(arg0, "wtime")) xtime = &their_time;
-						else xtime = &our_time;
-					}
-					
-					arg = strtok(NULL, "\r\n\t ");
-					if (arg == NULL) {
-						fprintf(stderr, "%s: malformed 'go' command\n", argv[0]);
-						return 1;
-					}
-					
-					errno = 0;
-					*xtime = strtol(arg, &end, 10);
-					if (errno || *end != 0 || *xtime < 0) {
-						fprintf(stderr, "%s: malformed time in 'go' command\n", argv[0]);
-						return 1;
-					}
-				}
-				
-				if (!strcmp(arg0, "movetime")) {
-					
-					arg = strtok(NULL, "\r\n\t ");
-					
-					if (arg == NULL) {
-						fprintf(stderr, "%s: malformed 'go movetime' command\n", argv[0]);
-						return 1;
-					}
-					
-					errno = 0;
-					time = strtol(arg, &end, 10);
-					if (errno || *end != 0 || time < 0) {
-						fprintf(stderr, "%s: malformed move time in 'go' command\n", argv[0]);
-						return 1;
-					}
-				}
-			}
-			
-			if (our_time < 0 && time < 0) {
-				fprintf(stderr, "%s: warning: missing constraints in 'go' command\n", argv[0]);
-			}
-			
-			options.max_time = time;
-			options.our_time = our_time;
-			count = moonfish_best_move(&chess, &move, &options);
-			
-			printf("info nodes %ld\n", count);
-			
-			moonfish_to_uci(&chess, &move, name);
-			printf("bestmove %s\n", name);
+			moonfish_go(&chess);
 			continue;
 		}
 		
@@ -116,54 +173,7 @@
 		if (!strcmp(arg, "quit")) break;
 		
 		if (!strcmp(arg, "position")) {
-			
-			arg = strtok(NULL, "\r\n\t ");
-			if (arg == NULL) {
-				fprintf(stderr, "%s: incomplete 'position' command\n", argv[0]);
-				return 1;
-			}
-			
-			if (!strcmp(arg, "fen")) {
-				
-				arg = strtok(NULL, "\r\n");
-				if (arg == NULL) {
-					fprintf(stderr, "%s: incomplete 'position fen' command\n", argv[0]);
-					return 1;
-				}
-				moonfish_from_fen(&chess, arg);
-				
-				arg = strstr(arg, "moves");
-				if (arg != NULL) {
-					do arg--;
-					while (*arg == '\t' || *arg == ' ') ;
-					strcpy(line, arg);
-					strtok(line, "\r\n\t ");
-				}
-				else {
-					strtok("", "\r\n\t ");
-				}
-			}
-			else {
-				if (!strcmp(arg, "startpos")) {
-					moonfish_chess(&chess);
-				}
-				else {
-					fprintf(stderr, "%s: malformed 'position' command\n", argv[0]);
-					return 1;
-				}
-			}
-			
-			arg = strtok(NULL, "\r\n\t ");
-			if (arg != NULL && !strcmp(arg, "moves")) {
-				while ((arg = strtok(NULL, "\r\n\t ")) != NULL) {
-					if (moonfish_from_uci(&chess, &move, arg)) {
-						fprintf(stderr, "%s: invalid move '%s'\n", argv[0], arg);
-						return 1;
-					}
-					chess = move.chess;
-				}
-			}
-			
+			moonfish_position(&chess);
 			continue;
 		}
 		
@@ -181,7 +191,7 @@
 		
 		if (!strcmp(arg, "debug") || !strcmp(arg, "setoption") || !strcmp(arg, "ucinewgame") || !strcmp(arg, "stop")) continue;
 		
-		fprintf(stderr, "%s: unknown command '%s'\n", argv[0], arg);
+		fprintf(stderr, "warning: unknown command '%s'\n", arg);
 	}
 	
 	return 0;
--