def back_rank_mate(puzzle: Puzzle) -> bool: node = puzzle.game.end() board = node.board() king = board.king(not puzzle.pov) assert king is not None assert isinstance(node, ChildNode) back_rank = 7 if puzzle.pov else 0 if board.is_checkmate() and square_rank(king) == back_rank: squares = SquareSet.from_square(king + (-8 if puzzle.pov else 8)) if puzzle.pov: if chess.square_file(king) < 7: squares.add(king - 7) if chess.square_file(king) > 0: squares.add(king - 9) else: if chess.square_file(king) < 7: squares.add(king + 9) if chess.square_file(king) > 0: squares.add(king + 7) for square in squares: piece = board.piece_at(square) if piece is None or piece.color == puzzle.pov or board.attackers( puzzle.pov, square): return False return any( square_rank(checker) == back_rank for checker in board.checkers()) return False
def exposed_king(puzzle: Puzzle) -> bool: if puzzle.pov: pov = puzzle.pov board = puzzle.mainline[0].board() else: pov = not puzzle.pov board = puzzle.mainline[0].board().mirror() king = board.king(not pov) assert king is not None if chess.square_rank(king) < 5: return False squares = SquareSet.from_square(king - 8) if chess.square_file(king) > 0: squares.add(king - 1) squares.add(king - 9) if chess.square_file(king) < 7: squares.add(king + 1) squares.add(king - 7) for square in squares: if board.piece_at(square) == Piece(PAWN, not pov): return False for node in puzzle.mainline[1::2][1:-1]: if node.board().is_check(): return True return False