# Challenge 87 - Choose Your Own Story
# Each place offers two valid destinations.
SCENES = {
    "crossroads": ("A fork in the path.", ("forest", "bridge")),
    "forest": ("A hidden woodland path.", ("tower", "cellar")),
    "bridge": ("A creaking bridge.", ("cellar", "tower")),
    "tower": ("A tower with two staircases.", ("safe", "trapped")),
    "cellar": ("Two mysterious doors.", ("trapped", "safe")),
}
ENDINGS = {"safe": "You find your way home!",
           "trapped": "You must wait for rescue."}

def follow_path(choices):
    current = "crossroads"
    for choice in choices:
        if current in ENDINGS:
            break
        if choice not in (1, 2):
            raise ValueError("Choices must be 1 or 2")
        current = SCENES[current][1][choice - 1]
    return current

def play():
    while True:
        current = "crossroads"
        while current not in ENDINGS:
            description, destinations = SCENES[current]
            print(f"\n{description}")
            print(f"1: {destinations[0]} | 2: {destinations[1]}")
            answer = input("Choose 1 or 2: ").strip()
            if answer not in ("1", "2"):
                print("Choose 1 or 2.")
                continue
            current = destinations[int(answer) - 1]
        print(ENDINGS[current])
        if input("Restart? (y/n): ").strip().lower() != "y":
            return

if __name__ == "__main__":
    play()
