Help (I can't think of a better title)

MRAB python at mrabarnett.plus.com
Sat May 22 20:39:25 EDT 2010


Lanny wrote:
> The answer may be right infront of me but I really can't figure this
> out.
> I'm trying to build a interactive fiction kind of game, silly I know
> but I
> am a fan of the genre. I'm trying to build up an index of all the
> rooms in
> the game from an outside file called roomlist.txt. The only problem is
> that
> every room ends up having four exits. Here's the code.
> 
> 
> class room() :
>     room_id = 'room_id'
>     name = 'room'
>     description = 'description'
>     item_list =
>     exits = {}
 >     visits = 0

These attributes are being defined as belonging to the class, so they
will be shared by all the instances of the class. This isn't a problem
for immutable items such as strings, but is for mutable items such as
dicts. In short, all the rooms share the same 'exits' dict.

You should really define the instance attributes (variables) in the
'__init__' method.

>     def leave(self, direction)
>         global roomlist
>         global player_current_room
>         if direction not in player_current_room.exitskeys() :
>             print 'There is no exit in that direction.'
>             return 1
>         roomlist[self.room_id] = self
>         player_current_room =
> roomlist[player_current_room.exits[direction]]
>         print player_current_room.name
>         if player_current_room.visits < 1 :
>             print player_current_room.description
>             if player_current_room.item_list != [] :
>                 stdout.write('You can see ')
>                 for item in player_current_room.item_list :
>                     stdout.write('a')
>                     if item.display_name[0] in ['a','e','i','o','u'] :
>                                  stdout.write('n ' + item.display_name
> +
> ',')
>                     else :
>                         stdout.write(item.display_name + ',')
>                         pass
> 
>                 print
>             print 'Exits:',
>             for way in player_current_room.exits :
>                 print way.capitalize(),
>             print
>             player_current_room.visits += 1
>             pass
>         else :
>             player_current_room.visits += 1
>             pass
>         pass
> 
> def build_rooms(room_file) :
>     global roomlist
>     rfile = open(room_file)
>     tmp_builder = ''
>     for line in rfile :
>         tmp_builder = tmp_builder + line[:-1]
>         pass
>     for location in tmp_builder.rsplit('::') :
>         if location.rsplit(';')[-1] == '' :
>             location = location[:-1]
>         if len(location.rsplit(';')) != 5 :
>             if '-dev' or '-v' in argv :
>                 print location.rsplit(';')[0], 'had',
> len(location.rsplit(';')), 'values in it, 5 expected'
>                 for value in location.rsplit(';') :
>                     print; print value
>                     foobar.append(value)
>                 print 'A room failed to initalize due to either too
> much or
> not enough values being present in the build file'
>                 pass
>             pass
>         else :
>             roomlist[location.rsplit(';')[0]] = room()
>             roomlist[location.rsplit(';')[0]].room_id =
> location.rsplit(';')[0]
>             roomlist[location.rsplit(';')[0]].name =
> location.rsplit(';')[1]
>             roomlist[location.rsplit(';')[0]].description =
> location.rsplit(';')[2]
>             if location.rsplit(';')[3] != 'none' :
>                 pass
>             tmp_var = location.rsplit(';')[4]
>             print location.rsplit(';')[0],
> roomlist[location.rsplit(';')[0]].exits, 'before'
>             for way in tmp_var.rsplit(',') :
>                 roomlist[location.rsplit(';')[0]].exits[way.rsplit(':')
> [0]]
> = way.rsplit(':')[1]
> 
> roomlist = {}
> build_rooms('room_list.txt')
> 
> And here is the roomlist.txt file :
> 
> start_room;
> Starting Room;
> This is the starting room, if you can read this text it means that at
> least
> one part of this beta is working.;
> none;
> north:second_room,west:aux_room;
> ::
> second_room;
> Second Room;
> Yo, Yo! This is the second room, if you can see this text a
> substantitally
> greater amount of the game is running than would have been if you
> didn't see
> this text.;
> apple;
> south:start_room;
> ::
> aux_room;
> Auxillary Room;
> No, there aren't any barbarian conscripts here, but there is a table!;
> none;
> east:start_room;
> 
> Ideally roomlist['start_room'].exits would equal {'aux_room' :
> 'west',
> 'second_room' : 'north'} but it doesn't. Sorry if this is unclear or
> too
> long, but I'm really stumped why it is giving bad output




More information about the Python-list mailing list