当前位置: 首页>>代码示例>>Python>>正文


Python Piece.from_klass_and_color方法代码示例

本文整理汇总了Python中piece.Piece.from_klass_and_color方法的典型用法代码示例。如果您正苦于以下问题:Python Piece.from_klass_and_color方法的具体用法?Python Piece.from_klass_and_color怎么用?Python Piece.from_klass_and_color使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在piece.Piece的用法示例。


在下文中一共展示了Piece.from_klass_and_color方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import from_klass_and_color [as 别名]
    def __init__(self, position, move):

        resulting_position = position.copy().make_move(move)
        captured = position._pieces[move.target._x88]
        piece = position._pieces[move.source._x88]
        ocolor = Piece.opposite_color(position.fen._to_move)

        # Pawn moves.
        enpassant = False
        if Piece.klass(piece) == PAWN:
            # En-passant.
            if move.target.file != move.source.file and not captured:
                enpassant = True
                captured = Piece.from_klass_and_color(PAWN, ocolor)

        # Castling.
        # TODO: Support Chess960.
        # TODO: Validate the castling move.
        if Piece.klass(piece) == KING:
            self.is_king_side_castle = move.target.x - move.source.x == 2
            self.is_queen_side_castle = move.target.x - move.source.x == -2
        else:
            self.is_king_side_castle = self.is_queen_side_castle = False

        # Checks.
        self.is_check = resulting_position.is_check()
        self.is_checkmate = resulting_position.is_checkmate()

        self.move = move
        self.piece = piece
        self.captured = captured
        self.is_enpassant = enpassant

        self._set_text(position)
开发者ID:arthurfait,项目名称:kivy-chess,代码行数:36,代码来源:notation.py

示例2: to_move

# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import from_klass_and_color [as 别名]
    def to_move(cls, position, san):
        
        san = str(san)

        # Castling moves.
        if san == "O-O" or san == "O-O-O":
            # TODO: Support Chess960, check the castling moves are valid.
            rank = 1 if position.fen.turn == "w" else 8
            if san == "O-O":
                return Move(
                    source=Square.from_rank_and_file(rank, 'e'),
                    target=Square.from_rank_and_file(rank, 'g'))
            else:
                return Move(
                    source=Square.from_rank_and_file(rank, 'e'),
                    target=Square.from_rank_and_file(rank, 'c'))
        # Regular moves.
        else:
            matches = cls.san_regex.match(san)
            if not matches:
                raise ValueError("Invalid SAN: %s." % repr(san))

            if matches.group(1):
                klass = Piece.klass(matches.group(1).lower())
            else:
                klass = PAWN
            piece = Piece.from_klass_and_color(klass, position.fen._to_move)
            target = Square(matches.group(4))

            source = None
            for m in position.get_legal_moves():
                if position._pieces[m.source._x88] != piece or m.target != target:
                    continue

                if matches.group(2) and matches.group(2) != m.source.file:
                    continue
                if matches.group(3) and matches.group(3) != str(m.source.rank):
                    continue

                # Move matches. Assert it is not ambiguous.
                if source:
                    raise MoveError(
                        "Move is ambiguous: %s matches %s and %s."
                            % san, source, m)
                source = m.source

            if not source:
                raise MoveError("No legal move matches %s." % san)

            return Move(source, target, matches.group(5) or None)
开发者ID:arthurfait,项目名称:kivy-chess,代码行数:52,代码来源:notation.py

示例3: _set_text

# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import from_klass_and_color [as 别名]
    def _set_text(self, position):

        move = self.move

        piece_klass = Piece.klass(self.piece)

        # Generate the SAN.
        san = ""
        if self.is_king_side_castle:
            san += "O-O"
        elif self.is_queen_side_castle:
            san += "O-O-O"
        else:
            if piece_klass != PAWN:
                san += Piece.from_klass_and_color(piece_klass, WHITE)

            if position:
                san += self._get_disambiguator(move, position)

            if self.captured:
                if piece_klass == PAWN:
                    san += move.source.file
                san += "x"
            san += move.target.name

            if move.promotion:
                san += "="
                san += move.promotion.upper()

        if self.is_checkmate:
            san += "#"
        elif self.is_check:
            san += "+"

        if self.is_enpassant:
            san += " (e.p.)"

        self._text = san
开发者ID:arthurfait,项目名称:kivy-chess,代码行数:40,代码来源:notation.py

示例4: test_from_klass_and_color

# 需要导入模块: from piece import Piece [as 别名]
# 或者: from piece.Piece import from_klass_and_color [as 别名]
 def test_from_klass_and_color(self):
     self.assertEqual('P', Piece.from_klass_and_color(piece.PAWN, piece.WHITE))
开发者ID:arthurfait,项目名称:kivy-chess,代码行数:4,代码来源:test_piece.py


注:本文中的piece.Piece.from_klass_and_color方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。