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


Python Board.is_in_bounds方法代码示例

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


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

示例1: OthelloBoard

# 需要导入模块: import Board [as 别名]
# 或者: from Board import is_in_bounds [as 别名]
class OthelloBoard(Board):
    def __init__(self, *args, **kwargs):
        if len(args) == 4:
            #col, row, p1_symbol, p2_symbol
            self.board = Board(args[0], args[1])
            self._p1_symbol = args[2]
            self._p2_symbol = args[3]
            self.currentTurn = True
        elif len(args) == 1:
            #OthelloBoard
            self.board = Board(args[0].board)
            self._p1_symbol = args[0]._p1_symbol
            self._p2_symbol = args[0]._p2_symbol
            self.currentTurn = args[0].currentTurn
        else:
            sys.exit("OthelloBoard: Incorrect __init__ usage")
    def initialize(self):
        self.board.set_cell(self.board._num_cols / 2 - 1, self.board._num_rows / 2 - 1, self._p1_symbol)
        self.board.set_cell(self.board._num_cols / 2, self.board._num_rows / 2, self._p1_symbol)
        self.board.set_cell(self.board._num_cols / 2 - 1, self.board._num_rows / 2, self._p2_symbol)
        self.board.set_cell(self.board._num_cols / 2, self.board._num_rows / 2 - 1, self._p2_symbol)
    def set_coords_in_direction(self, col, row, dir):
        if dir == Direction.N:
            next_col = col
            next_row = row + 1
        elif dir == Direction.NE:
            next_col = col + 1;
            next_row = row + 1;
        elif dir == Direction.E:
            next_col = col + 1;
            next_row = row;
        elif dir == Direction.SE:
            next_col = col + 1;
            next_row = row - 1;
        elif dir == Direction.S:
            next_col = col;
            next_row = row - 1;
        elif dir == Direction.SW:
            next_col = col - 1;
            next_row = row - 1;
        elif dir == Direction.W:
            next_col = col - 1;
            next_row = row;
        elif dir == Direction.NW:
            next_col = col - 1;
            next_row = row + 1;
        else:
            next_col = None
            next_row = None   
        return next_col, next_row
    def check_endpoint(self, col, row, symbol, dir, match_symbol):
        if not self.board.is_in_bounds(col, row) or self.board.is_cell_empty(col, row):
            return False
        else:
            if match_symbol:
                if self.board.get_cell(col, row) == symbol:
                    return True
                else:
                    next_col, next_row = self.set_coords_in_direction(col, row, dir)
                    return self.check_endpoint(next_col, next_row, symbol, dir, match_symbol)
            else:
                if self.board.get_cell(col, row) == symbol:
                    return False
                else:
                    next_col, next_row = self.set_coords_in_direction(col, row, dir)
                    return self.check_endpoint(next_col, next_row, symbol, dir, not match_symbol)
    def is_legal_move(self, col, row, symbol):
        result = False
        if not self.board.is_in_bounds(col, row) or not self.board.is_cell_empty(col, row):
            return result
        else:
            for d in Direction:
                next_col, next_row = self.set_coords_in_direction(col, row, d)
                if self.check_endpoint(next_col, next_row, symbol, d, False):
                    result = True
                    break
        return result
    def flip_pieces_helper(self, col, row, symbol, dir):
        if self.board.get_cell(col, row) == symbol:
            return 0
        else:
            self.board.set_cell(col, row, symbol)
            next_col, next_row = self.set_coords_in_direction(col, row, dir)
            return 1 + self.flip_pieces_helper(next_col, next_row, symbol, dir)
    def flip_pieces(self, col, row, symbol):
        pieces_flipped = 0
        if not self.board.is_in_bounds(col, row):
            return None
        for d in Direction:
            next_col, next_row = self.set_coords_in_direction(col, row, d)
            if self.check_endpoint(next_col, next_row, symbol, d, False):
                pieces_flipped += self.flip_pieces_helper(next_col, next_row, symbol, d)
        return pieces_flipped
    def has_legal_moves_remaining(self, symbol):
        for i in range(self.board._num_cols):
            for j in range(self.board._num_rows):
                if self.board.is_cell_empty(i, j) and self.is_legal_move(i, j, symbol):
                    return True
        return False
    def count_score(self, symbol):
#.........这里部分代码省略.........
开发者ID:muratcancicek,项目名称:Assignment-Projects,代码行数:103,代码来源:OthelloBoard.py


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