ref: 5d9d88fac73e9c8281ebf01b39d13fe56376ef74
parent: d049afd87a9848868daa4c4588fd72f6e727bd39
author: zamfofex <zamfofex@twdb.moe>
date: Sat Oct 7 03:04:32 EDT 2023
prevent leaving the king in check, fix time control handling
--- a/README.md
+++ b/README.md
@@ -22,7 +22,7 @@
- no en passant
- no underpromotion
- the TUI will also prevent you from making those kinds of moves
-- the TUI allows illegal moves that leave the king in check
+- the TUI does not detect when the game has ended due to stalemate or checkmate
- no quiescence search
- no iterative deepening
- no transposition table
--- a/chess.c
+++ b/chess.c
@@ -417,3 +417,21 @@
ctx->white = 1;
if (*fen == 'b') moonfish_rotate(ctx);
}
+
+int moonfish_validate(struct moonfish *ctx)
+{+ int x, y;
+ struct moonfish_move moves[512];
+ struct moonfish_move *move;
+
+ for (y = 0 ; y < 8 ; y++)
+ for (x = 0 ; x < 8 ; x++)
+ {+ moonfish_moves(ctx, moves, (x + 1) + (y + 2) * 10);
+ for (move = moves ; move->piece != moonfish_outside ; move++)
+ if (move->captured == moonfish_their_king)
+ return 0;
+ }
+
+ return 1;
+}
--- a/moonfish.h
+++ b/moonfish.h
@@ -68,4 +68,6 @@
void moonfish_play_uci(struct moonfish *ctx, char *name);
void moonfish_to_uci(char *name, struct moonfish_move *move, int white);
+int moonfish_validate(struct moonfish *ctx);
+
#endif
--- a/tools/play.c
+++ b/tools/play.c
@@ -140,7 +140,16 @@
static void moonfish_time(int time, char *name)
{- printf(" \x1B[48;5;248m\x1B[38;5;235m %2d:%02d \x1B[0m %s\n", time / 60, time % 60, name);+ printf(" \x1B[48;5;248m\x1B[38;5;235m ");+
+ if (time < 60)
+ printf("%4ds", time);+ else if (time < 3600)
+ printf("%2d:%02d", time / 60, time % 60);+ else
+ printf("%dh %d:%02d", time / 3600, time % 3600 / 60, time % 60);+
+ printf(" \x1B[0m %s \n", name);}
static void moonfish_fancy(struct moonfish_fancy *fancy)
@@ -239,6 +248,7 @@
{struct moonfish_move moves[32];
struct moonfish_move *move;
+ int valid;
y0 = 9 - y0;
y1 = 9 - y1;
@@ -249,8 +259,15 @@
{if (move->to == x1 + (y1 + 1) * 10)
{- *found = *move;
- return 0;
+ moonfish_play(ctx, move);
+ valid = moonfish_validate(ctx);
+ moonfish_unplay(ctx, move);
+
+ if (valid)
+ {+ *found = *move;
+ return 0;
+ }
}
}
@@ -259,6 +276,7 @@
static void moonfish_reset_time(struct moonfish_fancy *fancy)
{+ struct timespec prev_timespec;
struct timespec timespec;
int *time;
long int *nano;
@@ -280,8 +298,11 @@
else time = &fancy->their_time, nano = &fancy->their_nano;
}
- timespec.tv_sec -= fancy->timespec.tv_sec;
- timespec.tv_nsec -= fancy->timespec.tv_nsec;
+ prev_timespec = fancy->timespec;
+ fancy->timespec = timespec;
+
+ timespec.tv_sec -= prev_timespec.tv_sec;
+ timespec.tv_nsec -= prev_timespec.tv_nsec;
while (timespec.tv_nsec < 0)
{--
⑨