示例#1
0
def run_simulation(socket: SocketIO):
    random.seed(time())
    monsters = generate_world_elements(
        lambda position_energy: NormalMonster(*position_energy, 1,
                                              "SlowMonster"),
        randint(5, 15),
    )
    fast_monsters = generate_world_elements(
        lambda position_energy: NormalMonster(*position_energy, 2,
                                              "FastMonster"),
        randint(5, 15),
    )
    for monster in fast_monsters:
        monsters.add(monster)

    foods = generate_world_elements(
        lambda position_energy: Food(*position_energy), randint(90, 100))
    world = World(monsters, foods, world_map_constructor(socket))
    for turn in range(500):
        if turn % 50 == 0:
            foods = generate_world_elements(
                lambda position_energy: Food(*position_energy),
                randint(20, 30))
            for food in foods:
                world.foods.add(food)
        world.play_one_day()
        world.display()
        sleep(0.5)
示例#2
0
class TestDatabase(unittest.TestCase):
	def setUp(self):
		self.tomo = Tomo("Smith", 25, 100, [])
		self.food = Food(1, "Raclette", 50, 38)
		
	def test_insert_tomo(self):
		try:
			self.tomo.create_tomo(Database.GetInstance('test_tomo.db'))
		except:
			print("Création du Tomo échouée")

	def test_get_tomo(self):
		self.tomo = None
		try:
			self.tomo = Tomo.get_tomo(Database.GetInstance('test_tomo.db'), "Smith")
			print(self.tomo)
		except:
			print("Récupération du Tomo impossible")

	def test_insert_food(self):
		try:
			self.food.create_food(Database.GetInstance('test_tomo.db'))
		except Exception as e:
			print(e)

	def test_get_food(self):
		self.food = None
		try:
			self.food = Food.get_food(Database.GetInstance('test_tomo.db'), "Ralof")
		except Exception as e:
			print(e)

	def tearDown(self):
		#self.db.disconnect()
		pass
示例#3
0
    def test_whenDigestingFood_thenEnergyIsTranferred(self):
        energy = create_autospec(Energy)
        food_energy = create_autospec(Energy)
        food = Food(SOME_POSITION, food_energy)

        food.digest(energy)

        energy.add.assert_called_once_with(food_energy)
示例#4
0
    def test_whenMovingBy_thenFoodDoesntMove(self):
        energy = create_autospec(Energy)
        some_delta_position = Position(2, 2)
        food = Food(SOME_POSITION, energy)

        moved_food = food.move_by(some_delta_position)

        self.assertEqual(hash(food), hash(moved_food))
 def setUp(self):
     self.pub = Pub("The Prancing Pony", 100.00)
     self.customer_1 = Customer("Mark", 10.00, 33, 0)
     self.customer_2 = Customer("Andrew", 20.00, 17, 10)
     self.customer_3 = Customer("Steven", 0.00, 37, 20)
     self.drink_1 = Drink("Whisky", 5, 5, 10)
     self.drink_2 = Drink("Punk IPA", 4.50, 1, 10)
     self.food_1 = Food("Pie", 2.75, 5, 10)
     self.food_2 = Food("Crisps", 0.99, 1, 0)
示例#6
0
	def setUp(self):
		self.foods_list = [
			Food(1, "Raclette", -2, 10),
			Food(2, "Petit sushi des poubelles", 10, -5)
			#Food(-6, "Bâton", 12, 24), 
			#Food(2, "Potion", 53, 10), 
			#Food(3, "01644", 10, 10), 
			#Food(4, "Tomooooooooooooooooooooooooooooooooooooo", 5, 1)
		]
示例#7
0
    def setUp(self):
        self.customer = Customer("Malcolm", 25.0, 28)
        self.drink_1 = Drink("Mojito", 8.0, 7)
        self.drink_2 = Drink("Pilsen", 6.5, 4)
        self.food_1 = Food("Chips", 10.0, 5)
        self.food_2 = Food("Chicken Nuggets", 12.5, 8)

        drinks = [self.drink_2, self.drink_1]
        self.pub = Pub("JP's", drinks, 500)
示例#8
0
	def setUp(self):
		self.tomo_list = [
			Tomo("Ralof", 2, 100, ["Bâton"]), 
		]

		self.foods_list = [
			Food(1, "Bâton", 12, 24), 
			Food(2, "Potion", 53, 10)
		]
示例#9
0
    def test_givenFoodsDontHaveSamePosition_whenCalculatingHashes_thenHashesAreDifferent(
            self):
        energy = create_autospec(Energy)
        food = Food(SOME_POSITION, energy)
        food_2 = Food(SOME_POSITION + SOME_POSITION, energy)

        hash_food = hash(food)
        hash_food_2 = hash(food_2)

        self.assertNotEqual(hash_food, hash_food_2)
示例#10
0
    def test_givenFoodSurroundingMonster_whenPlayingTurn_thenMonsterMoveTowardFood(
            self):
        food = Food(SOME_POSITION + DELTA_POSITION_WITHIN_RANGE, SOME_ENERGY)
        monster = NormalMonster(SOME_POSITION, SOME_ENERGY, RANGE_OF_MOTION,
                                MONSTER_NAME)

        monster.play_turn(self.monsters_mock, WorldElements({food}))

        expected_movement = food.delta_position_from(monster.position)
        self.monsters_mock.move.assert_called_once_with(
            monster, expected_movement, SOME_ENERGY)
示例#11
0
    def setUp(self):
        stock = {
            "drinks": [Drink("cosmo", 4.00, 3),
                       Drink("beer", 2.50, 2)],
            "food": [Food("burger", 2.00, 10),
                     Food("hotdog", 2.00, 5)]
        }

        self.pub = Pub("The Prancing Pony", 100.00, stock)
        self.customer = Customer("Bob", 50.00, 30)
        self.drink = self.pub.stock["drinks"][0]
        self.young_customer = Customer("Betty", 20.00, 16)
        self.food = self.pub.stock["food"][1]
示例#12
0
    def test_ant_food_score(self):
        fp = Point(1, 2)
        f = Food(1, fp, 4)

        ant = Ant(1)
        ant.loc = Point(0, 0)
        self.assertAlmostEqual(1.333, ant.food_score(f), 3)
示例#13
0
 def test_ant_pick_food(self):
     foods = [Food(1, Point(1, x), x + 2) for x in range(4)]
     ant = Ant(1)
     ant.loc = Point(0, 0)
     i = ant.pick_food(foods)
     self.assertEqual(0, i[0])
     self.assertEqual(2.0, i[1])
示例#14
0
    def check_food_collision(self, entity, food):

        entity_coord_matrix = []
        for y in range(entity.coord[1], entity.coord[1] + entity.size[1]):
            for x in range(entity.coord[0], entity.coord[0] + entity.size[0]):
                entity_coord_matrix.append([x, y])

        food_coord_matrix = []
        for y in range(food.coord[1], food.coord[1] + food.size[1]):
            for x in range(food.coord[0], food.coord[0] + food.size[0]):
                food_coord_matrix.append([x, y])

        intercede = False
        for entity_coord in entity_coord_matrix:
            for food_coord in food_coord_matrix:
                if entity_coord == food_coord:
                    intercede = True

        if intercede:

            if entity.chromosome_score < 1:
                entity.chromosome_score = self.food.value
            else:
                entity.chromosome_score += self.food.value

            self.food = Food()
示例#15
0
    def test_givenFoodAtMonsterPosition_whenPlayingTurn_thenFoodIsRemoved(
            self):
        food = Food(SOME_POSITION, SOME_ENERGY)
        foods = WorldElements({food})

        A_MONSTER.play_turn(WorldElements({A_MONSTER}), foods)

        self.assertNotIn(food, foods)
示例#16
0
 def setUp(self):
     self.pub = Pub("The prancing pony", 100)
     self.drink = Drink("Beer", 2, 6)
     self.food = Food("Burger", 3, 2)
     self.customer = Customer("name", 10, 21)
     self.underage_customer = Customer("name", 10, 16)
     self.pub.add_stocklist(self.drink)
     self.pub.add_food(self.food)
示例#17
0
 def setUp(self):
     self.food = Food("Lasagne", 2.50, 5)
     self.pub = Pub("The Prancing Pony", 100.00,
                    ["Budweiser", "White Wine", "Red Wine", "Coke"])
     self.drink = Drink("Budweiser", 3.75, 4)
     self.customer = Customer("Joe Bloggs", 34, 10.00, 0)
     self.customer_2 = Customer("Bob Smith", 17, 5.00, 0)
     self.customer_3 = Customer("Bob Dylan", 55, 30.00, 15)
示例#18
0
 def setUp(self):
     self.pub = Pub("The Prancing Pony", 100.00)
     self.stock = {"beer": 5, "wine": 7, "absinthe": 2}
     self.drink_1 = Drink("wine", 8, 0.12)
     self.drink_2 = Drink("absinthe", 8, 0.8)
     self.food_1 = Food("pizza", 6, 0.4)
     self.customer_1 = Customer("Craig", 20, 18)
     self.customer_2 = Customer("Jack", 20, 18)
     self.customer_3 = Customer("Jack", 20, 17)
示例#19
0
 def test_customer_drunkenness_doesnt_go_below_zero(self):
     pub = Pub("The Prancing Pony", 100.00)
     drink = Drink("Vodka Martini", 6.50, 3)
     food = Food("Sunday Roast", 8.20, 8)
     pub.add_drink_to_list(drink)
     pub.add_food_to_list(food)
     self.customer.buy_drink(drink, pub)
     self.customer.buy_food(food, pub)
     self.assertEqual(0, self.customer.drunkenness)
示例#20
0
 def test_customer_drunkenness_goes_down_with_food(self):
     pub = Pub("The Prancing Pony", 100.00)
     drink = Drink("Vodka Martini", 6.50, 3)
     food = Food("Pizza", 7.20, 3)
     pub.add_drink_to_list(drink)
     pub.add_food_to_list(food)
     self.customer.buy_drink(drink, pub)
     self.customer.buy_drink(drink, pub)
     self.customer.buy_food(food, pub)
     self.assertEqual(3, self.customer.drunkenness)
示例#21
0
    def setUp(self):
        self.pub = Pub("Ox", 100.00)
        self.drink = Drink("Highland Park 18yr", 6.95, 8)
        self.drink2 = Drink("Rum", 5.50, 4)
        self.drink3 = Drink("Beer", 3.20, 2)

        self.food = Food("Chips and gravy", 3.50, 100)
        
        self.customer1 = Customer("Malcolm", 33, 25.00)
        self.customer2 = Customer("Harrison", 15, 30.00)
        self.customer3 = Customer("Dave", 21, 50.00)
示例#22
0
	def update(self, cio, key):
		self.timer.update()

		# 'c' key - creates new cyan small fish
		if key == cio.key_c:
			smallFish = SmallFish("cyan")
			self.initFish(smallFish)
			self.fishes.append(smallFish)

		# 'p' key - creates new predator fish
		if key == cio.key_p:
			predatorFish = PredatorFish("red")
			self.initFish(predatorFish)
			self.predators.append(predatorFish)

		# 'f' key - adds some food for the fishes
		if key == cio.key_f:
			food = Food("yellow")
			food.setPos(self.randPos())
			self.food.append(food)

		# Update all the objects in the tank
		for predator in self.predators:
			predator.update(self.timer.getDelta(), self.fishes)
		for fish in self.fishes:
			newFish = fish.update(self.timer.getDelta(), self.fishes, self.food, self.predators)
			if newFish:
				smallFish = SmallFish("cyan")
				smallFish.setBoundaries(1, 1, self.tank_w, self.tank_h)
				smallFish.setPos(fish.getPos())
				self.fishes.append(smallFish)

		# Move the objects (movements were stored in update)
		for predator in self.predators:
			predator.postUpdate()
		for fish in self.fishes:
			fish.postUpdate()
示例#23
0
    def test_givenNoFoodsAtMonsterPosition_whenPlayingTurn_thenMonsterDoesnGainEnergy(
            self, generate_fraction):
        generate_fraction.return_value = 2 * SOME_REPRODUCTION_PROBABILITY
        food = Food(SOME_POSITION + SOME_POSITION, SOME_ENERGY)
        monster_energy = Energy(SOME_ENERGY_VALUE)
        monster = NormalMonster(
            SOME_POSITION,
            monster_energy,
            RANGE_OF_MOTION,
            MONSTER_NAME,
            SOME_REPRODUCTION_PROBABILITY,
        )

        monster.play_turn(self.monsters_mock, WorldElements({food}))

        expected_energy = Energy(SOME_ENERGY_VALUE)
        self.assertEqual(expected_energy, monster_energy)
示例#24
0
    def start(self):

        while True:

            print("Generation: %s | Population: %s" %
                  (self.generation.index, self.generation.population_size))

            for entity in self.generation.population:

                self.food = Food()
                self.time = self.frames_sec * self.total_time

                while self.time > 0 and entity.alive:

                    if self.display:
                        self.display_screen_info(entity, pygame.event.get())

                    entity.update_direction()
                    entity.update_coord()
                    self.check_bounds_collision(entity, self.screen_size)
                    self.check_food_collision(entity, self.food)

                    distance_to_food = (
                        (self.food.coord[0] - entity.coord[0])**2) + (
                            (self.food.coord[1] - entity.coord[1])**2)
                    distance_to_food = math.sqrt(
                        distance_to_food) / self.screen_size[0]
                    if distance_to_food < self.min_distance_to_score and entity.chromosome_score < 1 and entity.alive:
                        entity.chromosome_score = 1 - distance_to_food

                    self.time -= self.frames_sec

                print("Subject: %s | Score: %.2f" %
                      (entity.name, round(entity.chromosome_score, 2)))

            print("Generation: %s | Killed: %s | Best: %s" %
                  self.generation.advance_generation())
示例#25
0
 def setUp(self):
     self.food = Food("Pizza", 3.50, 3)
示例#26
0
 def test_add_food_to_list(self):
     food = Food("Mac and cheese", 5.50, 2)
     self.pub.add_food_to_list(food)
     self.assertEqual(1, self.pub.food_list_count())
示例#27
0
 def test_customer_cannot_afford_food(self):
     food_1 = Food("Chips", 6.50, 2)
     self.assertEqual(False,
                      self.customer_2.customer_can_afford_item(food_1))
示例#28
0
 def test_customer_can_afford_food(self):
     food_1 = Food("Chips", 1.50, 2)
     self.assertEqual(True,
                      self.customer_1.customer_can_afford_item(food_1))
示例#29
0
 def test_make_sober(self):
     food_1 = Food("Steak", 11.99, 8)
     self.customer_1.make_drunk(self.drink_2)
     self.customer_1.make_sober(food_1)
     self.assertEqual(2, self.customer_1.drunkenness)
示例#30
0
 def test_rejuvenation_level__True(self):
     customer = Customer("Bob", 20.00, 40, 11)
     food = Food("Kebab", 4, 3)
     customer.rejuvenation_level(food)
     self.assertEqual(8, customer.drunkeness)
示例#31
0
def test_consumed():
    food = Food()
    food.consumed()
    assert food.location[0] < 600
    assert food.location[1] < 600