Пример #1
0
def selfplay(nn, game: GameState, **game_args):
    states = []
    optimal_pis = []
    game_outcome = None

    state = game.init(**game_args)
    mcts = MCTS(game, nn)
    turn = -1

    times = [time()]
    while game_outcome is None:
        turn += 1
        if turn % 2:
            print("Turn {}".format(turn))
            print(str(state))

        optimal_pi = mcts.search()

        states.append(state)
        optimal_pis.append(optimal_pi)

        action = sample_action(state, optimal_pi)
        mcts.next_turn(action)
        state = state.take_action(action)

        game_outcome = state.game_outcome(last_move=action)
        t_i = time()
        print("Move time: {:.2f}s".format(t_i - times[-1]))
        times.append(t_i)

    print(f"Final turn: {turn}")
    print("Total time: {:.2f}s".format(times[-1] - times[0]))
    if game_outcome == GameOutcomes.DRAW:
        print("It's a draw!!")
    elif turn % 2 == 0:
        print("First player wins!")
        print(str(state))
    else:
        print("Second player wins!")
        state.inverse()
        print(str(state))

    if game_outcome == GameOutcomes.DRAW:
        z = [0] * len(states)
    elif game_outcome == GameOutcomes.LOSS:
        z = [(-1)**(i + 1) for i in range(len(states), 0, -1)]
    else:
        raise Exception('Invalid game outcome: {}'.format(game_outcome))

    nn.fit_game_state(states, optimal_pis, z)
Пример #2
0
def interactive_play(game: GameState, nn, start, **game_args):
    game_outcome = None

    state = game.init(**game_args)
    mcts = MCTS(game, nn)
    turn = -1 if start else 0

    while game_outcome is None:
        turn += 1

        is_player_turn = (turn % 2 == 0)

        if is_player_turn:
            print("Turn {}".format(turn // 2))
            print(str(state))
            valid_action = False
            while valid_action is False:
                action = int(input("What's your next move? (0..6): "))
                try:
                    mcts.next_turn(action)
                    valid_action = True
                except ValueError:
                    print("Invalid action, pick another one")
                    valid_action = False

            state = state.take_action(action)

        else:
            optimal_pi = mcts.search()

            action = sample_action(state, optimal_pi)
            mcts.next_turn(action)
            state = state.take_action(action)

        game_outcome = state.game_outcome(last_move=action)

    print("Game finished")
    if game_outcome == GameOutcomes.DRAW:
        print("It was a draw!!")
    elif is_player_turn:
        print("You won!")
        state.inverse()
        print(str(state))
    else:
        print("You loose :(")
        print(str(state))