Exemplo n.º 1
0
    def monte_carlo(self, board, depth=None):
        if depth is None:
            depth = []

        ttt = TicTacToe()
        ttt.copy(board)

        # ランダムに自分の一手を行う
        position = self.pick_one(ttt)
        ttt.set(position, self.t)

        if ttt.eval() == TicTacToe.ON_PROGRESS:
            # 敵の手もランダムに一手を行う
            t_enemy = TicTacToe.O if self.t == TicTacToe.X else TicTacToe.X
            position_enemy = self.pick_one(ttt)
            ttt.set(position_enemy, t_enemy)

        depth = depth + [position]

        # 結果を判定。決着がつかないなら再帰的に繰り返す
        result = ttt.eval()
        if result != TicTacToe.ON_PROGRESS:
            return {
                "position": depth[0],
                "game_result": result,
                "moves": depth
            }

        return self.monte_carlo(ttt.board, depth)
Exemplo n.º 2
0
def TicTacToeGame():
    from tictactoe import TicTacToe
    from AI import AnywhereCanSet, RandomUntilEnd, MonteCarloTree

    game = TicTacToe()

    # AIを選ぶ
    ai = RandomUntilEnd(game, TicTacToe.X)

    while True:
        print "\n----"
        print game.print_board(True)
        cmd = raw_input("move?:")
        if cmd == "q":
            break
        if cmd == "r":
            game.reset()
            print game.print_board()
            continue

        try:
            position = int(cmd)
            game.set(position, TicTacToe.O)
        except Exception as e:
            print e.message
            continue
        # print game.print_board()
        # print "status:", game.eval()

        print ai.play()
        print game.print_board()
        print "status:", game.eval()

        if game.eval() != TicTacToe.ON_PROGRESS:
            game.reset()
            print "\n\nnew game!"
            print game.print_board()
    def monte_carlo(self, board, t, depth=None):
        self.order_count += 1

        if depth is None:
            depth = []

        ttt = TicTacToe()
        ttt.copy(board)

        # ランダムに自分の一手を行う
        position = _pick_one(ttt)
        ttt.set(position, t)

        t_enemy = TicTacToe.O if t == TicTacToe.X else TicTacToe.X

        depth = depth + [(t, position)]

        # 結果を判定。決着がつかないなら再帰的に繰り返す
        status = ttt.eval()
        if status != TicTacToe.ON_PROGRESS:
            result = {
                "game_result": status,
                "moves": depth,
                TicTacToe.WIN_O: 0,
                TicTacToe.WIN_X: 0,
                TicTacToe.DRAW: 0,
            }
            result[status] += 1
            return result

        result = {
            "position": position,
            TicTacToe.WIN_O: 0,
            TicTacToe.WIN_X: 0,
            TicTacToe.DRAW: 0,
        }
        mc_results = [
            self.monte_carlo(ttt.board, t_enemy, depth) for _ in range(1)
        ]
        for mc_result in mc_results:
            result[TicTacToe.WIN_O] += mc_result[TicTacToe.WIN_O]
            result[TicTacToe.WIN_X] += mc_result[TicTacToe.WIN_X]
            result[TicTacToe.DRAW] += mc_result[TicTacToe.DRAW]
        return result