Re: [Tutor] my text adventure, saving and restoring

2005-12-04 Thread david
thanks that was very helpful. i added all that stuff because i was 
trying to figure out some way of getting at the descriptions and exits.
that is the part i'm stuck on. but knowing that world is all i need is 
good because i can focus my efforts better. thanks.

> You could make your save() method a lot simpler; something like this:
> 
>def save(self):
>f = open('savefile', 'w')
>pickle.dump(world,f)
>f.close()
> 
> This is all you need, because 'world' contains all the information
> about your world.  Then, when you load the data back, you will need to
> figure out some way of building your 'rooms' data structure from
> 'world'.
> 
> Does this help?
> 
> --
> John.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] my text adventure, saving and restoring

2005-12-04 Thread John Fouhy
On 05/12/05, david <[EMAIL PROTECTED]> wrote:
> when i restore from the pickle i can see my exits and descriptions are still
> there.
> def save(self):
> f = open('savefile', 'w')
> pickle.dump(world,f)
> pickle.dump(rooms,f)
> for i in rooms:
> pickle.dump(i,f)
> f.close()

Hi David,

You appear to be saving your information multiple times.

'world' is a dictionary with Room instances as its values.  Those same
Room instances occur in 'rooms'.  So, when you run your save method,
python does this:
 1. Write information to the pickle file telling python to construct a
dictionary.  This includes telling python how to construct the Room
instances.
 2. Write information to the pickle file telling python to construct a
list.  This includes telling python how to construct the Room
instances.
 3. For each Room instance, write information to the pickle file
telling python how to construct it.

Now, when you unpickle the information, python does this:
 1. Build a dictionary to be the world.  Build all the Rooms (because
the Rooms were the values of this dictionary).
 2. Build a list of Rooms.  Build all the Rooms (because the Rooms
were the contents of this list).
 3. Build all the Rooms again, and do nothing with them.

Initially, your Room isntances in world were the same as the Rooms in
rooms.  But after unpickling, python builds the Rooms separately for
each data structure, and so they end up different.

Let me try some ASCII art:
[view this in a monospaced font, eg, by cutting-and-pasting to Notepad]

Before:

world: (0,0) |-> Room0; (0,1) |-> Room1; (1,1) |-> Room2
  /--//---/  /--/
 /   /  /
 /--\/--\/--\
 | Room || Room || Room |
 |object||object||object|
 \--/\--/\--/
   //   /---//--/
rooms: [ RoomA,   RoomB,   RoomC ]

After:

world: (0,0) |-> Room0; (0,1) |-> Room1; (1,1) |-> Room2
  /--//---/  /--/
 /   /  /
 /--\/--\/--\
 | Room || Room || Room |
 |object||object||object|
 \--/\--/\--/

rooms: [ RoomA,   RoomB,   RoomC ]
   \\   \---\\--\
 /--\/--\/--\
 | Room || Room || Room |
 |object||object||object|
 \--/\--/\--/

You could make your save() method a lot simpler; something like this:

def save(self):
f = open('savefile', 'w')
pickle.dump(world,f)
f.close()

This is all you need, because 'world' contains all the information
about your world.  Then, when you load the data back, you will need to
figure out some way of building your 'rooms' data structure from
'world'.

Does this help?

--
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] my text adventure, saving and restoring

2005-12-04 Thread david



when i restore from the pickle i can see my exits 
and descriptions are still there.
but look won't see the description and move can't 
move to the next room.
i am stumped. what is going on here? how can i fix 
it? please help?
 
 
 
IDLE 1.1.2   No 
Subprocess >>> >rooms[<__main__.Room instance at 
0x00E08EB8>]
 
{}>world{(0, 0): <__main__.Room 
instance at 0x00E08EB8>}>dr>>startroom>l[0, 
0]startroom>dig n(0, 
1)>n>dr>>nextroom>rooms[<__main__.Room 
instance at 0x00E08EB8>, <__main__.Room instance at 
0x00E7BFD0>]startroom{'n': <__main__.Room instance at 
0x00E7BFD0>}nextroom{'s': <__main__.Room instance at 
0x00E08EB8>}>world{(0, 1): <__main__.Room instance at 
0x00E7BFD0>, (0, 0): <__main__.Room instance at 
0x00E08EB8>}>save>quitTraceback (most recent call 
last):  File "C:\Documents and Settings\david\Desktop\t.py", line 150, 
in ?    p.do()  File "C:\Documents and 
Settings\david\Desktop\t.py", line 93, in do    
sys.exit()SystemExit>>> >rooms[<__main__.Room 
instance at 0x00E13F30>, <__main__.Room instance at 
0x00E13698>]startroom{'n': <__main__.Room instance at 
0x00E13698>}nextroom{'s': <__main__.Room instance at 
0x00E13F30>}>world{(0, 1): <__main__.Room instance at 
0x00E13FA8>, (0, 0): <__main__.Room instance at 
0x00E132B0>}>nalas, you cannot go that way>l[0, 
0]
 
>import sysimport stringimport 
pickleimport os.path
 
world = {}rooms = []class 
Room:    def 
__init__(self,coords):    
self.contents = []    
self.description = ''    self.coords 
= coords    world[tuple(coords)] = 
self    
rooms.append(self)    self.exits = 
{}    def 
nextdoor(self,direction):    if 
direction == 
'n':    
nextdoor =  (self.coords[0], self.coords[1] + 
1)    return 
list(nextdoor)    elif direction == 
's':    
nextdoor =  list((self.coords[0], self.coords[1] - 
1))    return 
nextdoor    elif direction == 
'e':    
nextdoor =  list((self.coords[0] +1, 
self.coords[1]))    
return nextdoor    elif direction == 
'w':    
nextdoor =  (self.coords[0] -1, 
self.coords[1])    
return list(nextdoor)    
 
class Player:    def 
__init__(self,name):    self.name = 
name    self.location = 
None    self.inventory = 
[]    self.wielded = 
None    def 
look(self):    print 
self.location.coords    print 
self.location.description
 
    def 
move(self,direction):    
type(direction)    if 
self.location.exits.has_key(direction):    
self.location = 
self.location.exits[direction]    
else:    
print 'alas, you cannot go that way'    def 
wield(self,what):    self.wielded = 
what    def 
wear(self,what):    
pass    def 
take(self,what):    
pass    def 
drop(self,what):    
pass    def 
dig(self,direction):    target = 
tuple(self.location.nextdoor(direction))    
print target    if 
self.location.exits.has_key(target):    
print 'there is already an exit to that 
room'    elif 
world.has_key(target):    
print 'already a room there, attempt to make 
exits'   
    
self.location.exits[direction] = 
world[target]   
    
world[target].exits[opdir(direction)] = 
self.location    
    
else:    
world[target]=Room(target)    
self.location.exits[direction] = 
world[target]    
world[target].exits[opdir(direction)] = 
self.location 
    def 
describeroom(self):    
self.location.description = raw_input('>>')    def 
save(self):    f = open('savefile', 
'w')    
pickle.dump(world,f)    
pickle.dump(rooms,f)    for i in 
rooms:    
pickle.dump(i,f)   
    f.close()    
def do(self):    cmd = 
string.split(raw_input('>'))    
verb = cmd[0]    if len(cmd) > 
1:    target 
= cmd[1]    
    if verb == 
'l':    
self.look()    elif verb in 
['n','s','e','w']:   
    
self.move(verb)    elif verb == 
'quit':    
sys.exit()    elif verb == 
'i':    for a 
in 
self.inventory:    
print a.name    elif verb == 
'dig':    
self.dig(target)    elif verb == 
'dr':    
self.describeroom()    elif verb == 
'save':    
self.save()    elif verb == 
'world':    
print world    elif verb == 
'rooms':    
print 
rooms    for 
i in 
rooms:    
print 
i.description    
print i.exits    
else:    
print 'what?'
 
class Thing:    def 
__init__(self,name):    self.name = 
name
 
def opdir(direction):    if direction == 
'n':    return 
's'    if direction == 
's':    return 
'n'    if direction == 
'e':    return 
'w'    if direction == 
'w':    return 
'e'        p = 
Player('david')room1 = Room([0,0])
 
p.location = room1sword = Thing('sword')hat = 
Thing('hat')p.inventory.append(sword)p.inventory.append(ha