shithub: moonfish

Download patch

ref: 51e92acb3c80242155d047b42c72a1e83cbd150d
parent: e70fe4956e6b622a15d362e3b05c788057015b8c
author: zamfofex <zamfofex@twdb.moe>
date: Tue Dec 19 11:22:51 EST 2023

undo stalemate and threefold repetition consideration

The code was kinda awkward and hacky, and it only worked sometimes.

--- a/README.md
+++ b/README.md
@@ -31,11 +31,11 @@
   - no en passant
   - no underpromotion
 - the TUI will also prevent you from making those kinds of moves
+- the TUI does not detect when the game has ended due to stalemate or checkmate
 - no transposition table
 - no good move ordering heuristic
 - no support for `go infinite` or `go mate`
 - no move name or FEN validation (may lead to potential exploits)
-- games with too many moves may cause a buffer overflow
 
 download
 ---
--- a/chess.c
+++ b/chess.c
@@ -589,103 +589,3 @@
 	
 	return valid ^ 1;
 }
-
-int moonfish_finished(struct moonfish_chess *chess)
-{
-	int x, y;
-	struct moonfish_move moves[32];
-	struct moonfish_move *move;
-	int valid;
-	
-	for (y = 0 ; y < 8 ; y++)
-	for (x = 0 ; x < 8 ; x++)
-	{
-		moonfish_moves(chess, moves, (x + 1) + (y + 2) * 10);
-		for (move = moves ; move->piece != moonfish_outside ; move++)
-		{
-			moonfish_play(chess, move);
-			valid = moonfish_validate(chess);
-			moonfish_unplay(chess, move);
-			if (valid) return 0;
-		}
-	}
-	
-	return 1;
-}
-
-int moonfish_checkmate(struct moonfish_chess *chess)
-{
-	if (!moonfish_finished(chess)) return 0;
-	return moonfish_check(chess);
-}
-
-int moonfish_stalemate(struct moonfish_chess *chess)
-{
-	if (!moonfish_finished(chess)) return 0;
-	return moonfish_check(chess) ^ 1;
-}
-
-static int moonfish_equals(struct moonfish_chess *a, struct moonfish_chess *b)
-{
-	int x, y;
-	
-	if (a->white != b->white) return 0;
-	if (a->score != b->score) return 0;
-	
-	if (a->castle.white_oo != b->castle.white_oo) return 0;
-	if (a->castle.white_ooo != b->castle.white_ooo) return 0;
-	if (a->castle.black_oo != b->castle.black_oo) return 0;
-	if (a->castle.black_ooo != b->castle.black_ooo) return 0;
-	
-	for (y = 0 ; y < 8 ; y++)
-	for (x = 0 ; x < 8 ; x++)
-		if (a->board[(x + 1) + (y + 2) * 10] != b->board[(x + 1) + (y + 2) * 10])
-			return 0;
-	
-	return 1;
-}
-
-void moonfish_repetition_account(struct moonfish_repetition *repetition, struct moonfish_chess *chess, int count)
-{
-	struct moonfish_repetition_entry *entry;
-	
-	entry = repetition->entries;
-	
-	for (;;)
-	{
-		if (entry->count == -1) break;
-		if (moonfish_equals(&entry++->chess, chess)) break;
-	}
-	
-	if (entry->count == -1)
-	{
-		entry->chess = *chess;
-		entry->count = 0;
-		entry[1].count = -1;
-	}
-	
-	entry->count += count;
-	
-	if (entry->count != 0) return;
-	
-	for (;;)
-	{
-		entry[0] = entry[1];
-		entry++;
-		if (entry->count == -1) break;
-	}
-}
-
-int moonfish_repetition(struct moonfish_repetition *repetition)
-{
-	struct moonfish_repetition_entry *entry;
-	
-	entry = repetition->entries;
-	
-	for (;;)
-	{
-		if (entry->count == -1) return 0;
-		if (entry->count >= 3) return 1;
-		entry++;
-	}
-}
--- a/main.c
+++ b/main.c
@@ -33,7 +33,6 @@
 	ctx->argv0 = argv[0];
 	ctx->cpu_count = -1;
 	moonfish_chess(&ctx->chess);
-	ctx->repetition.entries[0].count = -1;
 	
 	for (;;)
 	{
@@ -132,8 +131,6 @@
 				return 1;
 			}
 			
-			moonfish_repetition_account(&ctx->repetition, &ctx->chess, 1);
-			
 			arg = strtok(NULL, "\r\n\t ");
 			if (arg != NULL && !strcmp(arg, "moves"))
 			{
@@ -141,7 +138,6 @@
 				{
 					moonfish_from_uci(&ctx->chess, &move, arg);
 					moonfish_play(&ctx->chess, &move);
-					moonfish_repetition_account(&ctx->repetition, &ctx->chess, 1);
 				}
 			}
 		}
--- a/minify.sh
+++ b/minify.sh
@@ -60,8 +60,6 @@
 sed '/^[^"'"'"']/s/\(\b\|_\)empty\(\b\|_\)/\1E\2/g' |
 sed '/^[^"'"'"']/s/\(\b\|_\)type\(\b\|_\)/\1T\2/g' |
 sed '/^[^"'"'"']/s/\(\b\|_\)outside\(\b\|_\)/\1J\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)repetition\(\b\|_\)/\1V\2/g' |
-sed '/^[^"'"'"']/s/\(\b\|_\)account\(\b\|_\)/\1I\2/g' |
 
 # remove inserted line breaks
 tr -d '\n' |
--- a/moonfish.h
+++ b/moonfish.h
@@ -47,21 +47,9 @@
 	int score;
 };
 
-struct moonfish_repetition_entry
-{
-	struct moonfish_chess chess;
-	int count;
-};
-
-struct moonfish_repetition
-{
-	struct moonfish_repetition_entry entries[512];
-};
-
 struct moonfish
 {
 	struct moonfish_chess chess;
-	struct moonfish_repetition repetition;
 	char *argv0;
 	int cpu_count;
 };
@@ -91,11 +79,5 @@
 
 int moonfish_validate(struct moonfish_chess *chess);
 int moonfish_check(struct moonfish_chess *chess);
-int moonfish_checkmate(struct moonfish_chess *chess);
-int moonfish_stalemate(struct moonfish_chess *chess);
-int moonfish_finished(struct moonfish_chess *chess);
-
-void moonfish_repetition_account(struct moonfish_repetition *repetition, struct moonfish_chess *chess, int count);
-int moonfish_repetition(struct moonfish_repetition *repetition);
 
 #endif
--- a/search.c
+++ b/search.c
@@ -84,7 +84,6 @@
 	int i, j;
 	struct moonfish_search_info infos[32];
 	int result;
-	int repetition;
 #ifdef __MINGW32__
 	SYSTEM_INFO info;
 #endif
@@ -110,8 +109,6 @@
 	moves = move_array;
 	best_score = -200 * moonfish_omega;
 	
-	repetition = moonfish_repetition(&ctx->repetition) * 2;
-	
 	for (y = 0 ; y < 8 ; y++)
 	for (x = 0 ; x < 8 ; x++)
 	{
@@ -153,31 +150,6 @@
 		}
 		
 		moonfish_play(&ctx->chess, moves);
-		
-		if (moonfish_checkmate(&ctx->chess))
-		{
-			*best_move = *moves;
-			moonfish_unplay(&ctx->chess, moves);
-			return -200 * moonfish_omega;
-		}
-		
-		if (repetition != 2)
-		{
-			moonfish_repetition_account(&ctx->repetition, &ctx->chess, 1);
-			repetition = moonfish_repetition(&ctx->repetition);
-			moonfish_repetition_account(&ctx->repetition, &ctx->chess, -1);
-		}
-		
-		if (moonfish_stalemate(&ctx->chess) || repetition == 1)
-		{
-			if (best_score < 0)
-			{
-				*best_move = *moves;
-				best_score = 0;
-			}
-			moonfish_unplay(&ctx->chess, moves++);
-			continue;
-		}
 		
 		if (!moonfish_validate(&ctx->chess))
 		{
--- a/tools/play.c
+++ b/tools/play.c
@@ -572,7 +572,6 @@
 				moonfish_play(&fancy->chess, &move);
 				moonfish_reset_time(fancy);
 				moonfish_fancy(fancy);
-				if (moonfish_finished(&fancy->chess)) break;
 				
 				pthread_mutex_unlock(fancy->mutex);
 				
@@ -584,8 +583,6 @@
 				name += strlen(name);
 				moonfish_reset_time(fancy);
 				moonfish_fancy(fancy);
-				if (moonfish_finished(&fancy->chess)) break;
-				
 				pthread_mutex_unlock(fancy->mutex);
 				
 				printf("\x1B[?1000h");
--