コード例 #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)
コード例 #2
0
    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