Пример #1
0
def ui_main(action_file=None, ai_first=False, depth=50, breadth=10):
    board = Board()
    ai, action = None, None
    ai_status = GameStatus.RedMoving if ai_first else GameStatus.BlackMoving
    # main loop
    while not board.won:
        print_board(board)
        if board.status == ai_status:
            ai = Node(board)
            action = ai.search(depth, breadth)
            write_action(ai, action, action_file)
        else:
            try:
                action = read_action(action_file)
            except (EOFError, KeyboardInterrupt):
                # end of input
                return
            except:
                action = None
            if action is None:
                print('invalid command')
                continue
        board.apply_action(action)
    print_board(board)
    print('game over', end='')
    if board.status == GameStatus.RedWon:
        print(', red won', end='')
    elif board.status == GameStatus.BlackWon:
        print(', black won', end='')
    print()
Пример #2
0
def ui_main(action_file=None, ai_first=False):
    board = Board()
    action = None
    ai_status = GameStatus.RedMoving if ai_first else GameStatus.BlackMoving
    node = Node(board)
    # main loop
    while not board.won:
        print_board(board)
        if board.status == ai_status:
            time_start = time.time()
            count = 0
            # 在限定时间进行蒙特卡罗模拟
            while((time.time() - time_start) < 30):
                count += 1
                node.search()
            logging.debug("total count %d", count)
            action = node.find_best_child().action
            write_action(node, action, action_file)
        else:
            try:
                action = read_action(action_file)
            except (EOFError, KeyboardInterrupt):
                # end of input
                return
            except:
                action = None
            if action is None:
                print('invalid command')
                continue
        node = node.apply_action(action)
        board = node.status
    print_board(board)
    print('game over', end='')
    if board.status == GameStatus.RedWon:
        print(', red won', end='')
    elif board.status == GameStatus.BlackWon:
        print(', black won', end='')
    print()