示例#1
0
文件: base.py 项目: NElias/swottc
 def reproduction_by_mating(self, creatures, world):
     variants = filter(lambda x: isinstance(x, self.__class__) and x.is_ready_reproduction, creatures)
     if variants:
         partner = choice(variants)
         partner.reproductive = 0
         self.reproductive = 0
         child = self.__class__(x=self.x, y=self.y)
         child.brain = Kohonen.generate(child.brain, self.brain, partner.brain)
         is_add = world.add_creature_square(child)
         self.history.append("Birth %s %s" % (is_add, child))
示例#2
0
文件: base.py 项目: NElias/swottc
 def __init__(self, *args, **kwargs):
     super(Mammals, self).__init__(*args, **kwargs)
     self.brain = Kohonen(12, 6, 4)
     self.course = choice((COURSE_SOUTH, COURSE_NORTH, COURSE_WEST, COURSE_EAST))
     self.turns = 0
示例#3
0
文件: base.py 项目: NElias/swottc
class Mammals(Base):
    def __init__(self, *args, **kwargs):
        super(Mammals, self).__init__(*args, **kwargs)
        self.brain = Kohonen(12, 6, 4)
        self.course = choice((COURSE_SOUTH, COURSE_NORTH, COURSE_WEST, COURSE_EAST))
        self.turns = 0

    def turn(self, world):
        super(Mammals, self).turn(world)
        if not self.is_alive:
            return
        eye = Eye(self.course, world, (self.x, self.y))
        if self.is_ready_reproduction:
            self.reproduction_by_division(world)
        answer = self.brain.signal_eye(eye)
        if ACTION_GO == answer:
            world.move_creature(self)
        elif ACTION_LEFT == answer:
            self.course = course_turn(self.course, False)
        elif ACTION_RIGHT == answer:
            self.course = course_turn(self.course, True)
        elif ACTION_EAT == answer:
            creature, pos = world.get_creature_by_course(self)
            if creature:
                self.eat(creature)
        else:
            raise Exception()
        self.hunger()

    def eat(self):
        raise Exception("Need to implement")

    def hunger(self):
        raise Exception("Need to implement")

    def reproduction_by_mating(self, creatures, world):
        variants = filter(lambda x: isinstance(x, self.__class__) and x.is_ready_reproduction, creatures)
        if variants:
            partner = choice(variants)
            partner.reproductive = 0
            self.reproductive = 0
            child = self.__class__(x=self.x, y=self.y)
            child.brain = Kohonen.generate(child.brain, self.brain, partner.brain)
            is_add = world.add_creature_square(child)
            self.history.append("Birth %s %s" % (is_add, child))

    def reproduction_by_simple_mating(self, world):
        variants = filter(lambda x: isinstance(x, self.__class__) and x.is_ready_reproduction, world._objects)
        if variants:
            partner = choice(variants)
            child = self.__class__(x=self.x, y=self.y)
            child.brain = Kohonen.generate(child.brain, self.brain, partner.brain)
            is_add = world.add_creature_square(child)
            if is_add:
                partner.reproductive = 0
                self.reproductive = 0
                world.reproductions += 1
                self.history.append("Birth %s %s" % (is_add, child))

    def reproduction_by_division(self, world):
        child = self.__class__(x=self.x, y=self.y)
        child.brain = self.brain.copy().mutate()
        is_add = world.add_creature_square(child)
        if is_add:
            self.reproductive = 0
            world.reproductions += 1
            self.history.append("Birth %s %s" % (is_add, child))

    def reproductive_up(self, hp):
        if self.reproductive + hp > self.base_health:
            self.reproductive = self.base_health
        else:
            self.reproductive += hp

    @property
    def is_ready_reproduction(self):
        return self.reproductive == self.base_health