shithub: moonfish

Download patch

ref: ed289f774f665abf31a6c8c369a03d65c06c2aae
parent: 9977a83d0f0df153f8c21d84fae7653de01caf65
author: zamfofex <zamfofex@twdb.moe>
date: Wed Oct 11 08:23:17 EDT 2023

style changes, remove nonstandard commands, decrease search depth

--- a/chess.c
+++ b/chess.c
@@ -1,54 +1,55 @@
 #include "moonfish.h"
 
-static char moonfish_delta(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from, unsigned char *to, signed char delta)
+static struct moonfish_move *moonfish_create_move(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from, unsigned char to)
 {
-	*to += delta;
-	
-	if (ctx->board[*to] <= moonfish_our_king) return 0;
-	
 	(*moves)->from = from;
-	(*moves)->to = *to;
+	(*moves)->to = to;
 	(*moves)->piece = ctx->board[from];
 	(*moves)->promotion = ctx->board[from];
-	(*moves)->captured = ctx->board[*to];
+	(*moves)->captured = ctx->board[to];
 	(*moves)->castle = ctx->castle;
-	(*moves)++;
-	
+	return (*moves)++;
+}
+
+static char moonfish_delta(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from, unsigned char *to, signed char delta)
+{
+	*to += delta;
+	if (ctx->board[*to] <= moonfish_our_king) return 0;
+	moonfish_create_move(ctx, moves, from, *to);
 	if (ctx->board[*to] != moonfish_empty) return 0;
-	
 	return 1;
 }
 
 static void moonfish_slide(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from, signed char delta)
 {
-	unsigned char to = from;
+	unsigned char to;
+	to = from;
 	while (moonfish_delta(ctx, moves, from, &to, delta)) { }
 }
 
 static void moonfish_jump(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from, signed char delta)
 {
-	unsigned char to = from;
-	moonfish_delta(ctx, moves, from, &to, delta);
+	moonfish_delta(ctx, moves, from, &from, delta);
 }
 
 static void moonfish_move_knight(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
 {
-	moonfish_jump(ctx, moves, from, 1 + 20);
-	moonfish_jump(ctx, moves, from, -1 + 20);
-	moonfish_jump(ctx, moves, from, 1 - 20);
-	moonfish_jump(ctx, moves, from, -1 - 20);
-	moonfish_jump(ctx, moves, from, 2 + 10);
-	moonfish_jump(ctx, moves, from, -2 + 10);
-	moonfish_jump(ctx, moves, from, 2 - 10);
-	moonfish_jump(ctx, moves, from, -2 - 10);
+	moonfish_jump(ctx, moves, from, 21);
+	moonfish_jump(ctx, moves, from, 19);
+	moonfish_jump(ctx, moves, from, -19);
+	moonfish_jump(ctx, moves, from, -21);
+	moonfish_jump(ctx, moves, from, 12);
+	moonfish_jump(ctx, moves, from, 8);
+	moonfish_jump(ctx, moves, from, -8);
+	moonfish_jump(ctx, moves, from, -12);
 }
 
 static void moonfish_move_bishop(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
 {
-	moonfish_slide(ctx, moves, from, 1 + 10);
-	moonfish_slide(ctx, moves, from, -1 + 10);
-	moonfish_slide(ctx, moves, from, 1 - 10);
-	moonfish_slide(ctx, moves, from, -1 - 10);
+	moonfish_slide(ctx, moves, from, 11);
+	moonfish_slide(ctx, moves, from, 9);
+	moonfish_slide(ctx, moves, from, -9);
+	moonfish_slide(ctx, moves, from, -11);
 }
 
 static void moonfish_move_rook(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
@@ -78,7 +79,7 @@
 
 static void moonfish_castle_low(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
 {
-	int to;
+	unsigned char to;
 	
 	for (to = 22 ; to != from ; to++)
 		if (ctx->board[to] != moonfish_empty)
@@ -88,18 +89,12 @@
 	if (moonfish_attacked(ctx, from, from - 1)) return;
 	if (moonfish_attacked(ctx, from, from - 2)) return;
 	
-	(*moves)->from = from;
-	(*moves)->to = from - 2;
-	(*moves)->piece = moonfish_our_king;
-	(*moves)->promotion = moonfish_our_king;
-	(*moves)->captured = moonfish_empty;
-	(*moves)->castle = ctx->castle;
-	(*moves)++;
+	moonfish_create_move(ctx, moves, from, from - 2);
 }
 
 static void moonfish_castle_high(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
 {
-	int to;
+	unsigned char to;
 	
 	for (to = 27 ; to != from ; to--)
 		if (ctx->board[to] != moonfish_empty)
@@ -109,13 +104,7 @@
 	if (moonfish_attacked(ctx, from, from + 1)) return;
 	if (moonfish_attacked(ctx, from, from + 2)) return;
 	
-	(*moves)->from = from;
-	(*moves)->to = from + 2;
-	(*moves)->piece = moonfish_our_king;
-	(*moves)->promotion = moonfish_our_king;
-	(*moves)->captured = moonfish_empty;
-	(*moves)->castle = ctx->castle;
-	(*moves)++;
+	moonfish_create_move(ctx, moves, from, from + 2);
 }
 
 static void moonfish_move_king(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
@@ -143,6 +132,7 @@
 
 static void moonfish_move_pawn(struct moonfish *ctx, struct moonfish_move **moves, unsigned char from)
 {
+	struct moonfish_move *move;
 	unsigned char promotion;
 	
 	promotion = moonfish_our_pawn;
@@ -150,25 +140,15 @@
 	
 	if (ctx->board[from + 10] == moonfish_empty)
 	{
-		(*moves)->piece = moonfish_our_pawn;
-		(*moves)->promotion = promotion;
-		(*moves)->captured = moonfish_empty;
-		(*moves)->from = from;
-		(*moves)->to = from + 10;
-		(*moves)->castle = ctx->castle;
-		(*moves)++;
+		move = moonfish_create_move(ctx, moves, from, from + 10);
+		move->promotion = promotion;
 				
 		if (from < 40)
 		{
 			if (ctx->board[from + 20] == moonfish_empty)
 			{
-				(*moves)->from = from;
-				(*moves)->to = from + 20;
-				(*moves)->piece = moonfish_our_pawn;
-				(*moves)->promotion = moonfish_our_pawn;
-				(*moves)->captured = moonfish_empty;
-				(*moves)->castle = ctx->castle;
-				(*moves)++;
+				move = moonfish_create_move(ctx, moves, from, from + 20);
+				move->promotion = promotion;
 			}
 		}
 	}
@@ -176,31 +156,21 @@
 	if (ctx->board[from + 9] >= moonfish_their_pawn)
 	if (ctx->board[from + 9] != moonfish_empty)
 	{
-		(*moves)->piece = moonfish_our_pawn;
-		(*moves)->promotion = promotion;
-		(*moves)->captured = ctx->board[from + 9];
-		(*moves)->from = from;
-		(*moves)->to = from + 9;
-		(*moves)->castle = ctx->castle;
-		(*moves)++;
+		move = moonfish_create_move(ctx, moves, from, from + 9);
+		move->promotion = promotion;
 	}
 	
 	if (ctx->board[from + 11] >= moonfish_their_pawn)
 	if (ctx->board[from + 11] != moonfish_empty)
 	{
-		(*moves)->piece = moonfish_our_pawn;
-		(*moves)->promotion = promotion;
-		(*moves)->captured = ctx->board[from + 11];
-		(*moves)->from = from;
-		(*moves)->to = from + 11;
-		(*moves)->castle = ctx->castle;
-		(*moves)++;
+		move = moonfish_create_move(ctx, moves, from, from + 11);
+		move->promotion = promotion;
 	}
 }
 
 void moonfish_moves(struct moonfish *ctx, struct moonfish_move *moves, unsigned char from)
 {
-	char piece;
+	unsigned char piece;
 	piece = ctx->board[from];
 	if (piece == moonfish_our_pawn) moonfish_move_pawn(ctx, &moves, from);
 	if (piece == moonfish_our_knight) moonfish_move_knight(ctx, &moves, from);
@@ -347,7 +317,6 @@
 
 void moonfish_chess(struct moonfish *ctx)
 {
-	static unsigned char pieces[] = {4, 2, 3, 5, 6, 3, 2, 4};
 	int x, y;
 	
 	for (y = 0 ; y < 12 ; y++)
@@ -354,56 +323,7 @@
 	for (x = 0 ; x < 10 ; x++)
 		ctx->board[x + y * 10] = moonfish_outside;
 	
-	for (x = 0 ; x < 8 ; x++)
-	{
-		ctx->board[(x + 1) + 20] = pieces[x] | 0x10;
-		ctx->board[(x + 1) + 30] = moonfish_our_pawn;
-		ctx->board[(x + 1) + 40] = moonfish_empty;
-		ctx->board[(x + 1) + 50] = moonfish_empty;
-		ctx->board[(x + 1) + 60] = moonfish_empty;
-		ctx->board[(x + 1) + 70] = moonfish_empty;
-		ctx->board[(x + 1) + 80] = moonfish_their_pawn;
-		ctx->board[(x + 1) + 90] = pieces[x] | 0x20;
-	}
-	
-	ctx->white = 1;
-	ctx->castle.white_oo = 1;
-	ctx->castle.white_ooo = 1;
-	ctx->castle.black_oo = 1;
-	ctx->castle.black_ooo = 1;
-}
-
-void moonfish_show(struct moonfish *ctx)
-{
-	static char chs[] = "pnbrqk";
-	
-	int x, y;
-	char ch;
-	unsigned char piece;
-	
-	for (y = 7 ; y >= 0 ; y--)
-	{
-		printf("   ");
-		
-		for (x = 0 ; x < 8 ; x++)
-		{
-			piece = ctx->board[(x + 1) + (y + 2) * 10];
-			if (piece == moonfish_empty)
-			{
-				printf("  ");
-				continue;
-			}
-			
-			ch = chs[(piece & 0xF) - 1];
-			
-			if (piece <= moonfish_our_king || !ctx->white)
-			if (piece > moonfish_our_king || ctx->white)
-				ch += 'A' - 'a';
-			
-			printf("%c ", ch);
-		}
-		printf("\n");
-	}
+	moonfish_fen(ctx, "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq");
 }
 
 void moonfish_play_uci(struct moonfish *ctx, char *name)
--- a/main.c
+++ b/main.c
@@ -4,22 +4,14 @@
 
 #include "moonfish.h"
 
-static char *moonfish_strdup(char *str)
-{
-	char *result;
-	result = malloc(strlen(str) + 1);
-	strcpy(result, str);
-	return result;
-}
-
 int main(int argc, char **argv)
 {
+	static char line[2048];
 	FILE *file;
 	struct moonfish *ctx;
-	char line[512], *arg;
+	char *arg;
 	struct moonfish_move move;
 	char name[6];
-	int score;
 	
 	if (argc < 1) return 1;
 	
@@ -73,7 +65,6 @@
 	printf("   (moonfish by zamfofex)\n");
 	printf("   (inspired by sunfish by tahle)\n");
 	printf("   (simple UCI interface)\n");
-	printf("   (try typing 'help')\n");
 	
 	printf("\n");
 	printf(
@@ -105,47 +96,12 @@
 		arg = strtok(line, "\r\n\t ");
 		if (arg == NULL) continue;
 		
-		if (!strcmp(arg, "play"))
+		if (!strcmp(arg, "go"))
 		{
-			while ((arg = strtok(NULL, "\r\n\t ")) != NULL)
-				moonfish_play_uci(ctx, arg);
-		}
-		else if (!strcmp(arg, "go"))
-		{
 			moonfish_best_move(ctx, &move);
 			moonfish_to_uci(name, &move, ctx->white);
 			printf("bestmove %s\n", name);
 		}
-		else if (!strcmp(arg, "eval"))
-		{
-			score = moonfish_best_move(ctx, &move);
-			printf("score %d\n", score / 127);
-		}
-		else if (!strcmp(arg, "show"))
-		{
-			moonfish_show(ctx);
-		}
-		else if (!strcmp(arg, "restart"))
-		{
-			moonfish_chess(ctx);
-		}
-		else if (!strcmp(arg, "fen"))
-		{
-			arg = strtok(NULL, "\r\n");
-			if (arg == NULL)
-			{
-				fprintf(stderr, "malformed 'fen' command\n");
-				free(ctx);
-				return 1;
-			}
-			moonfish_fen(ctx, arg);
-		}
-		else if (!strcmp(arg, "echo"))
-		{
-			arg = strtok(NULL, "\r\n");
-			if (arg == NULL) arg = "";
-			printf("%s\n", arg);
-		}
 		else if (!strcmp(arg, "quit"))
 		{
 			break;
@@ -170,10 +126,13 @@
 				{
 					do arg--;
 					while (*arg == '\t' || *arg == ' ');
-					arg = moonfish_strdup(arg);
-					strtok(arg, "\r\n\t ");
-					free(arg);
+					strcpy(line, arg);
+					strtok(line, "\r\n\t ");
 				}
+				else
+				{
+					strtok("", "\r\n\t ");
+				}
 			}
 			else if (!strcmp(arg, "startpos"))
 			{
@@ -206,28 +165,9 @@
 		else if (!strcmp(arg, "debug") || !strcmp(arg, "setoption") || !strcmp(arg, "ucinewgame") || !strcmp(arg, "stop"))
 		{
 		}
-		else if (!strcmp(arg, "help"))
-		{
-			printf("   'show'          - shows the current position as ASCII\n");
-			printf("   'play <moves>'  - plays the given moves on the current position\n");
-			printf("   'eval'          - shows the evaluation of the current position\n");
-			printf("   'go'            - finds the best move in the current position\n");
-			printf("   'restart'       - resets the board to the initial position\n");
-			printf("   'fen <FEN>'     - sets the board to the given FEN\n");
-			printf("   'echo <message> - shows the given message (useful for scripts)\n");
-			printf("   'quit'          - self-explanatory\n");
-			printf("   for UCI compatibility:\n");
-			printf("   'debug', 'setoption' - ignored\n");
-			printf("   'ucinewgame', 'stop' - ignored\n");
-			printf("   'uci', 'isready'     - handled per UCI\n");
-			printf("   'position startpos [moves <moves>]'  - equivalent to 'restart' then 'play <moves>'\n");
-			printf("   'position fen <FEN> [moves <moves>]' - equivalent to 'fen <FEN>' then 'play <moves>'\n");
-		}
 		else
 		{
 			fprintf(stderr, "%s: unknown command '%s'\n", argv[0], arg);
-			free(ctx);
-			return 1;
 		}
 		
 		fflush(stdout);
--- a/moonfish.h
+++ b/moonfish.h
@@ -22,7 +22,7 @@
 	moonfish_outside = 0,
 	moonfish_empty = 0xFF,
 	
-	moonfish_omega = 10000000
+	moonfish_omega = 5000000
 };
 
 struct moonfish_nnue
@@ -66,8 +66,6 @@
 
 void moonfish_play(struct moonfish *ctx, struct moonfish_move *move);
 void moonfish_unplay(struct moonfish *ctx, struct moonfish_move *move);
-
-void moonfish_show(struct moonfish *ctx);
 
 int moonfish_tanh(int value);
 
--- a/search.c
+++ b/search.c
@@ -48,7 +48,7 @@
 	return score * 360 + scale * ctx->nnue.scale * 360 / 256;
 }
 
-static int moonfish_quiesce(struct moonfish *ctx, int alpha, int beta, int depth)
+static int moonfish_search(struct moonfish *ctx, int alpha, int beta, int depth)
 {
 	int x, y;
 	struct moonfish_move moves[32];
@@ -55,43 +55,15 @@
 	struct moonfish_move *move;
 	int score;
 	
-	score = moonfish_evaluate(ctx);
-	if (depth == 0) return score;
-	
-	if (score >= beta) return beta;
-	if (alpha < score) alpha = score;
-	
-	for (y = 0 ; y < 8 ; y++)
-	for (x = 0 ; x < 8 ; x++)
+	if (depth <= 0)
 	{
-		moonfish_moves(ctx, moves, (x + 1) + (y + 2) * 10);
+		score = moonfish_evaluate(ctx);
+		if (depth <= -3) return score;
 		
-		for (move = moves ; move->piece != moonfish_outside ; move++)
-		{
-			if (move->captured == moonfish_empty) continue;
-			if (move->captured == moonfish_their_king) return moonfish_omega * (4 + depth);
-			
-			moonfish_play(ctx, move);
-			score = -moonfish_quiesce(ctx, -beta, -alpha, depth - 1);
-			moonfish_unplay(ctx, move);
-			
-			if (score >= beta) return beta;
-			if (score > alpha) alpha = score;
-		}
+		if (score >= beta) return beta;
+		if (alpha < score) alpha = score;
 	}
 	
-	return alpha;
-}
-
-static int moonfish_search(struct moonfish *ctx, int alpha, int beta, int depth)
-{
-	int x, y;
-	struct moonfish_move moves[32];
-	struct moonfish_move *move;
-	int score;
-	
-	if (depth == 0) return moonfish_quiesce(ctx, alpha, beta, 2);
-	
 	for (y = 0 ; y < 8 ; y++)
 	for (x = 0 ; x < 8 ; x++)
 	{
@@ -99,7 +71,8 @@
 		
 		for (move = moves ; move->piece != moonfish_outside ; move++)
 		{
-			if (move->captured == moonfish_their_king) return moonfish_omega * depth;
+			if (depth <= 0 && move->captured == moonfish_empty) continue;
+			if (move->captured == moonfish_their_king) return moonfish_omega * (depth + 10);
 			
 			moonfish_play(ctx, move);
 			score = -moonfish_search(ctx, -beta, -alpha, depth - 1);
@@ -129,6 +102,8 @@
 		
 		for (move = moves ; move->piece != moonfish_outside ; move++)
 		{
+			char s[10];
+			
 			moonfish_play(ctx, move);
 			
 			if (!moonfish_validate(ctx))
@@ -137,8 +112,11 @@
 				continue;
 			}
 			
-			score = -moonfish_search(ctx, -10 * moonfish_omega, 10 * moonfish_omega, 4);
+			score = -moonfish_search(ctx, -10 * moonfish_omega, 10 * moonfish_omega, 3);
 			moonfish_unplay(ctx, move);
+			
+			moonfish_to_uci(s, move, ctx->white);
+			printf("%s %d\n", s, score);
 			
 			if (score > best_score)
 			{
--