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


Python Board.get_figure方法代码示例

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


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

示例1: __init__

# 需要导入模块: from chess.board import Board [as 别名]
# 或者: from chess.board.Board import get_figure [as 别名]
class ChessLogic:
    """
    Game logic for chess game.
    """

    def __init__(self, state):
        self._board = Board(state)
        self._current_player = figures.figure.WHITE

    def get_moves(self, figure):
        """
        Returns all moves for selected figure.
        """

        logging.info("Generating moves for %s on %s:%s", figure.get_type(),
                     figure.get_position()[0], figure.get_position()[1])
        moves = figure.generate_moves(8, 8)
        return moves

    def move_figure(self, start, target):
        """
        Move with figure from start to target position.
        """
        figure = self._board.get_figure(start[0], start[1])
        # Check if figure is on the start position
        if figure is None:
            logging.error(
                "No figure on specified position %s:%s",
                start[0],
                start[1])
            return
        # Check if figure is owned by current player
        if figure.get_owner() != self._current_player:
            logging.error("Can't move with oponent figure")
            return

        if figure.move_to(target[0], target[1], 8, 8):
            # Castling
            if figure.get_type() == figures.figure.KING and figure.isCastling():
                pos = figure.get_position()
                # Check if king moved left or right
                if pos[0] > 4:
                    rook = self._board.get_figure(7, pos[1])
                    rook.move_to(
                        target[0] - 1,
                        pos[1],
                        8,
                        8,
                        False)
                    logging.info(
                        "Move rook figure on %s:%s after castling", 7, pos[1])
                else:
                    rook = self._board.get_figure(0, pos[1])
                    rook.move_to(
                        target[0] + 1,
                        pos[1],
                        8,
                        8,
                        False)
                    logging.info(
                        "Move rook figure on %s:%s after castling", 0, pos[1])

    def get_state(self):
        """
        Get current state of game.
        """
        state = []
        # Loop through whole game board
        # If figure is found in position, then print figure
        # else print empty space
        logging.info("Return chess game board state.")
        for y_index in range(0, 8):
            for x_index in range(0, 8):
                fig = self._board.get_figure(x_index, y_index)
                if fig:
                    logging.debug(
                        "Figure found on %s:%s with color %s",
                        x_index,
                        y_index,
                        fig.get_owner())
                    state.append(self._get_figure_mark(fig))
                else:
                    state.append('')

        return state

    def _get_figure_mark(self, figure):
        """
        Return mark that will be print in application output.
        """
        mark = ''
        if figure.get_owner() == figures.figure.BLACK:
            mark += 'b'
        else:
            mark += 'w'

        fig_type = figure.get_type()

        if fig_type == figures.figure.PAWN:
            mark += 'p'
#.........这里部分代码省略.........
开发者ID:Zlopez,项目名称:chess,代码行数:103,代码来源:logic.py


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