Skip to content

Commit 2edbbc5

Browse files
committed
Disallow friendly king captures in self-capture variants
1 parent 6bce59e commit 2edbbc5

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/movegen.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ namespace {
152152

153153
const Bitboard pawns = pos.pieces(Us, PAWN);
154154
const Bitboard movable = pos.board_bb(Us, PAWN) & ~pos.pieces();
155-
const Bitboard capturable = pos.board_bb(Us, PAWN) & (allowFriendlyCaptures ? pos.pieces() : pos.pieces(Them));
155+
const Bitboard friendlyCapturable = pos.pieces(Us) & ~pos.pieces(Us, KING);
156+
const Bitboard capturable = pos.board_bb(Us, PAWN)
157+
& (allowFriendlyCaptures ? (pos.pieces(Them) | friendlyCapturable)
158+
: pos.pieces(Them));
156159

157160
target = Type == EVASIONS ? target : AllSquares;
158161

@@ -417,7 +420,7 @@ namespace {
417420

418421
captureTarget = target;
419422
if (pos.self_capture() && (Type == NON_EVASIONS || Type == CAPTURES))
420-
captureTarget |= pos.pieces(Us);
423+
captureTarget |= pos.pieces(Us) & ~pos.pieces(Us, KING);
421424

422425
moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
423426
for (PieceSet ps = pos.piece_types() & ~(piece_set(PAWN) | KING); ps;)
@@ -476,7 +479,7 @@ namespace {
476479
Bitboard kingMoves = pos.moves_from (Us, KING, ksq) & ~pos.pieces();
477480
Bitboard kingCaptureMask = Type == EVASIONS ? ~pos.pieces(Us) : captureTarget;
478481
if (Type == EVASIONS && pos.self_capture())
479-
kingCaptureMask |= pos.pieces(Us);
482+
kingCaptureMask |= pos.pieces(Us) & ~pos.pieces(Us, KING);
480483
Bitboard kingQuietMask = Type == EVASIONS ? ~pos.pieces(Us) : target;
481484
Bitboard b = (kingAttacks & kingCaptureMask) | (kingMoves & kingQuietMask);
482485
while (b)

src/position.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,15 @@ bool Position::pseudo_legal(const Move m) const {
13711371
return false;
13721372

13731373
// The destination square cannot be occupied by a friendly piece unless self capture is allowed
1374-
if ((pieces(us) & to) && !(self_capture() && capture(m)))
1375-
return false;
1374+
if (pieces(us) & to)
1375+
{
1376+
if (!(self_capture() && capture(m)))
1377+
return false;
1378+
1379+
// Friendly kings are never capturable, even when self-capture is enabled
1380+
if (type_of(piece_on(to)) == KING)
1381+
return false;
1382+
}
13761383

13771384
// Handle the special case of a pawn move
13781385
if (type_of(pc) == PAWN)

0 commit comments

Comments
 (0)