[Tutor] Python information
William Griffin
WILLIAM.GRIFFIN@asu.edu
Fri, 02 Aug 2002 09:52:28 -0700
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
--Boundary_(ID_HPDIycDfCuX9BiByMooKLQ)
Content-type: multipart/alternative;
boundary="Boundary_(ID_YVe2VmL/oLoD8N3w+7F78g)"
--Boundary_(ID_YVe2VmL/oLoD8N3w+7F78g)
Content-type: text/plain; charset="iso-8859-1"
Like the responders on this thread, I had a difficult time grasping OOP and
faced with the task of trying to write a simulation of children playing and
forming small groups, I looked at as many existing simulation programs and
games as I could find. Fortunately, I found several that helped:
http://sourceforge.net/projects/probot/
and
http://www.pythonpros.com/gstein/war/
and
http://www.liacs.nl/~jjacob/
and finally, monsters.py (I forgot how I got it or who the author is, but it
is very good start); it is attached.
As Alan mentioned in another post, either you get it or it is a slow
process; I've found myself in the slow process group although I've been able
to build the simulation -- but its not pretty.
Anyway, there is some code in these modules that should provide a basis for
building a newbie game.
bill
-----Original Message-----
From: Rob [mailto:rob@uselesspython.com]
Sent: Friday, August 02, 2002 6:06 AM
To: 'Tutor@Python. Org'
Subject: RE: [Tutor] Python information
I have been thinking of doing something like this. However, my current skill
level would only enable me to be a part of a development team on such a
project.
I also think that embedding a python/jython interpreter in a training game
would give this project an edge unmatched by anything else I've seen out
there.
Rob
http://uselesspython.com
> -----Original Message-----
> From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
> shey crompton
> Sent: Friday, August 02, 2002 3:31 AM
> To: 'tutor@python.org'
> Subject: RE: [Tutor] Python information
>
>
> Hi Rob et al,
> A python Robocode game sounds like a great idea for a newbie like
> me. I wish
> I had the knowhow to write a game like that as it would probably be quite
> beneficial to complete newbies, and programmers checking out Python.
> One initial idea would be to have a tutorial that takes you through each
> part of the robot code. For example, changing the hit points on
> the enemies
> (newbie), adding a bigger weapon with graphics etc to your own robot
> (intermediate), and rewriting the AI for the enemies (expert).
> I am currently going through Alan Gauld's book and How to Think Like a
> Computer Scientist. I am setting myself little goals and trying
> to find out
> how to achieve them with the knowledge I have. I feel having a game to
> adjust the attributes in the code would help all round to show
> off Python's
> versatility, while helping people to learn how to program.
> I am interested to hear what people on this list feel about my idea. As I
> said, I don't have the knowledge (Yet!) to make the game, but I would like
> to give it a go one day, or help someone with the design, and/or
> testing of
> such a program.
>
> I think I'll stop now. :-)
>
> Shey
>
> -----Original Message-----
> From: Rob [mailto:rob@uselesspython.com]
> Sent: 02 August 2002 04:23
> To: 'Tutor@Python. Org'
> Subject: RE: [Tutor] Python information
>
> OOP does seem to be an interesting situation. We think in objects all day
> long, and yet when it comes time to program along the same lines, struggle
> seems common.
>
> Perhaps we should put our collective heads together and create a Python
> equivalent to Robocode, which is designed to teach OOP in Java by
> having you
> program battling robot tanks.
>
> Let's say you want to write a "Dungeons & Dragons"-type game. You know
> you'll need it to have certain things (objects). You make a list
> of some of
> them:
>
> -monsters
> -dungeons
> -non-player characters
> -player characters
> -weapons
> -treasure
> -equipment
> -etc.
>
> Some of these can be lumped into more general categories. For instance,
> non-player characters and player characters can both fall under
> the category
> (class), which can just be called "character". All characters can possess
> certain characteristics in common, such as:
>
> -amount of damage that may be survived
> -height
> -weight
> -name
> -species
> -vocation
> -inventory
> -etc.
>
> The process goes on and on, really. The idea is to think of what kind of
> objects you will want/need in your program, create classes for these
> objects, and then create instances of these classes. A class
> instance can be
> "Banglor the Elf", a specific instance of class character.
>
> Objects have two general things the programmer needs to fuss over:
> attributes and behavior. Attributes can be the character's name,
> hair color,
> fondness for dirty poetry, etc. and behaviors can be throwing spears,
> running, singing, sleeping, and stopping going forward when bumping into
> walls.
>
> Does any of this sort of discussion help at all?
>
> Rob (who should really be studying for tomorrow's C++ final ;-)
> http://uselesspython.com
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
--Boundary_(ID_YVe2VmL/oLoD8N3w+7F78g)
Content-type: text/html; charset="iso-8859-1"
Content-transfer-encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2655.35">
<TITLE>RE: [Tutor] Python information</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=3D2>Like the responders on this thread, I had a difficult =
time grasping OOP and faced with the task of trying to write a =
simulation of children playing and forming small groups, I looked at as =
many existing simulation programs and games as I could find. =
Fortunately, I found several that helped:</FONT></P>
<P><FONT SIZE=3D2><A HREF=3D"http://sourceforge.net/projects/probot/" =
TARGET=3D"_blank">http://sourceforge.net/projects/probot/</A></FONT>
</P>
<P><FONT SIZE=3D2>and</FONT>
</P>
<P><FONT SIZE=3D2><A HREF=3D"http://www.pythonpros.com/gstein/war/" =
TARGET=3D"_blank">http://www.pythonpros.com/gstein/war/</A></FONT>
</P>
<P><FONT SIZE=3D2>and </FONT>
</P>
<P><FONT SIZE=3D2><A HREF=3D"http://www.liacs.nl/~jjacob/" =
TARGET=3D"_blank">http://www.liacs.nl/~jjacob/</A></FONT>
</P>
<P><FONT SIZE=3D2>and finally, monsters.py (I forgot how I got it or =
who the author is, but it is very good start); it is attached. </FONT>
</P>
<P><FONT SIZE=3D2>As Alan mentioned in another post, either you get it =
or it is a slow process; I've found myself in the slow process group =
although I've been able to build the simulation -- but its not =
pretty.</FONT></P>
<P><FONT SIZE=3D2>Anyway, there is some code in these modules that =
should provide a basis for building a newbie game.</FONT>
</P>
<P><FONT SIZE=3D2>bill</FONT>
</P>
<BR>
<BR>
<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: Rob [<A =
HREF=3D"mailto:rob@uselesspython.com">mailto:rob@uselesspython.com</A>]<=
/FONT>
<BR><FONT SIZE=3D2>Sent: Friday, August 02, 2002 6:06 AM</FONT>
<BR><FONT SIZE=3D2>To: 'Tutor@Python. Org'</FONT>
<BR><FONT SIZE=3D2>Subject: RE: [Tutor] Python information</FONT>
</P>
<BR>
<P><FONT SIZE=3D2>I have been thinking of doing something like this. =
However, my current skill</FONT>
<BR><FONT SIZE=3D2>level would only enable me to be a part of a =
development team on such a</FONT>
<BR><FONT SIZE=3D2>project.</FONT>
</P>
<P><FONT SIZE=3D2>I also think that embedding a python/jython =
interpreter in a training game</FONT>
<BR><FONT SIZE=3D2>would give this project an edge unmatched by =
anything else I've seen out</FONT>
<BR><FONT SIZE=3D2>there.</FONT>
</P>
<P><FONT SIZE=3D2>Rob</FONT>
<BR><FONT SIZE=3D2><A HREF=3D"http://uselesspython.com" =
TARGET=3D"_blank">http://uselesspython.com</A></FONT>
</P>
<P><FONT SIZE=3D2>> -----Original Message-----</FONT>
<BR><FONT SIZE=3D2>> From: tutor-admin@python.org [<A =
HREF=3D"mailto:tutor-admin@python.org">mailto:tutor-admin@python.org</A>=
]On Behalf Of</FONT>
<BR><FONT SIZE=3D2>> shey crompton</FONT>
<BR><FONT SIZE=3D2>> Sent: Friday, August 02, 2002 3:31 AM</FONT>
<BR><FONT SIZE=3D2>> To: 'tutor@python.org'</FONT>
<BR><FONT SIZE=3D2>> Subject: RE: [Tutor] Python information</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> Hi Rob et al,</FONT>
<BR><FONT SIZE=3D2>> A python Robocode game sounds like a great idea =
for a newbie like</FONT>
<BR><FONT SIZE=3D2>> me. I wish</FONT>
<BR><FONT SIZE=3D2>> I had the knowhow to write a game like that as =
it would probably be quite</FONT>
<BR><FONT SIZE=3D2>> beneficial to complete newbies, and programmers =
checking out Python.</FONT>
<BR><FONT SIZE=3D2>> One initial idea would be to have a tutorial =
that takes you through each</FONT>
<BR><FONT SIZE=3D2>> part of the robot code. For example, changing =
the hit points on</FONT>
<BR><FONT SIZE=3D2>> the enemies</FONT>
<BR><FONT SIZE=3D2>> (newbie), adding a bigger weapon with graphics =
etc to your own robot</FONT>
<BR><FONT SIZE=3D2>> (intermediate), and rewriting the AI for the =
enemies (expert).</FONT>
<BR><FONT SIZE=3D2>> I am currently going through Alan Gauld's book =
and How to Think Like a</FONT>
<BR><FONT SIZE=3D2>> Computer Scientist. I am setting myself little =
goals and trying</FONT>
<BR><FONT SIZE=3D2>> to find out</FONT>
<BR><FONT SIZE=3D2>> how to achieve them with the knowledge I have. =
I feel having a game to</FONT>
<BR><FONT SIZE=3D2>> adjust the attributes in the code would help =
all round to show</FONT>
<BR><FONT SIZE=3D2>> off Python's</FONT>
<BR><FONT SIZE=3D2>> versatility, while helping people to learn how =
to program.</FONT>
<BR><FONT SIZE=3D2>> I am interested to hear what people on this =
list feel about my idea. As I</FONT>
<BR><FONT SIZE=3D2>> said, I don't have the knowledge (Yet!) to make =
the game, but I would like</FONT>
<BR><FONT SIZE=3D2>> to give it a go one day, or help someone with =
the design, and/or</FONT>
<BR><FONT SIZE=3D2>> testing of</FONT>
<BR><FONT SIZE=3D2>> such a program.</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> I think I'll stop now. :-)</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> Shey</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> -----Original Message-----</FONT>
<BR><FONT SIZE=3D2>> From: =
Rob [<A =
HREF=3D"mailto:rob@uselesspython.com">mailto:rob@uselesspython.com</A>]<=
/FONT>
<BR><FONT SIZE=3D2>> Sent: 02 August 2002 04:23</FONT>
<BR><FONT SIZE=3D2>> To: 'Tutor@Python. Org'</FONT>
<BR><FONT SIZE=3D2>> Subject: RE: =
[Tutor] Python information</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> OOP does seem to be an interesting situation. =
We think in objects all day</FONT>
<BR><FONT SIZE=3D2>> long, and yet when it comes time to program =
along the same lines, struggle</FONT>
<BR><FONT SIZE=3D2>> seems common.</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> Perhaps we should put our collective heads =
together and create a Python</FONT>
<BR><FONT SIZE=3D2>> equivalent to Robocode, which is designed to =
teach OOP in Java by</FONT>
<BR><FONT SIZE=3D2>> having you</FONT>
<BR><FONT SIZE=3D2>> program battling robot tanks.</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> Let's say you want to write a "Dungeons =
& Dragons"-type game. You know</FONT>
<BR><FONT SIZE=3D2>> you'll need it to have certain things =
(objects). You make a list</FONT>
<BR><FONT SIZE=3D2>> of some of</FONT>
<BR><FONT SIZE=3D2>> them:</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> -monsters</FONT>
<BR><FONT SIZE=3D2>> -dungeons</FONT>
<BR><FONT SIZE=3D2>> -non-player characters</FONT>
<BR><FONT SIZE=3D2>> -player characters</FONT>
<BR><FONT SIZE=3D2>> -weapons</FONT>
<BR><FONT SIZE=3D2>> -treasure</FONT>
<BR><FONT SIZE=3D2>> -equipment</FONT>
<BR><FONT SIZE=3D2>> -etc.</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> Some of these can be lumped into more general =
categories. For instance,</FONT>
<BR><FONT SIZE=3D2>> non-player characters and player characters can =
both fall under</FONT>
<BR><FONT SIZE=3D2>> the category</FONT>
<BR><FONT SIZE=3D2>> (class), which can just be called =
"character". All characters can possess</FONT>
<BR><FONT SIZE=3D2>> certain characteristics in common, such =
as:</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> -amount of damage that may be survived</FONT>
<BR><FONT SIZE=3D2>> -height</FONT>
<BR><FONT SIZE=3D2>> -weight</FONT>
<BR><FONT SIZE=3D2>> -name</FONT>
<BR><FONT SIZE=3D2>> -species</FONT>
<BR><FONT SIZE=3D2>> -vocation</FONT>
<BR><FONT SIZE=3D2>> -inventory</FONT>
<BR><FONT SIZE=3D2>> -etc.</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> The process goes on and on, really. The idea is =
to think of what kind of</FONT>
<BR><FONT SIZE=3D2>> objects you will want/need in your program, =
create classes for these</FONT>
<BR><FONT SIZE=3D2>> objects, and then create instances of these =
classes. A class</FONT>
<BR><FONT SIZE=3D2>> instance can be</FONT>
<BR><FONT SIZE=3D2>> "Banglor the Elf", a specific =
instance of class character.</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> Objects have two general things the programmer =
needs to fuss over:</FONT>
<BR><FONT SIZE=3D2>> attributes and behavior. Attributes can be the =
character's name,</FONT>
<BR><FONT SIZE=3D2>> hair color,</FONT>
<BR><FONT SIZE=3D2>> fondness for dirty poetry, etc. and behaviors =
can be throwing spears,</FONT>
<BR><FONT SIZE=3D2>> running, singing, sleeping, and stopping going =
forward when bumping into</FONT>
<BR><FONT SIZE=3D2>> walls.</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> Does any of this sort of discussion help at =
all?</FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> Rob (who should really be studying for =
tomorrow's C++ final ;-)</FONT>
<BR><FONT SIZE=3D2>> <A HREF=3D"http://uselesspython.com" =
TARGET=3D"_blank">http://uselesspython.com</A></FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>></FONT>
<BR><FONT SIZE=3D2>> =
_______________________________________________</FONT>
<BR><FONT SIZE=3D2>> Tutor maillist - =
Tutor@python.org</FONT>
<BR><FONT SIZE=3D2>> <A =
HREF=3D"http://mail.python.org/mailman/listinfo/tutor" =
TARGET=3D"_blank">http://mail.python.org/mailman/listinfo/tutor</A></FON=
T>
<BR><FONT SIZE=3D2>></FONT>
</P>
<BR>
<BR>
<P><FONT =
SIZE=3D2>_______________________________________________</FONT>
<BR><FONT SIZE=3D2>Tutor maillist - Tutor@python.org</FONT>
<BR><FONT SIZE=3D2><A =
HREF=3D"http://mail.python.org/mailman/listinfo/tutor" =
TARGET=3D"_blank">http://mail.python.org/mailman/listinfo/tutor</A></FON=
T>
</P>
<P><FONT FACE=3D"Arial" SIZE=3D2 COLOR=3D"#000000"></FONT>
</BODY>
</HTML>=
--Boundary_(ID_YVe2VmL/oLoD8N3w+7F78g)--
--Boundary_(ID_HPDIycDfCuX9BiByMooKLQ)
Content-type: application/octet-stream; name=monsters.py
Content-transfer-encoding: QUOTED-PRINTABLE
Content-disposition: attachment; filename=monsters.py
"""This prototype shows how different monsters are driven by their ne=
eds=0A(hunger, tiredness, aggression, some special needs).=0A=0AThe a=
ctions are still dummies (just printing to the screen what the=0Amons=
ter is supposed to do), but one can see patterns emerge: sleep,=0Aeat=
, fight, eat, smoke cigar, sleep, ...=0A=0AThese patterns are differe=
nt for each kind of monster. Some are=0Amore aggressive than others, =
some need less sleep, etc. Each monster=0Aalso has different prioriti=
es: some prefer food over fighting, others=0Alove their cigars, and s=
o on. These priorities are used when there are=0Aseveral needs of the=
same level.=0A=0ASome questions have already been answered - e.g. ho=
w to prevent that=0Amonsters eat all fortune cookies :-)=0A=0AOther q=
uestions remain: Will the players appreciate the interesting=0Abehavi=
our of the monsters? How do we balance the game (cheating might=0Abe =
a solution - if too many monsters have been eaten, there just=0Aappea=
r new ones...)=0A=0AI think it's easier to answer these questions whe=
n the framework has=0Abeen integrated into the game. """=0A=0Aimport =
time=0Aimport string=0A=0Aclass Monster:=0A """ a monster which ca=
n act according to his internal state and=0A external =0A f=
actors"""=0A=0A def __init__(self):=0A """init state, rules=
and needs"""=0A self.rules =3D []=0A for element in se=
lf.behaviour:=0A self.rules =3D self.rules + self._make_ru=
les(element[0],=0A element[1])=0A self.needs =3D Ne=
eds(self.rules, self.state, self.order)=0A self.act_dict =3D {=
'hungry': self.eat_food, 'tired':=0A self.fall_asleep,=0A =
'aggressive': self.attack_enemy}=0A=0A def _ma=
ke_rules(self, type, borders):=0A """ defines rules for each i=
nterval in borders"""=0A rules =3D []=0A for idx in ran=
ge(5):=0A rules.append(Rule(type, Condition(borders[idx],=
=0A borders[idx+1], idx+1)))=0A return rules=0A=
=0A def act(self, ext_factors =3D []): =0A """act according=
to needs and (not yet) external factors"""=0A self.needs.upda=
te(self.state, ext_factors) # max_need =3D =0A self.needs.get_=
max()=0D=0A action =3D self.act_dict[max_need[0]]=0A ac=
tion()=0A=0A def eat_food(self):=0A """ when hungry, eat so=
me food=0A just a dummy right now - later on, the characte=
r=0A should actively try to find something to eat."""=0A =
print "\teating some food\t(+)\t",self.needs=0A self.sta=
te.change(hungry=3D0)=0A self.state.increase("tired", 5)=0A =
self.state.increase("aggressive", 5)=0A=0A def fall_asleep(se=
lf):=0A """ also just a dummy. sleeping characters=0A =
still have to be able to react when something=0A starts=
to attack - maybe with a little delay"""=0A print "\tfalling =
asleep\t\t(-)\t",self.needs=0A self.state.change(tired=3D0)=
=0A self.state.increase("hungry", 10)=0A self.state.inc=
rease("aggressive", 1)=0A=0A def attack_enemy(self):=0A """=
JAT - just another dummy. =0A remark: fighting makes hung=
ry!"""=0A print "\tattacking someone\t(@)\t",self.needs=0A =
self.state.change(aggressive=3D0)=0A self.state.increase("=
hungry", 10)=0A=0A=0Aclass State:=0A """ the current state of the =
character. up to now only=0A three parameters: level of hunger=
, tiredness and=0A aggressiveness other parameters should defi=
nitely be added,=0A e.g. fear, needs to communicate etc."""=
=0A=0A def __init__(self, **state):=0A """ init state to so=
me values=0A this should be overridden by each monster, ac=
cording=0A to its characteristics"""=0A self.state =
=3D state=0A=0A def __repr__(self):=0A res =3D ""=0A =
for element in self.state.keys():=0A res =3D res + elemen=
t + ": " + str(self.state[element]) +"\n"=0A return res=0A=
=0A def change(self, **new_state):=0A """ changes all param=
eters in the dictionary new_state=0A to their correspondin=
g values"""=0A for key in new_state.keys():=0A self=
.state[key] =3D new_state[key]=0A=0A def increase(self, parameter,=
val):=0A """increases the parameter by val"""=0A self.=
state[parameter] =3D self.state[parameter] + val=0A=0Aclass Condition=
:=0A def __init__(self, min, max, res):=0A self.min, self.m=
ax, self.res =3D min, max, res=0A=0A def __repr__(self):=0A =
res =3D "\tIf " + str(self.min) + " <=3D value and value < "=0A =
res =3D res + str(self.max) + " return " + str(self.res) + "\n"=
=0A return res=0A=0A def eval(self, val):=0A if self=
.min <=3D val and val < self.max:=0A return self.res=0A =
else:=0A return 0=0A=0Aclass Rule:=0A """ a rule d=
escribing the connection between state and need=0A eg. some ch=
aracters have to eat quite often=0A self.about: 'hungr=
y', =0A self.cond: is 15<=3Dx<20? =0A =
yes =3D=3D> return 4 (_very_ hungry)=0A other charac=
ters do not have to eat often=0A self.about: 'hungry',=
=0A self.cond: is 15<=3Dx<20? =0A =
yes =3D=3D> return 2 (not so bad)"""=0A=0A def __init__(sel=
f, about, cond):=0A self.about =3D about=0A self.cond =
=3D cond=0A=0A def __repr__(self):=0A res =3D ""=0A =
res =3D res + "Rule for " + self.about + ":\n"=0A res =3D res =
+ self.cond.__repr__()=0A return res=0A=0A def eval(self, s=
tate):=0A """ check whether condition is fullfilled in state=
=0A if yes, return val, otherwise 0"""=0A return se=
lf.cond.eval(state.state[self.about])=0A=0Aclass Needs:=0A """ the=
current needs of the character=0A every need can range betwee=
n a value from 0 to 5, where=0A 5 is the strongest need (chara=
cter collapses when his=0A hunger reaches level 5 - he goes be=
rserk with aggressiveness=0A level 5, etc."""=0A=0A def __i=
nit__(self, rules, state, order):=0A self.needs =3D {}=0A =
self.rules =3D rules=0A self.order =3D order # defines a=
n order between the needs=0A self.update(state, [])=0A=0A d=
ef __repr__(self):=0A res =3D ""=0A for need in self.or=
der:=0A res =3D res + need[0] + "-" + str(self.needs[need]=
) + "; "=0A return "(" + res[:-2]+")"=0A=0A def update(self=
, state, ext_factors=3D[]):=0A """ update needs according to s=
tate and (not yet) external=0A factors - use rules for upd=
ate"""=0A for rule in self.rules:=0A v =3D rule.eva=
l(state)=0A if v:=0A self.needs[rule.about]=
=3D v=0A self.check_alive()=0A=0A def get_max(self):=0A =
"""return most urgent need"""=0A max =3D (None, 0)=0A =
for need in self.order:=0A val =3D self.needs[need]=
=0A if val>max[1]:=0A max =3D (need, val)=
=0A return max=0A=0A def check_alive(self):=0A """ c=
heck, whether no need exceeds the maximum (4).=0A characte=
r can die from hunger, no sleep, =0A heart attack, etc."""=
=0A for need in self.needs.keys():=0A if self.needs=
[need] > 4:=0A raise "Died! ", need + " exceeds maximu=
m"=0A=0Aclass Bobbit(Monster):=0A """a peaceful monster - its main=
objectives are its belly=0A and its cigars"""=0A def __ini=
t__(self):=0A self.type=3D"Bobbit"=0A self.order =3D ["=
hungry", "smoke", "tired", "aggressive"]=0A self.state =3D Sta=
te(hungry=3D0, tired=3D0, aggressive=3D10, smoke=3D5)=0A self.=
behaviour =3D [('hungry', [0, 5, 10, 15, 20, 50]), =0A ('tir=
ed', [0, 8, 16, 24, 32, 50]),=0A ('aggressive', [0, 30, 35, =
40, 45, 50]),=0A ('smoke', [0, 10, 15, 20, 40, 50])]=0A =
Monster.__init__(self)=0A self.act_dict =3D {'hungry': self=
.eat_food, =0A 'tired': self.fall_asleep,=
=0A 'aggressive': self.attack_enemy, =0A =
'smoke': self.smoke}=0A=0A def eat_food(self):=
=0A Monster.eat_food(self)=0A self.state.increase("smok=
e", 10)=0A=0A def smoke(self):=0A print "\tsmoking some cig=
ar\t(!)\t",self.needs=0A self.state.change(smoke=3D0)=0A=0A=
=0Aclass Spider(Monster):=0A """a very aggressive type. needs almo=
st no sleep, weaves=0A webs sometimes"""=0A def __init__(se=
lf):=0A self.type=3D"Giant Spider"=0A self.order =3D ["=
aggressive", "hungry", "tired", "web"]=0A self.state =3D State=
(hungry=3D10, tired=3D0, aggressive=3D20, web=3D10)=0A self.be=
haviour =3D [('hungry', [0, 10, 20, 30, 40, 50]), =0A ('tire=
d', [0, 30, 35, 40, 45, 50]),=0A ('web', [0, 15, 22, 29, 36,=
50]),=0A ('aggressive', [0, 5, 12, 18, 25, 50])]=0A =
Monster.__init__(self)=0A self.act_dict =3D {'hungry': self.ea=
t_food, =0A 'tired': self.fall_asleep,=0A =
'aggressive': self.attack_enemy, =0A =
'web': self.weave_web}=0A=0A def attack_enemy(self):=
=0A print "\tattacking someone\t(@)\t",self.needs=0A se=
lf.state.increase("aggressive", -8)=0A self.state.increase("hu=
ngry", 10)=0A self.state.increase("web", 10)=0A=0A def weav=
e_web(self):=0A print "\tweaving some web\t(!)\t",self.needs=
=0A self.state.change(web=3D0)=0A self.state.increase("=
hungry", 10)=0A=0A=0Aclass Orc(Monster):=0A """straightforward, ve=
ry balanced life. could be enhanced by =0A adding the need to =
be in a group (no idea how to do =0A this, though...)"""=0A =
def __init__(self):=0A self.type=3D"Orc"=0A self.order=
=3D ["aggressive", "hungry", "tired"]=0A self.state =3D State=
(hungry=3D5, tired=3D0, aggressive=3D15)=0A self.behaviour =
=3D [('hungry', [0, 10, 20, 30, 40, 50]), =0A ('tired', [0, =
10, 20, 30, 40, 50]),=0A ('aggressive', [0, 8, 13, 18, 23, 5=
0])]=0A Monster.__init__(self)=0A=0Aclass Hobgoblin(Monster):=
=0A """also straightforward, needs less sleep than orc"""=0A de=
f __init__(self):=0A self.type=3D"Hobgoblin"=0A self.or=
der =3D ["aggressive", "tired", "hungry"]=0A self.state =3D St=
ate(hungry=3D30, tired=3D15, aggressive=3D10)=0A self.behaviou=
r =3D [('hungry', [0, 10, 20, 30, 40, 50]), =0A ('tired', [0=
, 20, 30, 40, 45, 50]),=0A ('aggressive', [0, 15, 20, 25, 30=
, 50])]=0A Monster.__init__(self)=0A=0Aclass Nymph(Monster):=
=0A """not very agressive, but tries to steal goods"""=0A def _=
_init__(self):=0A self.type=3D"Nymph"=0A self.order =
=3D ["tired", "greedy", "hungry", "aggressive"]=0A self.state =
=3D State(hungry=3D25, tired=3D10, aggressive=3D10,greedy=3D5)=0D=
=0A self.behaviour =3D [('hungry', [0, 10, 20, 30, 40,50]), =
=0A ('tired', [0, 10, 20, 30, 40, 50]),=0A ('aggres=
sive', [0, 20, 25, 33, 40, 50]),=0A ('greedy', [0, 6, 12, 18=
, 24, 50])]=0A Monster.__init__(self)=0A self.act_dict =
=3D {'hungry': self.eat_food, =0A 'tired': se=
lf.fall_asleep,=0A 'aggressive': self.attack_=
enemy, =0A 'greedy': self.steal}=0A=0A def=
eat_food(self):=0A Monster.eat_food(self)=0A self.stat=
e.increase("greedy", 5)=0A=0A def steal(self):=0A print "\t=
stealing some money\t(!)\t",self.needs=0A self.state.change(gr=
eedy=3D0)=0A=0Adef test():=0A # define order of needs=0A # coul=
d be specific for each kind of monster=0A=0A # print introductory =
message=0A print "This simulation shows different monsters with di=
fferent"=0A print "patterns of behaviour. It follows each monster =
on it's "=0A print "way through the dungeon and prints out each ac=
tion the "=0A print "monster takes." =0D=0A print "I use three =
columns to show an action:"=0D=0A print "\t* The first column show=
s the action of the monster"=0D=0A print "\t* The second column sh=
ows the symbol for this action."=0D=0A print "\t\tThis makes it ea=
sier to recognize patterns."=0A print "\t* The third column shows =
how strong the needs of the"=0A print "\t\tmonster thave been _bef=
ore_ they chose the action."=0A print "\t\tt stands for tired, h f=
or hungry and a for "=0D=0A print "\t\taggressive" =0D=0A print=
"Each monster has different priorities. Bobbits are quite "=0D=0A =
print "peaceful and need lots of food, while giant spiders"=0D=0A =
print "prefer attacking poor victims. Orcs also like to attack,"=
=0D=0A print "but they need more food and sleep, and so on."=0D=
=0A print raw_input("Press <Return>...")=0A=0A # there we go!=
=0A monsters =3D [Bobbit(), Spider(), Orc(), Hobgoblin(), Nymph()]=
=0A while 1:=0A idx =3D 1=0A for c in monsters:=0A =
print =0A print idx, c.type+":"=0A =
print "\t"+c.__doc__=0A idx =3D idx + 1=0A choice =
=3D input("Which type of monster would you like to watch?")#-1=0D=
=0A num =3D input("Specify the number of turns the monster sho=
uld take: ")=0D=0A monster =3D monsters[choice]=0D=0A m=
onster.type+":"=0D=0A for i in range(num):=0A monst=
er.act()=0A time.sleep(1)=0A choice =3D raw_input("=
Would you like to watch another monster \=0A (Y/N>? ")=0D=0A =
if string.upper(choice) !=3D "Y":=0A break=0A=0Aif _=
_name__ =3D=3D "__main__":=0A test()=0A=0A=
--Boundary_(ID_HPDIycDfCuX9BiByMooKLQ)--