Skip to content

Commit 98229cc

Browse files
authored
Merge pull request #67 from Belzedar94/codex/fix-legal-move-detection-for-battlekings
Fix Battlekings gating legality on captures
2 parents fdb1191 + e4d8bbb commit 98229cc

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/position.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,16 @@ bool Position::legal(Move m) const {
12741274
Bitboard occ = occupied | gating_square(m);
12751275
if (type_of(m) == EN_PASSANT)
12761276
occ ^= capture_square(to);
1277-
if (attackers_to(gating_square(m), occ, ~us))
1277+
1278+
Bitboard attackers = attackers_to(gating_square(m), occ, ~us);
1279+
1280+
// Ignore attacks from enemy pieces that will be removed as part of the move
1281+
if (capture(m) && piece_on(to) != NO_PIECE)
1282+
attackers &= ~SquareBB[to];
1283+
if (type_of(m) == EN_PASSANT)
1284+
attackers &= ~SquareBB[capture_square(to)];
1285+
1286+
if (attackers)
12781287
return false;
12791288
}
12801289

test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def test_chess_promotion_does_not_gate(self):
435435
self.assertEqual(fen, "Q7/8/8/8/8/8/7p/7K b - - 0 1")
436436

437437
def test_battlekings_king_spawn_blocked(self):
438-
fen = "8/8/8/8/8/3p4/4Q3/8 w - - 0 1"
438+
fen = "8/8/8/8/8/4r3/4Q3/4r3 w - - 0 1"
439439
moves = sf.legal_moves("battlekings", fen, [])
440440
self.assertFalse(moves)
441441

@@ -466,6 +466,11 @@ def test_battlekings_first_commoner_capture_with_multiple(self):
466466
fen = "3qk3/pppp1ppp/2nkr3/4p1b1/P1N5/NPPP3P/NBNNPPPN/8 w - - 3 8"
467467
self._check_immediate_game_end("battlekings", fen, ["c4d6"], True, -sf.VALUE_MATE)
468468

469+
def test_battlekings_gating_capture_allows_commoner_spawn(self):
470+
fen = "1Q1QRQ2/BqqqQQQQ/R2Rqq1Q/QQQQ2qQ/qB1QRRq1/rq1q1BNQ/qQQQNRNP/1R6 b - - 0 57"
471+
moves = sf.legal_moves("battlekings", fen, [])
472+
self.assertIn("a2b2", moves)
473+
469474
def test_legal_moves(self):
470475
fen = "10/10/10/10/10/k9/10/K9 w - - 0 1"
471476
result = sf.legal_moves("capablanca", fen, [])

0 commit comments

Comments
 (0)