Computer 2

Python Adventure Code


# Adventure by Mr. Graham

def choose(message, choices):
    """Get a choice from the player."""
    print("\n" + message)
    keys = []
    for (key, choice) in choices:
        print("  {}: {}".format(key, choice))
        keys.append(key)
    key_str = "(" + ", ".join(keys) + ")"
    chosen = ""
    while(chosen == ""):
        answer = input(key_str + " ? ")
        if answer in keys:
            chosen = answer
    return chosen


print("\n\nPython Adventure")
score = 0
room = 1
finished = False
apple_present = True


while not finished:

    if room == 1:
        c = choose("You are on a beautiful beach that continues " +
                    "to the north and south. There is a trail " +
                    "going east into the jungle",
                    [("n","go north"), ("e","go east"), ("s","go south")])
        if c == "n":
            room = 4
        elif c == "e":
            room = 5
        elif c == "s":
            room = 2
            
    elif room == 2:
        c = choose("You are at the entrance of a cave. " +
                    "A beach goes to the north.",
                    [("n", "go north"), ("c", "enter cave")])
        if c == "n":
            room = 1
        elif c == "c":
            room = 3

    elif room == 3:
        c = choose("You are in a dark cave. You can see nothing " +
                    "but the light coming in from the entrance.",
                    [("l", "leave cave"), ("e", "explore cave")])
        if c == "l":
            room = 2
        elif c == "e":
            print("\nYou fell into a very deep pit. You have died.")
            score -= 3
            finished = True

    elif room == 4:
        c = choose("You are at the north end of the beach, which " +
                    "is blocked by steep cliffs.",
                    [("c", "climb cliffs"), ("s", "go south")])
        if c == "c":
            print("\nAfter climbing 8 feet you fall, injuring yourself.")
            score -= 1
        elif c == "s":
            room = 1

    elif room == 5:
        c = choose("You are in a clearing in the jungle. A trail goes " +
                    "east and west. There is a tall tree here with " +
                    "branches almost like a ladder.",
                    [("w", "go west"), ("e", "go east"), ("c", "climb tree")])
        if c == "w":
            room = 1
        elif c == "e":
            room = 6
        elif apple_present:
            print("\nYou found an apple! You eat your snack and go back down.")
            score += 1
            apple_present = False
        elif not apple_present:
            print("\nThere is nothing up here, so you go back down.")

    elif room == 6:
        c = choose("You are at a boat dock. You see a man in a boat " +
                    "about 100 yards away.",
                    [("s", "shout at the man"), ("w", "go west")])
        if c == "w":
            room = 5
        elif c == "s":
            print("\nThe man is your friend searching for you. He rescues " +
                    "you from the island!")
            score += 3
            finished = True
    else:
        print(f"Unknown room number: {room}.")
        exit(1)

print("The game is over. Your score is " + str(score))