src/chess/rules.c

changeset 115
206201d544be
parent 110
e2bb03494d46
--- a/src/chess/rules.c	Mon Apr 27 18:06:00 2026 +0200
+++ b/src/chess/rules.c	Tue Apr 28 12:25:48 2026 +0200
@@ -633,7 +633,7 @@
     }
 }
 
-int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color) {
+static int eval_move1(char *mstr, Move *move, uint8_t color) {
     memset(move, 0, sizeof(Move));
     move->fromfile = POS_UNSPECIFIED;
     move->fromrow = POS_UNSPECIFIED;
@@ -745,23 +745,41 @@
     }
 
     
-    if (move->piece) {
-        if (move->piece == PAWN
-            && move->torow == (color==WHITE?7:0)
-            && !move->promotion) {
-            return NEED_PROMOTION;
-        }
-        
-        move->piece |= color;
+    if (!move->piece) {
+        return INVALID_MOVE_SYNTAX;
+    }
+
+    if (move->piece == PAWN
+        && move->torow == (color==WHITE?7:0)
+        && !move->promotion) {
+        return NEED_PROMOTION;
+    }
+
+    move->piece |= color;
+
+    if (!chkidx_to(move)) {
+        return INVALID_POSITION;
+    }
+
+    return VALID_MOVE_SYNTAX;
+}
+
+int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color) {
+    int result = eval_move1(mstr, move, color);
+    if (result == VALID_MOVE_SYNTAX) {
         if (move->fromfile == POS_UNSPECIFIED
             || move->fromrow == POS_UNSPECIFIED) {
             return getlocation(gamestate, move);
-        } else {
-            return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
+        } else if (!chkidx_from(move)) {
+            return INVALID_POSITION;
         }
-    } else {
-        return INVALID_MOVE_SYNTAX;
     }
+    return result;
+}
+
+int check_move(char *mstr, uint8_t color) {
+    Move move;
+    return eval_move1(mstr, &move, color);
 }
 
 bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,

mercurial