def is_in_check(board_state): if board_state.turn == TURN_WHITE: king = msb(board_state.occupied_w & board_state.kings) return bool(_attackers_mask(board_state, TURN_BLACK, king, board_state.occupied)) else: king = msb(board_state.occupied_b & board_state.kings) return bool(_attackers_mask(board_state, TURN_WHITE, king, board_state.occupied))
def _is_safe(board_state, king, blockers, move): if move.from_square == king: if is_castling(board_state, move): return True else: return not bool(_attackers_mask(board_state, not board_state.turn, move.to_square, board_state.occupied)) elif is_en_passant(board_state, move): return (pin_mask(board_state, board_state.turn, move.from_square) & BB_SQUARES[move.to_square] and not _ep_skewered(board_state, king, move.from_square)) else: return (not blockers & BB_SQUARES[move.from_square] or BB_RAYS[move.from_square, move.to_square] & BB_SQUARES[king])
def generate_legal_moves(board_state, from_mask=BB_ALL, to_mask=BB_ALL): if board_state.turn == TURN_WHITE: king = msb(board_state.kings & board_state.occupied_w) else: king = msb(board_state.kings & board_state.occupied_b) blockers = _slider_blockers(board_state, king) checkers = _attackers_mask(board_state, not board_state.turn, king, board_state.occupied) #If in check if checkers:#If no moves are found it needs to be passed along that the board is in check, so it doesn't need to be computed again directly afterwards for move in _generate_evasions(board_state, king, checkers, from_mask, to_mask): if _is_safe(board_state, king, blockers, move): yield move else:#If no moves are found it needs to be passed along that the board is not in check, so it doesn't need to be computed again directly afterwards for move in generate_pseudo_legal_moves(board_state, from_mask, to_mask): if _is_safe(board_state, king, blockers, move): yield move