Example #1
0
def test_Zoo_basic():
    '''
    Test all functionality of Zoo class
    '''
    a1 = zoo.Snake('red')
    a2 = zoo.Snake('blue')
    a3 = zoo.Sheep('orange')
    a4 = zoo.Wolf('purple')
    a5 = zoo.Parrot('blue')
    a6 = zoo.Sheep('purple')
    a7 = zoo.Snake('red')
    a8 = zoo.Parrot('black')
    a9 = zoo.Wolf('blue')
    a10 = zoo.Sheep('green')

    c1 = zoo.Cage(1)
    c1.add_animals(a1, a2)
    c2 = zoo.Cage(12)
    c2.add_animals(a3)
    c3 = zoo.Cage(77)
    c3.add_animals(a4, a5, a6, a7)
    c4 = zoo.Cage(400)
    c4.add_animals(a8, a9)
    c5 = zoo.Cage(6)
    c5.add_animals(a10)

    z1 = zoo.Zoo()
    z1.add_cages(c1)
    z1.add_cages(c2, c3, c4)
    z1.add_cages(c5)

    # animals_by_color
    assert len(
        z1.animals_by_color('red')) == 2, 'expected 2 red animals in zoo'
    assert len(z1.animals_by_color(
        'turquoise')) == 0, 'expected 0 turquoise animals in zoo'
    assert len(
        z1.animals_by_color('blue')) == 3, 'expected 3 blue animals in zoo'

    # animals_by_legs
    assert len(z1.animals_by_legs(0)) == 3, 'expected 3 animals with 0 legs'
    assert len(z1.animals_by_legs(2)) == 2, 'expected 2 animals with 2 legs'
    assert len(z1.animals_by_legs(3)) == 0, 'expected 0 animals with 3 legs'
    assert len(z1.animals_by_legs(4)) == 5, 'expected 5 animals with 4 legs'

    # number_of_legs
    assert z1.number_of_legs() == 24, 'expected 20 legs in the zoo'

    # __repr__
    z2 = zoo.Zoo()
    z2.add_cages(c2, c4)
    expected_str = '12: (Orange sheep, 4 legs)\n400: (Black parrot, 2 legs), (Blue wolf, 4 legs)'
    assert str(z2) == expected_str, 'unexpected string format'
Example #2
0
 def check_existences(self):
     try:
         zoo.Animal("")
     except NameError:
         self.assertTrue(False, _("Does the class `Animal` exist?"))
     try:
         zoo.Lion("")
     except NameError:
         self.assertTrue(False, _("Does the class `Lion` exist?"))
     try:
         zoo.Owl("")
     except NameError:
         self.assertTrue(False, _("Does the class `Owl` exist?"))
     try:
         zoo.Giraffe("", 1.0)
     except NameError:
         self.assertTrue(False, _("Does the class `Giraffe` exist?"))
     animal = zoo.Animal("")
     self.assertTrue(hasattr(animal, "name"), _("Your class `Animal` does not have an attribute `name`."))
     self.assertTrue(hasattr(animal, "diurnal"), _("Your class `Animal` does not have an attribute `diurnal`."))
     self.assertTrue(hasattr(animal, "nb_legs"), _("Your class `Animal` does not have an attribute `nb_legs`."))
     self.assertTrue(hasattr(animal, "asleep"), _("Your class `Animal` does not have an attribute `asleep`."))
     try:
         my_zoo = zoo.Zoo()
         self.assertTrue(hasattr(my_zoo, "animals"), _("Your class `Zoo` does not have an attribute `animals`."))
         self.assertTrue(hasattr(my_zoo, "add_animal"), _("Your class `Zoo` does not have a method "
                                                          "named `add_animal`."))
         self.assertTrue(hasattr(zoo, "create_my_zoo"), _("Does the class `create_my_zoo()` exists?"))
     except NameError:
         self.assertTrue(False, _("Does the class `Zoo` exist?"))
Example #3
0
def test_voices():
    z = zoo.Zoo()
    z.add(zoo.Cat("Tom"))
    z.add(zoo.Dog("Bandit"))
    z.add(zoo.Bird("Charlie"))
    assert [animal.say()
            for animal in zoo.get_animals()] == ["meow", "woof", "tweet"]
Example #4
0
    def __init__(self, mapsize=(20, 20), tilesize=(5, 5), waterlevel=0.1):

        #map properties
        self.mapsize = mapsize  #in tiles
        self.tilesize = tilesize  #in pixels, height, width

        #basic core calculations
        self.mapsizeInPixels = (self.mapsize[0] * self.tilesize[0],
                                self.mapsize[1] * self.tilesize[1])

        #init the overall world data array:
        self.worldframe = np.zeros(
            (self.mapsize[0], self.mapsize[1], self.LAYERS), dtype=np.float64)

        #create the heightmap
        heightmap = np.random.rand(
            self.mapsize[0], self.mapsize[1])  #random value between 0. and 1.
        kernel = np.ones((5, 5), np.float64) / 25
        heightmap = cv2.filter2D(heightmap, -1, kernel)
        self.worldframe[:, :, self.LAYERID_HEIGHT] = self.scaleTo01(heightmap)

        #create water map (flood everything such that waterlevel amount of tiles are full of water)
        indexOfWaterLevel = int(self.mapsize[0] * self.mapsize[1] * waterlevel)
        heightmap = self.worldframe[:, :, self.LAYERID_HEIGHT]
        sortedFlattenedHeights = np.sort(np.ndarray.flatten(heightmap))
        waterLevel = sortedFlattenedHeights[indexOfWaterLevel]
        waterDepthMap = waterLevel - heightmap
        waterDepthMap[waterDepthMap <= 0] = 0.0
        self.worldframe[:, :, self.LAYERID_WATERDEPTH] = waterDepthMap

        #create humidity
        self.humidity = np.random.rand(
            self.mapsize[0], self.mapsize[1])  #random value between 0. and 1.
        kernel = np.ones((5, 5), np.float32) / 25
        self.humidity = cv2.filter2D(self.humidity, -1, kernel)
        self.worldframe[:, :,
                        self.LAYERID_HUMIDITY] = self.scaleTo01(self.humidity)

        #create vegetation
        vegetationMap = np.zeros(self.mapsize, dtype=np.float64)
        self.vegetationBaseGrowth = 0.0001
        self.worldframe[:, :, self.LAYERID_VEGETATION] = vegetationMap

        if GFX:
            #image: y-axis, x-axis, rgba
            self.image = np.zeros(
                (self.mapsizeInPixels[0], self.mapsizeInPixels[1], 4),
                np.uint8)

        self.highScoreCreatures = {}
        self.highScoreThreshold = 0
        self.keepTopK = 10

        self.zoo = zoo.Zoo()
Example #5
0
def makeTheZoo():
    return zoo.Zoo("Bill's Wonderland Zoo", getLocations())
Example #6
0
import zoo
import animals

seattle = zoo.Zoo('Old Seattle Zoo')
seattle.add_animal(animals.Turtle,
                   'Green Back').add_animal(animals.Turtle,
                                            'Big Shell').print_all_animals()
seattle.feed_animal('Green Back', 'greens')
print(seattle.animals[0].happiness)
print(seattle.animals[1].happiness)