예제 #1
0
 def setUp(self):
     self.grid = Grid(5, 5)
     self.queen = AntQueen(self.grid, NO_SPRITE)
     self.ant = AntWorker(self.grid, NO_SPRITE, randomizer=DeterministRandomizer())
예제 #2
0
class AntWorkerTestCase(unittest.TestCase):
    from net.cadrian.microcosmos.model.bugs.antWorkers import TRAIL_HILL, TRAIL_FOOD, TRAIL_LICE

    def setUp(self):
        self.grid = Grid(5, 5)
        self.queen = AntQueen(self.grid, NO_SPRITE)
        self.ant = AntWorker(self.grid, NO_SPRITE, randomizer=DeterministRandomizer())

    def test01a(self):
        """ an ant moving from the hill leaves a trail -- note: the hill is the square around the queen """
        self.grid.put(2, 2, self.queen)
        self.grid.put(1, 1, self.ant)
        self.ant.prepareToMove()
        self.grid.diffuse()
        self.assertEquals(32, self.grid.scent(1, 1, self.TRAIL_HILL))

    def test01b(self):
        """ an ant moving from the hill leaves a trail -- the ant far from the hill """
        self.grid.put(2, 2, self.queen)
        self.grid.put(0, 0, self.ant)
        self.ant.prepareToMove()
        self.grid.diffuse()
        self.assertEquals(0, self.grid.scent(0, 0, self.TRAIL_HILL))

    def test01c(self):
        """ an ant moving from the hill leaves a trail -- the ant is far from the hill but marked as coming from it """
        self.ant._setLeavingHill()
        self.grid.put(2, 2, self.queen)
        self.grid.put(0, 0, self.ant)
        self.ant.prepareToMove()
        self.grid.diffuse()
        self.assertEquals(32, self.grid.scent(0, 0, self.TRAIL_HILL))

    def test01d(self):
        """ an ant moving from the hill leaves a trail -- the ant is getting far from the hill but moves from it """
        self.grid.put(2, 2, self.queen)
        self.grid.put(1, 1, self.ant)
        self.ant.prepareToMove()
        self.grid.diffuse()
        self.assertEquals(32, self.grid.scent(1, 1, self.TRAIL_HILL))

        self.ant.move()
        self.assertFalse(self.grid.has(1, 1, self.ant))
        self.assertTrue(self.grid.has(self.ant.x, self.ant.y, self.ant))

        self.ant.prepareToMove()
        self.grid.diffuse()
        self.assertEquals(36, self.grid.scent(self.ant.x, self.ant.y, self.TRAIL_HILL))

    def test02a(self):
        """ when leaving hill, an ant will choose to explore by default """
        self.ant._setLeavingHill()
        self.grid.put(2, 2, self.queen)
        self.grid.put(1, 1, self.ant)
        self.ant.prepareToMove()
        self.assertEquals("exploration", str(self.ant.state))

    def test02b(self):
        """ when leaving hill, an ant will choose to follow a trail to food if there is one """
        self.ant._setLeavingHill()
        self.grid.put(2, 2, self.queen)
        self.grid.put(1, 1, self.ant)
        self.grid._ensurePheromone(1, 2, self.TRAIL_FOOD, 8)
        self.assertEquals(8, self.grid.scent(1, 2, self.TRAIL_FOOD))
        self.ant.prepareToMove()
        self.assertEquals("following food", str(self.ant.state))
        self.ant.move()
        self.assertFalse(self.grid.has(1, 1, self.ant))
        self.assertTrue(self.grid.has(1, 2, self.ant))

    def test02c(self):
        """ when leaving hill, an ant will choose to follow a trail to lice if there is one """
        self.ant._setLeavingHill()
        self.grid.put(2, 2, self.queen)
        self.grid.put(1, 1, self.ant)
        self.grid._ensurePheromone(1, 2, self.TRAIL_LICE, 8)
        self.assertEquals(8, self.grid.scent(1, 2, self.TRAIL_LICE))
        self.ant.prepareToMove()
        self.assertEquals("following lice", str(self.ant.state))
        self.ant.move()
        self.assertFalse(self.grid.has(1, 1, self.ant))
        self.assertTrue(self.grid.has(1, 2, self.ant))

    def test02d(self):
        """ when leaving hill, an ant will choose to follow the strongest trail is there are both a food and a lice trails """
        self.ant._setLeavingHill()
        self.grid.put(2, 2, self.queen)
        self.grid.put(1, 1, self.ant)
        self.grid._ensurePheromone(1, 2, self.TRAIL_LICE, 8)
        self.grid._ensurePheromone(1, 0, self.TRAIL_FOOD, 16)
        self.assertEquals(8, self.grid.scent(1, 2, self.TRAIL_LICE))
        self.ant.prepareToMove()
        self.assertEquals("following food", str(self.ant.state))
        self.ant.move()
        self.assertFalse(self.grid.has(1, 1, self.ant))
        self.assertFalse(self.grid.has(1, 2, self.ant))
        self.assertTrue(self.grid.has(1, 0, self.ant))

    def test02e(self):
        """ when leaving hill, an ant will choose to follow the lice trail over the food trail if both trails have the same strength """
        self.ant._setLeavingHill()
        self.grid.put(2, 2, self.queen)
        self.grid.put(1, 1, self.ant)
        self.grid._ensurePheromone(1, 2, self.TRAIL_LICE, 8)
        self.grid._ensurePheromone(1, 0, self.TRAIL_FOOD, 8)
        self.assertEquals(8, self.grid.scent(1, 2, self.TRAIL_LICE))
        self.ant.prepareToMove()
        self.assertEquals("following lice", str(self.ant.state))
        self.ant.move()
        self.assertFalse(self.grid.has(1, 1, self.ant))
        self.assertFalse(self.grid.has(1, 0, self.ant))
        self.assertTrue(self.grid.has(1, 2, self.ant))

    def test03a(self):
        """ when finding food, the ant takes some of it """
        self.ant._setLeavingHill()
        self.grid.put(2, 2, self.queen)
        self.grid.put(0, 0, self.ant)
        self.grid._ensurePheromone(1, 1, self.TRAIL_HILL, 8)
        food = Food(self.grid, store=5)
        self.grid.put(0, 0, food)
        self.ant.prepareToMove()
        self.assertEquals("storing food", str(self.ant.state))
        self.ant.move()
        self.assertTrue(self.grid.has(0, 0, self.ant))
        self.assertTrue(self.ant._hasFood())
        self.assertEquals(4, food.store)
        self.ant.prepareToMove()
        self.assertEquals("following hill", str(self.ant.state))
        self.ant.move()
        self.assertFalse(self.grid.has(0, 0, self.ant))
        self.assertTrue(self.grid.has(1, 1, self.ant))

    def test03b(self):
        """ when finding lice, the ant milks them """
        self.ant._setLeavingHill()
        self.grid.put(2, 2, self.queen)
        self.grid.put(0, 0, self.ant)
        self.grid._ensurePheromone(1, 1, self.TRAIL_HILL, 8)
        louse = Louse(self.grid, milk=5)
        self.grid.put(0, 0, louse)
        self.ant.prepareToMove()
        self.assertEquals("storing milk", str(self.ant.state))
        self.ant.move()
        self.assertTrue(self.grid.has(0, 0, self.ant))
        self.assertTrue(self.ant._hasFood())
        self.assertEquals(4, louse.milk)
        self.ant.prepareToMove()
        self.assertEquals("following hill", str(self.ant.state))
        self.ant.move()
        self.assertFalse(self.grid.has(0, 0, self.ant))
        self.assertTrue(self.grid.has(1, 1, self.ant))

    def test04(self):
        """ an ant tries to go home even when there is no trail to follow -- and it leaves a trail"""
        self.grid.put(2, 2, self.queen)
        self.grid.put(0, 0, self.ant)
        self.grid.diffuse()
        self.ant._setFood(SCENT_FOOD)
        self.grid.diffuse()
        self.ant.prepareToMove()
        self.assertEquals("following queen", str(self.ant.state))
        self.ant.move()
        self.assertFalse(self.grid.has(0, 0, self.ant))
        self.assertTrue(self.grid.has(1, 1, self.ant))
        self.assertEquals(16, self.grid.scent(0, 0, SCENT_FOOD.kind))