當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。