import random


def random_action(state):

    """
    Plays a game using random actions until a terminal state is reached.
    Returns score of that game. 
    """
    while not state.is_terminal():
        valid = state.get_valid_actions()
        if not valid:
            raise Exception("Non-terminal state has no valid actions: " + str(state))
        else:
            action = random.choice(valid)
            state = state.act(action)
    return state.get_score()