src/chess/king.c

changeset 161
3ff96fec144a
parent 98
9cb41383540f
child 163
2a6d83f4677e
--- a/src/chess/king.c	Wed Jul 29 13:45:51 2026 +0200
+++ b/src/chess/king.c	Thu Jul 30 11:49:47 2026 +0200
@@ -29,11 +29,10 @@
 
 #include "rules.h"
 #include "king.h"
-#include <stdlib.h>
 
-static bool king_castling_chkmoved(GameState *gamestate,
-    uint8_t row, uint8_t file) {
+#include <string.h>
 
+static bool king_castling_chkmoved(GameState *gamestate, Row row, File file) {
     for (unsigned i = 0; i < gamestate->movecount; i++) {
         if (gamestate->moves[i].fromfile == file
             && gamestate->moves[i].fromrow == row) {
@@ -51,7 +50,7 @@
     } else {
         /* castling */
         if (move->fromrow == move->torow &&
-            move->fromrow == ((move->piece&COLOR_MASK) == WHITE ? 0 : 7) &&
+            move->fromrow == (piece_color(move->piece) == WHITE ? 0 : 7) &&
             move->fromfile == fileidx('e') &&
             (move->tofile == fileidx('c') || move->tofile == fileidx('g'))) {
             
@@ -67,7 +66,7 @@
 
 bool king_isblocked(GameState *gamestate, Move *move) {
     
-    uint8_t opponent_color = opponent_color(move->piece&COLOR_MASK);
+    uint8_t opponent_color = opponent_color(piece_color(move->piece));
     
     /* being in check does not "block" the king, so don't test it here */
     bool blocked = false;
@@ -78,8 +77,11 @@
             blocked |= gamestate->board[move->torow][fileidx('b')];
         }
         uint8_t midfile = (move->tofile+move->fromfile)/2;
-        blocked |= last_move(gamestate).check ||
-            gamestate->board[move->torow][midfile] ||
+        bool incheck = false;
+        if (gamestate->movecount > 0) {
+            incheck = gamestate->moves[gamestate->movecount-1].check;
+        }
+        blocked |= incheck || gamestate->board[move->torow][midfile] ||
             is_covered(gamestate, move->torow, midfile, opponent_color);
     }
     

mercurial