fix regression: pawns cannot block check threats

Sat, 01 Aug 2026 11:26:48 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 01 Aug 2026 11:26:48 +0200
changeset 166
1a9b662bc201
parent 165
3e27c99ed721
child 167
fd1d3f0a7a73

fix regression: pawns cannot block check threats

relates to #960

src/chess/rules.c file | annotate | diff | comparison | revisions
src/chess/rules.h file | annotate | diff | comparison | revisions
--- a/src/chess/rules.c	Sat Aug 01 10:58:39 2026 +0200
+++ b/src/chess/rules.c	Sat Aug 01 11:26:48 2026 +0200
@@ -1040,18 +1040,20 @@
 }
 
 bool is_protected(const GameState *gamestate, Row row, File file, Color color) {
-    Move threats[16];
-    size_t threatcount;
-    if (get_real_threats(gamestate, row, file, color, threats, &threatcount)) {
-        for (size_t i = 0 ; i < threatcount ; i++) {
-            if (threats[i].piece != (color|KING)) {
-                return true;
-            }
+    Move candidates[16];
+    size_t ccount;
+    /* we need all candidates - not only threats! */
+    if (get_candidates(gamestate, row, file, color, candidates, &ccount)) {
+        for (size_t i = 0 ; i < ccount ; i++) {
+            /* skip the king */
+            if (piece_type(candidates[i].piece) == KING) continue;
+            /* skip pinned pieces */
+            if (is_pinned(gamestate, &candidates[i])) continue;
+            /* found one */
+            return true;
         }
-        return false;
-    } else {
-        return false;
     }
+    return false;
 }
 
 uint16_t remaining_movetime(const GameState *gamestate, Color color) {
--- a/src/chess/rules.h	Sat Aug 01 10:58:39 2026 +0200
+++ b/src/chess/rules.h	Sat Aug 01 11:26:48 2026 +0200
@@ -383,15 +383,15 @@
 /**
  * Checks, if a specified field is protected by a piece of a certain color.
  * 
- * This is the same as is_attacked(), but only considers pieces that are not
- * the king.
+ * A field is protected, if any piece except the king can either capture on
+ * that field or move to that field (and is not pinned).
  * 
  * @param gamestate the current game state
  * @param row row of the field to check
  * @param file file of the field to check
  * @param color the color of the piece that should cover the field
  * @return true, if any piece (excluding the king) of the specified color
- * threatens the specified field and could capture an opponent piece
+ * can move to the specified field (including capturing moves)
  */
 bool is_protected(const GameState *gamestate, Row row, File file, Color color);
 

mercurial