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


Python Board.clone方法代码示例

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


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

示例1: Game

# 需要导入模块: from term2048.board import Board [as 别名]
# 或者: from term2048.board.Board import clone [as 别名]

#.........这里部分代码省略.........

    def incScore(self, pts):
        """
        update the current score by adding it the specified number of points
        """
        self.score += pts
        if self.score > self.best_score:
            self.best_score = self.score

    def end(self):
        """
        return True if the game is finished
        """
        return not (self.board.won() or self.board.canMove())

    def eval_board(self, board):
        import math
        v = 0
        for x in range(len(board.cells) - 1):
            for y in range(len(board.cells[x]) - 1):
                v += abs(math.log(board.cells[x][y] if board.cells[x][y] else 1, 2) -
                         math.log(board.cells[x+1][y] if board.cells[x+1][y] else 1, 2))
                v += abs(math.log(board.cells[x][y] if board.cells[x][y] else 1, 2) -
                         math.log(board.cells[x][y+1] if board.cells[x][y+1] else 1, 2))
        for x in range(1,len(board.cells)):
            for y in range(1, len(board.cells[x])):
                v += abs(math.log(board.cells[x][y] if board.cells[x][y] else 1, 2) -
                         math.log(board.cells[x-1][y] if board.cells[x-1][y] else 1, 2))
                v += abs(math.log(board.cells[x][y] if board.cells[x][y] else 1, 2) -
                         math.log(board.cells[x][y-1] if board.cells[x][y-1] else 1, 2))
        return -v

    def eval_move(self, m):
        board = self.board.clone()
        board.move(m)
        if board.cells == self.board.cells:
            return 0
        v = 0
        return self.eval_board(board)
        for i in range(50): # try random moves 1000 times
            moves = (random.choice(range(1, 5)) for _ in range(3))
            for _ in range(2):
                after_move_board = board.clone()
                vv = 0
                for m in moves:
                    after_move_board.move(m)
                vv += self.eval_board(after_move_board)
            v = max(vv, v)
        return v

    def readMove(self):
        """
        read and return a move to pass to a board
        """
        #k = keypress.getKey()
        #return Game.__dirs.get(k)
        options = range(1,5)
        max_move = []
        max_val = float('-inf')
        for m in options:
            v = self.eval_move(m)
            if v > max_val:
                max_move = [m]
                max_val = v
            elif v == max_val:
                max_move.append(m)
开发者ID:Mike-Xie,项目名称:term2048solver,代码行数:70,代码来源:game.py


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