[Tutor] results not quite 100 percent yet
bhaaluu
bhaaluu at gmail.com
Wed Jan 30 21:14:17 CET 2008
On Jan 30, 2008 2:24 PM, bob gailer <bgailer at alum.rpi.edu> wrote:
> bhaaluu wrote:
> > # N S E W U D T
> > travelTable=[[0,2,0,0,0,0,0], # ROOM 1
> > [1,3,3,0,0,0,0], # ROOM 2
> It is good to finally see that you are building an adventure game.
>
> Consider creating a instance of a Room class for each room and saving
> them in a collection such as a list.
>
> This will give you much more flexibility as your game grows.
WooHoo! YES! This is what I started out wanting to do, as an exercise
in learning Python Object Oriented Programming (POOP). I thought
a Text Adventure Game would be a perfect learning tool because
it seems to have objects that model the real world (or a fantasy world).
There is the Explorer, a world (The Castle), Rooms to explore, objects
in the rooms, like Treasure to be picked-up, and Monsters to fight.
However, not having any experience with Text Adventure Games has
been a real bummer for me as far as designing POOP classes. So I
fell back on procedural programming in order to learn more about
TAGs, so I'd have a better idea about how to design the class in POOP.
>
> Inevitably I wound up doing a bunch of things more "Pythonically" so
> there may be stuff here you don't relate to yet. But is is all worth
> studying and will save you hours of headache later.
>
I really appreciate source code. I probably learn faster by modifying
and running source code than anything else, unless it is a step-by-step
tutorial that also has source code that can be modified and run! 8^D
> ----------------------------- code -----------------------------
> import random
> class Room:
> roomNo = 0
> def __init__(self, destinations, updatable=True):
> Room.roomNo += 1
> self.roomNo = Room.roomNo
> self.destinations = destinations
> # store treasure apaart from destinations
> self.treasure = 0 # add intial treasure
> self.updatable = updatable # may have the treasure updated
> def updateTreasure(self, treasure):
> self.treasure = treasure
> def __repr__(self):
> return " %s:%s" % (self.roomNo, self.treasure)
>
> rooms = [
> Room([0,2,0,0,0,0]), # ROOM 1
> Room([1,3,3,0,0,0]), # ROOM 2
> Room([2,0,5,2,0,0]), # ROOM 3
> Room([0,5,0,0,0,0]), # ROOM 4
> Room([4,0,0,3,5,13]), # ROOM 5
> Room([0,0,1,0,0,0], False), # ROOM 6 flagged as not updatable
> Room([0,8,0,0,0,0]), # ROOM 7
> Room([7,0,0,0,0,0]), # ROOM 8
> Room([0,9,0,0,0,8]), # ROOM 9
> # etc for the rest of the rooms -
> ]
> # note I omitted the initial treasure value since it is always 0
> # I modified the last for statement to account for only 9 rooms
>
> # use random.sample to create random subsets of values and rooms
> # - eliminates all the loops and tests
> # note this ensures no duplicate treasures (do you want that?)
>
> # create list of 8 random treasure values
> values = random.sample(range(10,110),4) + random.sample(range(-4, 0),4)
>
> # create list of 8 randomly selected updatable rooms
> roomsToUpdate= random.sample([room for room in rooms if room.updatable], 8)
>
> # update the rooms' Treasures
> for room, value in zip(roomsToUpdate, values):
> room.updateTreasure(value)
>
> a = range(1,99)
> for room in (3,5):
> rooms[room].updateTreasure(100 + random.choice(a))
>
> for room in rooms: print room
> ----------------------------- end code -----------------------------
I'll copy/paste this into an editor, and see if I can make it do anything.
Thank you!
>
> Things I did not do, but suggest:
> - store the room destinations in a text file rather than hard-coding
> them in the program. It is almost always a good idea to separate logic
> from data.
Right now, my procedural Python program has a main.py, data.py and
action.py. After Kent told me about using a nested sequence structure
instead of a dictionary, all I had to do was edit data.py and change
the table from a dictionary to a list of lists, then edit the other two
files and do a simple search and destroy to get rid of the " .values() ".
It really didn't take as long as I thought it would to change over.
>
> -create a Treasure class, storing instances directly in roo,s rather
> than indexes, and storing treasure definitions in the text file.
>
> -store room instances in the destinations rather than indexes.
>
> At this point you no longer need indexes!
>
> --
> Bob Gailer
> 919-636-4239 Chapel Hill, NC
Thanks again for the POOP!
Happy Programming!
--
b h a a l u u at g m a i l dot c o m
"You assist an evil system most effectively by obeying its
orders and decrees. An evil system never deserves such
allegiance. Allegiance to it means partaking of the evil.
A good person will resist an evil system with his or her
whole soul." [Mahatma Gandhi]
More information about the Tutor
mailing list