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