def roll(state, dice):
    """Apply the roll action to a state (and a die roll d) to yield a new state.

    If d is 1, get 1 point (losing any accumulated 'pending' points),
    and it is the other player's turn. If d > 1, add d to 'pending' points.
    """
    player, me, you, pending = state
    if dice == 1:
        return (not player, you, me + 1, 0)
    else:
        return (player, me, you, pending + dice)