src/chess/rules.c

changeset 216
d0c3d3016650
parent 201
b61c2b90f19c
--- a/src/chess/rules.c	Thu Sep 03 15:28:54 2026 +0200
+++ b/src/chess/rules.c	Thu Sep 03 16:18:24 2026 +0200
@@ -346,14 +346,48 @@
     }
     
     /* castling */
+    bool castling_happend = false;
     if (piece_type(move->piece) == KING && move->fromfile == fileidx('e')) {
         const Color color = piece_color(move->piece);
         if (move->tofile == fileidx('g')) {
             gamestate->board[move->torank][fileidx('h')] = 0;
             gamestate->board[move->torank][fileidx('f')] = mkpiece(ROOK, color);
+            castling_happend = true;
         } else if (move->tofile == fileidx('c')) {
             gamestate->board[move->torank][fileidx('a')] = 0;
             gamestate->board[move->torank][fileidx('d')] = mkpiece(ROOK, color);
+            castling_happend = true;
+        }
+    }
+    if (castling_happend) {
+        if (piece_color(move->piece) == WHITE) {
+            gamestate->castling.K = true;
+            gamestate->castling.Q = true;
+        } else {
+            gamestate->castling.k = true;
+            gamestate->castling.q = true;
+        }
+    } else {
+        if (piece_color(move->piece) == WHITE) {
+            if (move->fromrank == rankidx('1')) {
+                if (move->fromfile == fileidx('e')) {
+                    gamestate->castling.K = gamestate->castling.Q = true;
+                } else if (move->fromfile == fileidx('h')) {
+                    gamestate->castling.K = true;
+                } else if (move->fromfile == fileidx('a')) {
+                    gamestate->castling.Q = true;
+                }
+            }
+        } else {
+            if (move->fromrank == rankidx('8')) {
+                if (move->fromfile == fileidx('e')) {
+                    gamestate->castling.k = gamestate->castling.q = true;
+                } else if (move->fromfile == fileidx('h')) {
+                    gamestate->castling.k = true;
+                } else if (move->fromfile == fileidx('a')) {
+                    gamestate->castling.q = true;
+                }
+            }
         }
     }
     
@@ -941,11 +975,7 @@
 static int getlocation(const GameState *gamestate, Move *move) {
 
     Color color = piece_color(move->piece);
-    bool incheck = false;
-    if (gamestate->movecount > 0) {
-        incheck = is_check_position(gamestate);
-    }
-    
+
     Move candidates[16], *candidate = NULL;
     size_t candidatecount;
 
@@ -983,7 +1013,7 @@
             if (found) {
                 if (piece_type(move->piece) == KING) {
                     return KING_MOVES_INTO_CHECK;
-                } else if (incheck) {
+                } else if (is_check_position(gamestate)) {
                     return KING_IN_CHECK;
                 } else {
                     return PIECE_PINNED;

mercurial