2

I've been hacking away on a little game, just for fun, and I've run into a problem. I'll post the code and try my best to explain:

def parseCmd(string):
    cmd = string.split(' ')
    if cmd[0] == 'help':
        showHelp()
    elif cmd[0] == 'add':
        addServer()
    elif cmd[0] == 'bag':
        viewInventory(inventory)
    elif len(cmd) == 1 and cmd[0] == 'look':
        describeRoom()
    elif len(cmd) == 1 and cmd[0] == 'take':
        print 'What do you want me to take?'
    elif cmd[0] == 'take':
        pickUp(cmd[1],  items)
    elif cmd[0] == 'exit':
        sys.exit(0)
    else:
        print 'I don\'t know how to '  + cmd[0]

def describeRoom():
    print locations[player_location]

def pickUp(item,  item_list):
    if item in item_list[player_location]:
        item_list[player_location].remove(item)
        inventory.append(item)
        print 'You took the ' + item        
    else:
        print 'I can\'t find any ' + item

inventory = ['id card',  'money',  'keys']
player_location = 'cookieroom'
items = {'cookieroom': ['crowbar',  'hammer']}
locations = {'cookieroom': 'The cookieroom, where all the hard work gets done. \n\nNORTH: LFA - ITEMS: %s' % items[player_location], 
                'LFA': 'The infamous LFA, where dreams of office supplies become reality. there is a big guy sleeping in his chair next to a fire extinguisher.\n\nSOUTH: Cookieroom, WEST: WC'}

if __name__ == "__main__":
    while 1:
        t = raw_input('-> ')
        parseCmd(t)

So, as you can see I want the list of items in the items dictionary to change when you pick up an item available in that specific room. I can pick up the item and it gets added to my inventory but if I issue the command 'look' it shows the list of items in it's original state.

I've been googling and stackoverflowing for 1½ day now and I can't find anything that seems to solve this problem.

If something is unclear, just ask me and I'll try to answer.

0

1 Answer 1

4

The locations dictionary, which is from where the describeRoom function picks up its room description, is initialised once when the program starts. At that time, the location of the player is the cookieroom and the objects there are the crowbar and the hammer. So, a string is created like so

'The cookieroom, where all the hard work gets done. \n\nNORTH: LFA - ITEMS: ["crowbar", "hammer"]'

This string never changes even if you later alter the contents of the items dictionary.

Your locations dictionary should only contain the non changing part of the room description. The changing part (e.g. the list of items in the room etc.) should be recomputed everytime the users requests the description of the room.

Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thank you Noufal. I am not able to upvote this though since I don't have enough reputation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.