from typing import Optional


def run_episode(game, learner, train: bool = True, s0: Optional[list] = None) -> float:
    """Run an episode of `learner` on `game`

    Args:
        train: True if learner is training, False for evaluation
        s0: The start state of the game, None means random start

    Returns:
        Epsiode payoff
    """
    game.reset(s0)
    while not game.is_terminal():
        a = learner.act(game.context(), game.num_legal_actions(), train)
        game.step(a)
    if train:
        learner.update_from_episode(game.episode())
        learner.update_rates()
    return game.payoff()