[Tutor] my newbie program

david din22@cox.net
Tue Nov 26 01:41:02 2002


This is a multi-part message in MIME format.

------=_NextPart_000_0005_01C294E4.85B0B540
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

my program seems to be working. thanks to everyone
for their help. i think it could be cleaned up a little.
or maybe a lot.
my next step is to be able to save the rooms i create.
and current location etc.
is pickle what i want to look at?=20
anyway any comments, suggestions, improvements
much appreciated.=20
-david

import string

class Room:
    def __init__(self):
        self.desc=3D"a nondescript room"
        self.exits=3D[0,0,0,0]

startroom=3DRoom()

class Map:
    def __init__(self):
        self.grid=3D{(0,0):startroom}

map=3DMap()
   =20
class Actor:
    def __init__(self):
        self.location=3Dstartroom
    def act(self):
       =20
        cmd =3D raw_input('>')
        if len(string.split(cmd)) > 2:
            print "too many words"
   =20
        elif len(string.split(cmd)) =3D=3D 2:
            verb=3Dstring.split(cmd)[0]
            dirobj=3Dstring.split(cmd)[1]
        else:
            verb=3Dstring.split(cmd)[0]

        directions=3D{'n':0,'s':1,'e':2,'w':3}
        if cmd in directions.keys():
            if self.location.exits[directions[cmd]]:
                self.location=3Dself.location.exits[directions[cmd]]
                print self.location.desc
            else:
                print "you can't go that way"
        if cmd =3D=3D 'look':
            print self.location.desc
        dirs=3D{'n':(0,1),'s':(0,-1),'e':(1,0),'w':(-1,0)}
        if verb =3D=3D 'dig':
            for a,b in map.grid.items():
                if b =3D=3D self.location:
                    currentroomcoord=3Da
            x,y=3Dcurrentroomcoord
            deltaX,deltaY=3Ddirs[dirobj]
            newcoord=3D(x+deltaX,y+deltaY)
            if self.location.exits[directions[dirobj]]:
                print "nothing to dig"
           =20
            elif map.grid.has_key(newcoord):
                print "already a room there, digging an exit"
                =
self.location.exits[directions[dirobj]]=3Dmap.grid[newcoord]
                if dirobj =3D=3D 'n':
                    back=3D1
                if dirobj =3D=3D 's':
                    back=3D0
                if dirobj =3D=3D 'e':
                    back=3D3
                if dirobj =3D=3D 'w':
                    back=3D2
                =
map.grid[newcoord].exits[back]=3Dmap.grid[currentroomcoord]              =
 =20
               =20
            else:
                map.grid[newcoord]=3DRoom()
           =20
                =
self.location.exits[directions[dirobj]]=3Dmap.grid[newcoord]
                self.location=3Dmap.grid[newcoord]
                print self.location.desc
                if dirobj =3D=3D 'n':
                    back=3D1
                if dirobj =3D=3D 's':
                    back=3D0
                if dirobj =3D=3D 'e':
                    back=3D3
                if dirobj =3D=3D 'w':
                    back=3D2
                =
map.grid[newcoord].exits[back]=3Dmap.grid[currentroomcoord]
        if cmd =3D=3D 'describe':
            self.location.desc=3Draw_input('?')
           =20
me=3DActor()




------=_NextPart_000_0005_01C294E4.85B0B540
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>my program seems to be working. thanks =
to=20
everyone</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>for their help. i think it could be =
cleaned up a=20
little.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>or maybe a lot.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>my next step is to be able to save the =
rooms i=20
create.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>and current location etc.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>is pickle what i want to look at? =
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>anyway any comments, suggestions,=20
improvements</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>much appreciated. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>-david</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>import string</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>class Room:<BR>&nbsp;&nbsp;&nbsp; def=20
__init__(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
self.desc=3D"a=20
nondescript room"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
self.exits=3D[0,0,0,0]</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>startroom=3DRoom()<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>class Map:<BR>&nbsp;&nbsp;&nbsp; def=20
__init__(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
self.grid=3D{(0,0):startroom}</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>map=3DMap()<BR>&nbsp;&nbsp;&nbsp; =
<BR>class=20
Actor:<BR>&nbsp;&nbsp;&nbsp; def=20
__init__(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
self.location=3Dstartroom<BR>&nbsp;&nbsp;&nbsp; def=20
act(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd =3D=20
raw_input('&gt;')<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if=20
len(string.split(cmd)) &gt;=20
2:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
print=20
"too many words"<BR>&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elif =
len(string.split(cmd)) =3D=3D=20
2:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =

verb=3Dstring.split(cmd)[0]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;=20
dirobj=3Dstring.split(cmd)[1]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;=20
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;=20
verb=3Dstring.split(cmd)[0]</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial =
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
directions=3D{'n':0,'s':1,'e':2,'w':3}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;=20
if cmd in=20
directions.keys():<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;=20
if=20
self.location.exits[directions[cmd]]:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
self.location=3Dself.location.exits[directions[cmd]]<BR>&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
=20
print=20
self.location.desc<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;=20
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;=20
print "you can't go that =
way"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if=20
cmd =3D=3D=20
'look':<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;=20
print self.location.desc<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
dirs=3D{'n':(0,1),'s':(0,-1),'e':(1,0),'w':(-1,0)}<BR>&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;=20
if verb =3D=3D=20
'dig':<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; for=20
a,b in=20
map.grid.items():<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
if b =3D=3D=20
self.location:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
currentroomcoord=3Da<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;=20
x,y=3Dcurrentroomcoord<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;=20
deltaX,deltaY=3Ddirs[dirobj]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;=20
newcoord=3D(x+deltaX,y+deltaY)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;=20
if=20
self.location.exits[directions[dirobj]]:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
print "nothing to=20
dig"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
elif=20
map.grid.has_key(newcoord):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
print "already a room there, digging an=20
exit"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;=20
self.location.exits[directions[dirobj]]=3Dmap.grid[newcoord]<BR>&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;=20
if dirobj =3D=3D=20
'n':<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
back=3D1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
if dirobj =3D=3D=20
's':<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
back=3D0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=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&nbsp;&nbsp;&nbsp;=20
back=3D3<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
if dirobj =3D=3D=20
'w':<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
back=3D2<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
map.grid[newcoord].exits[back]=3Dmap.grid[currentroomcoord]<BR>&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
if cmd =3D=3D=20
'describe':<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;=20
self.location.desc=3Draw_input('?')<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>me=3DActor()</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2><BR></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_0005_01C294E4.85B0B540--