Exemplo n.º 1
0
    def __init__(self, board, parent, color, action):
        self.parent = parent  # 父节点
        self.children = []  # 子节点列表
        self.visit_times = 0  # 访问次数
        self.board = board  # 游戏选择这个Node的时的棋盘
        self.color = color  # 当前玩家
        self.prevAction = action  # 到达这个节点的action
        self.unvisitActions = list(board.get_legal_actions(color))  # 未访问过的actions
        self.isover = self.gameover(board)  # 是否结束了
        if (self.isover == False) and (len(self.unvisitActions) == 0):  # 没得走了但游戏还没结束
            self.unvisitActions.append("n")

        self.reward = {'X': 0, 'O': 0}
        self.bestVal = {'X': 0, 'O': 0}
Exemplo n.º 2
0
    def tree_search(self, board, color):
        """ 
        实现UCT方法顶层
        """
        # 只有一种选择
        actions = list(board.get_legal_actions(color))
        if len(actions) == 1:
            return list(actions)[0]

        # 创建根节点
        curBoard = deepcopy(board)
        root = Node(curBoard, None, color, None)

        # 时间限制60s以内
        try:
            func_timeout(59, self.iterate_function, args=[root])
        except FunctionTimedOut:
            pass

        return self.best_child(root, math.sqrt(2), color).curAction
Exemplo n.º 3
0
    def search(self, board, color):
        # board: 当前棋局
        # color: 当前玩家

        # 特殊情况:只有一种选择
        actions = list(board.get_legal_actions(color))
        if len(actions) == 1:
            return list(actions)[0]

        # 创建根节点
        newboard = deepcopy(board)
        root = Node(newboard, None, color, None)

        # 考虑时间限制
        try:
            # 测试程序规定每一步在60s以内
            func_timeout(59, self.whileFunc, args=[root])
        except FunctionTimedOut:
            pass

        return self.best_child(root, math.sqrt(2), color).prevAction
Exemplo n.º 4
0
 def game_over(board):
     l2 = list(board.get_legal_actions('O'))
     l1 = list(board.get_legal_actions('X'))
     return len(l2) == 0 and len(l1) == 0
Exemplo n.º 5
0
 def gameover(self, board):
     l1 = list(board.get_legal_actions('X'))
     l2 = list(board.get_legal_actions('O'))
     return (len(l1) == 0 and len(l2) == 0)