[Tutor] Killing an instance

Michael Langford mlangford.cs03 at gtalumni.org
Fri Sep 14 00:12:45 CEST 2007


This is a little more complicated.

The more object oriented way:
Make each cell know its location and have a reference to the world its in.
You can have the world set the object the coordinates the item is at when it
adds it to itself. When the life==0, you can have the yeast cell call a
remove( self.x, self.y) method on the world. This is useful if you're doing
a lot of things that involve the cell knowing its position. Your life
simulation could probably get this complicated.

The less object oriented way...that's a lot simpler:
Make a "reap()" method in the world class that removes all the tokens that
are life==0. I'd make a method in the cell like isDead(). The reap function
for the world will remove all cells that isDead()==True. This is a better
approach when your cells don't do very much more than they already do.

     --Michael

On 9/13/07, Ara Kooser <ghashsnaga at gmail.com> wrote:
>
>    I have two instances called and running. They interact with each
> other and I would like one of the instances to cease to exist in the
> second round based on a given condition. How do you kill an instance?
>
>    The code is below. I have Red and Yellow as my instances and I want
> them to die when life = 0 and not show up in the preceding rounds of
> the game.
>
> Thank you.
> Ara
>
>
>
>
> CODE HERE:
> ##########################################################################
> #Red and Yellow yeast model
> #
> #Both yeast start out cooperating.
> #If the yeast detect that there is no ade or lys present they will defect
> #but die.
> ##########################################################################
>
> import time
>
> chemicals = []
>
> #0 cooperates
> #1 defects
>
> class RedYeast:
> #Needs adenine to grow, releases lysine
>     def __init__(self,name,group):
>         self.name = name
>         self.group = group
>         self.life = 1
>         self.hate_tolerance = 0
>         self.choice = 0
>
> #METHODS
>     def lys_re(self,stuffa):
> #release of lys in the environment
>         if self.choice == 0:
>             print self.name, "-- Releasing lys"
>             chemicals.append(stuffa)
>         elif self.choice == 1:
>             print self.name, "-- Withholds lys"
>
>     def ade_need(self,stuffb):
>
>         if stuffb != chemicals:
>             print self.name,"is taking ade"
>             self.life = self.life+1
>             chemicals.remove(stuffb)
>             print chemicals
>             print "Life", self.life
>         else:
>             print "No ade presents"
>             self.life = self.life-1
>             self.choice = 1
>
>
>
> class YellowYeast:
> #Needs lysine to grow, releases adenine
>     def __init__(self,name,group):
>         self.name = name
>         self.group = group
>         self.life = 1
>         self.hate_tolerance = 0
>         self.choice = 0
>
> #METHODS
>     def ade_re(self,stuffa):
> #release of lys in the environment
>         if self.choice == 0:
>             print self.name, "-- Releasing ade"
>             chemicals.append(stuffa)
>         elif self.choice == 1:
>             print self.name, "-- Withholds ade"
>
>     def lys_need(self,stuffb):
>
>         if stuffb != chemicals:
>             print self.name," is taking ade"
>             self.life = self.life+1
>             chemicals.remove(stuffb)
>             print chemicals
>
>             print "Life",self.life
>             print
>         else:
>             print "No ade presents"
>             self.life = self.life-1
>             self.choice = 1
>
>
>     def death(self):
>
>
>
> ##############################################################
> #Start of program
> ##############################################################
>
> rounds = raw_input("How many rounds to you wish to play through?")
> print "Starting environment", chemicals
> rounds =int(rounds)
> count = 0
>
> #Creates the instances for RedYeast and YellowYeast
> one = RedYeast("Red","alpha")
> two = YellowYeast("Yellow","alpha")
>
> #Game logic
>
> while count < rounds:
>     print "##################################################"
>     print
>
>     one.lys_re("lys")
>     two.ade_re("ade")
>
>     print
>     print "Chemicals in the environment",chemicals
>     print
>
>     time.sleep(1)
>
>     one.ade_need("ade")
>     two.lys_need("lys")
>
>     print "##################################################"
>     print
>
>
>     count = count+1
>
> print "Ending environment", chemicals
>
> --
> Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
> an sub cardine glacialis ursae.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070913/39417767/attachment-0001.htm 


More information about the Tutor mailing list