Ejemplo n.º 1
0
def play_turn(game_state: GameState, ai) -> None:
    """
    Play a single turn in a game using the ai to select moves to take.
    :param game_state: GameState at the start of the turn
    :param ai: Function to choose the next action, based on the current GameState
    :return: None
    """
    current_player = game_state.player_turn

    while game_state.player_turn == current_player:
        game_state.assert_valid_game_state()
        chosen_action = ai(game_state, game_state.get_next_actions())
        game_state.resolve_action(chosen_action)
Ejemplo n.º 2
0
def mcts_search(game_state: GameState, player_turn, num_worms, Qsa, Nsa, Ns, visited):
    if game_state.player_turn != player_turn:
        return get_change_worm_count(game_state, player_turn, num_worms)

    s = str(game_state)
    if s not in visited:
        visited.add(s)
        Ns[s] = 0
        gs_copy = game_state.__copy__()
        gs_end_turn = random_rollout_result(gs_copy, player_turn)
        game_state.assert_valid_game_state()
        return get_change_worm_count(gs_end_turn, player_turn, num_worms)

    next_state = game_state.__copy__()
    ucb_action = get_best_action_ucb(next_state, Qsa, Nsa, Ns)
    next_state.resolve_action(ucb_action)
    v = mcts_search(next_state, player_turn, num_worms, Qsa, Nsa, Ns, visited)
    update_search_values(v, s, str(ucb_action), Qsa, Nsa, Ns)
    return v