Exemple #1
0
    def process(self, world_object: World) -> None:
        """Tornado scatters worms and food,
        Rain decrease health/energy of worms
        and decrease nutritional_value of food."""
        if len(world_object.rains) > 0:
            for rain in world_object.rains:
                for coordinate in rain.all_coordinates:
                    worms_in_cell = world_object.worms_at(coordinate)
                    food_in_cell = world_object.food_at(coordinate)
                    rain.raining_effect(worms_in_cell, food_in_cell)

        if len(world_object.tornadoes) > 0:
            for tornado in world_object.tornadoes:
                for coordinate in tornado.all_coordinates:
                    worms_in_cell = world_object.worms_at(coordinate)
                    food_in_cell = world_object.food_at(coordinate)
                    cell = world_object.cells[coordinate]
                    cell.worms = []
                    cell.food = []
                    tornado.tornado_effect(worms_in_cell, food_in_cell,
                                           world_object.width,
                                           world_object.height)
                    for worm in worms_in_cell:
                        world_object.cells[worm.coordinates].worms.append(worm)
                    for food in food_in_cell:
                        world_object.cells[food.coordinates].food.append(food)
Exemple #2
0
 def process(self, world_object: World) -> None:
     """Each worm picks up food if it is in the cage."""
     for eater in world_object.worms_by_initiative:
         targets = world_object.food_at(eater.coordinates)
         if len(targets) != 0:
             target = random.choice(targets)
             eater.eat(target)
Exemple #3
0
    def process(self, world_object: World) -> None:
        """The worms assess the danger and the presence of food
         in neighboring cells, then move to a random one of the most acceptable. """
        for worm in world_object.worms_by_initiative:
            if worm.dead:
                continue
            enemies = [
                enemy for enemy in world_object.worms_at(worm.coordinates)
                if enemy is not worm
            ]
            affordable_food = world_object.food_at(worm.coordinates)
            is_worm_in_danger = worm.is_dangerous(
                worm.max_danger_at_location(
                    world_object.worms_at(worm.coordinates)))

            is_nothing_interesting_in_cell = False

            if len(affordable_food) == 0 and len(enemies) == 0:
                is_nothing_interesting_in_cell = True

            if is_worm_in_danger or is_nothing_interesting_in_cell:
                neighbours_worms = world_object.get_neighbours_worms(
                    worm.coordinates, world_object.width, world_object.height)
                safe_steps = worm.get_safe_steps(neighbours_worms)
                if len(safe_steps) != 0:
                    steps_with_food = world_object.get_neighbours_food(
                        worm.coordinates)
                    dcoord = random.choice(
                        worm.get_best_steps(safe_steps, steps_with_food))

                    world_object.cells[worm.coordinates].worms.remove(worm)
                    worm.move(dcoord, world_object.width, world_object.height)
                    world_object.cells[worm.coordinates].worms.append(worm)