# Modify the program from the Second Dictionary challenge of lecture 56
# to use shelves instead of dictionaries.
#
# Do this by creating two programs. cave_initialise.py should create the two
# shelves (locations and vocabulary) with the appropriate keys and values.
#
# cave_game.py will then use the two shelves instead of dictionaries.
# Apart from opening and closing the shelves, cave_game will need only
# two changes to the actual code - remember that shelf keys MUST be strings!
#
# Just to be clear, cave_game.py will contain the code from line 45, everything
# before that (modified to use shelves) will be in cave_initialise.py.

import shelve

with shelve.open('books') as books:

    loc = 1
    while True:
        availableExits = ", ".join(books['locations'][loc]["exits"].keys())

        print(books['locations'][loc]["desc"])

        if loc == 0:
            break
        else:
            allExits = books['locations'][loc]["exits"].copy()
            allExits.update(books['locations'][loc]["namedExits"])

        direction = input("Available exits are " + availableExits).upper()
        print()

        # Parse the user input, using our vocabulary dictionary if necessary
        if len(direction) > 1:  # more than 1 letter, so check vocab
            words = direction.split()
            for word in words:
                if word in books['vocabulary']:   # does it contain a word we know?
                    direction = books['vocabulary'][word]
                    break

        if direction in allExits:
            loc = allExits[direction]
        else:
            print("You cannot go in that direction")
