[Tutor] Picking up Item()s
alan.gauld@bt.com
alan.gauld@bt.com
Mon, 18 Feb 2002 10:47:24 -0000
> class Room:...
>
> class Item:...
>
> areas = ['porch', 'foyer', 'bathroom', 'mid_stairs', 'living',
> 'hallway']
> rooms = [] # this is a list, can I suggest a dictionary:
> for n in areas:
> rooms.append(Room(n))
rooms[n] = Room(n)
> rooms[0].createExits({'e':1})
....
> rooms[5].createExits({'e':4, 'n':2, 's':3, 'w':1})
And can i suggest you put all the data build in one place so the areas table
becomes a list of tuples, something like:
> areas = [('porch',{'e':4}), ...
> ('hallway', {'e':4, 'n':2, 's':3, 'w':1})]
Then the above lines become:
rooms[n[0]] = Room(n[0])
rooms[n[0]].createExits(n[1])
> rooms[0].itemsInRooms(things[0])
> rooms[1].itemsInRooms(things[1])
The dictionary nbotation isa more readable here too:
rooms['porch'].itemsInRooms(things[0])
etc
> playLoc = 0
playloc = 'porch' #or areas[0] if you prefer
> words = ['n','s','e','w','q','get']
> command = []
>
> while 1:
> command = string.split(raw_input("--> "))
> if command[0] not in words:
> print "Come again?"
> else:
> if command[0] == 'q':
> sys.exit(1)
> elif command[0] in ['n','s','e','w']:
> if rooms[playLoc].exits.has_key(command[0]):
> playLoc = rooms[playLoc].exits.get(command[0])
To cater for dictionaries either change the exits dictionary
(my favourite) or do
playLoc = areas[rooms[playLoc].exits.get(command[0])]
None of which answers your actual question of course but might
make the code a little more readable and debuggable...
> print "You find yourself in the " +
> rooms[playLoc].name
> print "You can move ", rooms[playLoc].exits.keys()
> if len(rooms[playLoc].items) != 0:
> print "This room contains ", rooms[playLoc].items
Here's the cuprit. items is a list of Item instances.
You are printing an object, you need a method of Items to print
in a friendly way - maybe providing a __str__() method would suffice?
Or something that specifically returns a string - show() maybe?.
Alan g.