pgn_read/core/game.py
def legal_move_to_square_exists(self, square, capture, king_square):
        """Return True if a legal move to square exists.

        This method is intended for deciding if a check is checkmate, and it
        is assumed the king cannot move.

        En-passant captures cannot block a check but they can remove a pawn
        giving check.  Test if en-passant prevents checkmate as last thing
        because it seems least likely case and the algorithm is unlike any of
        the other line and point-to-point cases.

        """
        ppd = self._piece_placement_data
        checked_king = ppd[king_square].name
        for from_square, piece in ppd.copy().items():
            if piece.color != self._active_color:
                continue
            pn = piece.name
            if pn == checked_king:
                continue
            if pn in FEN_PAWNS:
                pn += capture
                if square in SOURCE_SQUARES[pn]:
                    if from_square not in SOURCE_SQUARES[pn][square]:
                        continue
                    ppd_to = ppd.pop(square, None)
                    ppd_from = ppd.pop(from_square)
                    ppd[square] = ppd_from
                    check = self.is_square_attacked_by_other_side(king_square)
                    del ppd[square]
                    if ppd_to is not None:
                        ppd[square] = ppd_to
                    ppd[from_square] = ppd_from
                    if not check:
                        return True
                continue
            if from_square in FEN_SOURCE_SQUARES[pn][square]:
                if pn in (FEN_WHITE_KNIGHT, FEN_BLACK_KNIGHT):
                    ppd_to = ppd.pop(square, None)
                    ppd_from = ppd.pop(from_square)
                    ppd[square] = ppd_from
                    check = self.is_square_attacked_by_other_side(king_square)
                    del ppd[square]
                    if ppd_to is not None:
                        ppd[square] = ppd_to
                    ppd[from_square] = ppd_from
                    if not check:
                        return True
                    continue
                ptp = POINT_TO_POINT[from_square, square]
                for sq in ptp[2][ptp[0]:ptp[1]]:
                    if sq in ppd:
                        break
                else:
                    ppd_to = ppd.pop(square, None)
                    ppd_from = ppd.pop(from_square)
                    ppd[square] = ppd_from
                    check = self.is_square_attacked_by_other_side(king_square)
                    del ppd[square]
                    if ppd_to is not None:
                        ppd[square] = ppd_to
                    ppd[from_square] = ppd_from
                    if not check:
                        return True
        if capture and self._en_passant_target_square != FEN_NULL:
            for k, v in EN_PASSANT_TARGET_SQUARES[self._active_color].items():
                if v != self._en_passant_target_square:
                    continue
                if k[0] != square:
                    continue
                if self._active_color == FEN_WHITE_ACTIVE:
                    pawn = FEN_WHITE_PAWN
                else:
                    pawn = FEN_BLACK_PAWN
                for from_square in FEN_SOURCE_SQUARES[
                    pawn][self._en_passant_target_square]:
                    piece = ppd.get(from_square)
                    if piece is None:
                        continue
                    if piece.name != pawn:
                        continue
                    ppd_to = ppd.pop(square, None)
                    ppd_from = ppd.pop(from_square)
                    ppd[self._en_passant_target_square] = ppd_from
                    check = self.is_square_attacked_by_other_side(king_square)
                    del ppd[self._en_passant_target_square]
                    if ppd_to is not None:
                        ppd[square] = ppd_to
                    ppd[from_square] = ppd_from
                    if not check:
                        return True
        return False
Return True if a legal move to square exists.

        This method is intended for deciding if a check is checkmate, and it
        is assumed the king cannot move.

        En-passant captures cannot block a check but they can remove a pawn
        giving check.  Test if en-passant prevents checkmate as last thing
        because it seems least likely case and the algorithm is unlike any of
        the other line and point-to-point cases.

        
Return True if a legal move to square exists.
This method is intended for deciding if a check is checkmate, and it
is assumed the king cannot move.

En-passant captures cannot block a check but they can remove a pawn
giving check.  Test if en-passant prevents checkmate as last thing
because it seems least likely case and the algorithm is unlike any of
the other line and point-to-point cases.
Return True if a legal move to square exists . This method is intended for deciding if a check is checkmate and it is assumed the king cannot move . En - passant captures cannot block a check but they can remove a pawn giving check . Test if en - passant prevents checkmate as last thing because it seems least likely case and the algorithm is unlike any of the other line and point - to - point cases .
def legal_move_to_square_exists(self, square, capture, king_square):
        ppd = self._piece_placement_data
        checked_king = ppd[king_square].name
        for from_square, piece in ppd.copy().items():
            if piece.color != self._active_color:
                continue
            pn = piece.name
            if pn == checked_king:
                continue
            if pn in FEN_PAWNS:
                pn += capture
                if square in SOURCE_SQUARES[pn]:
                    if from_square not in SOURCE_SQUARES[pn][square]:
                        continue
                    ppd_to = ppd.pop(square, None)
                    ppd_from = ppd.pop(from_square)
                    ppd[square] = ppd_from
                    check = self.is_square_attacked_by_other_side(king_square)
                    del ppd[square]
                    if ppd_to is not None:
                        ppd[square] = ppd_to
                    ppd[from_square] = ppd_from
                    if not check:
                        return True
                continue
            if from_square in FEN_SOURCE_SQUARES[pn][square]:
                if pn in (FEN_WHITE_KNIGHT, FEN_BLACK_KNIGHT):
                    ppd_to = ppd.pop(square, None)
                    ppd_from = ppd.pop(from_square)
                    ppd[square] = ppd_from
                    check = self.is_square_attacked_by_other_side(king_square)
                    del ppd[square]
                    if ppd_to is not None:
                        ppd[square] = ppd_to
                    ppd[from_square] = ppd_from
                    if not check:
                        return True
                    continue
                ptp = POINT_TO_POINT[from_square, square]
                for sq in ptp[2][ptp[0]:ptp[1]]:
                    if sq in ppd:
                        break
                else:
                    ppd_to = ppd.pop(square, None)
                    ppd_from = ppd.pop(from_square)
                    ppd[square] = ppd_from
                    check = self.is_square_attacked_by_other_side(king_square)
                    del ppd[square]
                    if ppd_to is not None:
                        ppd[square] = ppd_to
                    ppd[from_square] = ppd_from
                    if not check:
                        return True
        if capture and self._en_passant_target_square != FEN_NULL:
            for k, v in EN_PASSANT_TARGET_SQUARES[self._active_color].items():
                if v != self._en_passant_target_square:
                    continue
                if k[0] != square:
                    continue
                if self._active_color == FEN_WHITE_ACTIVE:
                    pawn = FEN_WHITE_PAWN
                else:
                    pawn = FEN_BLACK_PAWN
                for from_square in FEN_SOURCE_SQUARES[
                    pawn][self._en_passant_target_square]:
                    piece = ppd.get(from_square)
                    if piece is None:
                        continue
                    if piece.name != pawn:
                        continue
                    ppd_to = ppd.pop(square, None)
                    ppd_from = ppd.pop(from_square)
                    ppd[self._en_passant_target_square] = ppd_from
                    check = self.is_square_attacked_by_other_side(king_square)
                    del ppd[self._en_passant_target_square]
                    if ppd_to is not None:
                        ppd[square] = ppd_to
                    ppd[from_square] = ppd_from
                    if not check:
                        return True
        return False
def legal_move_to_square_exists ( self , square , capture , king_square ) : ppd = self . _piece_placement_data checked_king = ppd [ king_square ] . name for from_square , piece in ppd . copy ( ) . items ( ) : if piece . color != self . _active_color : continue pn = piece . name if pn == checked_king : continue if pn in FEN_PAWNS : pn += capture if square in SOURCE_SQUARES [ pn ] : if from_square not in SOURCE_SQUARES [ pn ] [ square ] : continue ppd_to = ppd . pop ( square , None ) ppd_from = ppd . pop ( from_square ) ppd [ square ] = ppd_from check = self . is_square_attacked_by_other_side ( king_square ) del ppd [ square ] if ppd_to is not None : ppd [ square ] = ppd_to ppd [ from_square ] = ppd_from if not check : return True continue if from_square in FEN_SOURCE_SQUARES [ pn ] [ square ] : if pn in ( FEN_WHITE_KNIGHT , FEN_BLACK_KNIGHT ) : ppd_to = ppd . pop ( square , None ) ppd_from = ppd . pop ( from_square ) ppd [ square ] = ppd_from check = self . is_square_attacked_by_other_side ( king_square ) del ppd [ square ] if ppd_to is not None : ppd [ square ] = ppd_to ppd [ from_square ] = ppd_from if not check : return True continue ptp = POINT_TO_POINT [ from_square , square ] for sq in ptp [ 2 ] [ ptp [ 0 ] : ptp [ 1 ] ] : if sq in ppd : break else : ppd_to = ppd . pop ( square , None ) ppd_from = ppd . pop ( from_square ) ppd [ square ] = ppd_from check = self . is_square_attacked_by_other_side ( king_square ) del ppd [ square ] if ppd_to is not None : ppd [ square ] = ppd_to ppd [ from_square ] = ppd_from if not check : return True if capture and self . _en_passant_target_square != FEN_NULL : for k , v in EN_PASSANT_TARGET_SQUARES [ self . _active_color ] . items ( ) : if v != self . _en_passant_target_square : continue if k [ 0 ] != square : continue if self . _active_color == FEN_WHITE_ACTIVE : pawn = FEN_WHITE_PAWN else : pawn = FEN_BLACK_PAWN for from_square in FEN_SOURCE_SQUARES [ pawn ] [ self . _en_passant_target_square ] : piece = ppd . get ( from_square ) if piece is None : continue if piece . name != pawn : continue ppd_to = ppd . pop ( square , None ) ppd_from = ppd . pop ( from_square ) ppd [ self . _en_passant_target_square ] = ppd_from check = self . is_square_attacked_by_other_side ( king_square ) del ppd [ self . _en_passant_target_square ] if ppd_to is not None : ppd [ square ] = ppd_to ppd [ from_square ] = ppd_from if not check : return True return False
Return True if a legal move to square exists.
Return True if a legal move to square exists .
"""Return True if a legal move to square exists.

        This method is intended for deciding if a check is checkmate, and it
        is assumed the king cannot move.

        En-passant captures cannot block a check but they can remove a pawn
        giving check.  Test if en-passant prevents checkmate as last thing
        because it seems least likely case and the algorithm is unlike any of
        the other line and point-to-point cases.

        """
self
square
capture
king_square