
What's the best way of detecting whether a move gives check (preferably without having to play the move)? I'm using a bitboard representation

- Ori
Moderator: Ras
Thank you, I'm familiar with the article, and my move generation is pretty fastexpositor wrote: ↑Sun Jan 16, 2022 9:07 am I'd recommend reading Generating Legal Chess Moves Efficiently by Peter Ellis Jones.
Code: Select all
// Direct checks
// If a piece moves to a destination within the vantage of its kind,
// we know check is being delivered
opp_king_sq := boards[opponent+king].tzcnt()
rook_vantage := rook_destinations(occupation, opp_king_sq)
bishop_vantage := bishop_destinations(occupation, opp_king_sq)
vantage_masks := [
0, rook_vantage | bishop_vantage, rook_vantage, bishop_vantage,
knight_destinations(opp_king_sq), pawn_attacks(turn, opp_king_sq)
]
// Discovered checks
// If a piece is on a waylay square, we will calculate its span to the enemy king, and
// if the piece moves to a destination outside that span, we know check is being delivered
waylay_mask := 0
for attacker in queen...knight {
atk_sources := self.boards[player+attacker]
while atk_sources != 0 {
atk_src := atk_sources.tzcnt()
span := match attacker {
queen => any_span(opp_king_sq, atk_src),
rook => cruciform_span(opp_king_sq, atk_src),
bishop => saltire_span(opp_king_sq, atk_src),
}
intermediate := span & occupation
if intermediate.popcnt() == 1 {
waylay_mask |= intermediate
}
atk_sources &= atk_sources - 1 // clears the lowest set bit
}
}
// and while generating the moves for a particular piece,
discovery_mask :=
if (1 << src_sq) & waylay_mask == 0 {
0xFFFFFFFFFFFFFFFF
}
else {
line_through(src_sq, opp_king_sq)
}
gives_check :=
(1 << dst_sq) & vantage_mask[piece] != 0 || (1 << dst_sq) & discovery_mask == 0
You dont need to play the move - just emulate the occupancy change on the single register with occ ^= (from | to) for moves and occ &= ~ from for takes.
Pre-compute a u64 lookup[pt][s1][s2], for every piece type if there a legal move connection between s1,s2 set bit 0. For ray pieces set the inbetween bits of they exist.