def test_whenHashingMonsterWithSamePosition_thenHashesAreTheSame(self): monster_1 = NormalMonster(Position(X_1, Y_1), SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) monster_2 = NormalMonster(Position(X_1, Y_1), SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) self.assertEqual(hash(monster_1), hash(monster_2))
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)
def test_givenMonsterWantsToMoveIntoAnOccupiedPosition_whenPlayingTurn_thenMonsterDoesntMove( self): monster = NormalMonster(SOME_POSITION, SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) self.monsters_mock.move.side_effect = PositionAlreadyOccupied monster.play_turn(self.monsters_mock, WorldElements(set())) self.monsters_mock.move.assert_called_once()
def test_givenMonsterDoesntHaveEnoughEnergyToMove_whenPlayingTurn_thenMonsterDies( self): monster = NormalMonster(SOME_POSITION, SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) self.monsters_mock.move.side_effect = NotEnoughEnergyToMove monster.play_turn(self.monsters_mock, WorldElements(set())) self.monsters_mock.remove.assert_called_once_with(monster)
def test_givenNoFoodsSurroundingMonster_whenPlayingTurn_thenMonsterMovesAnyWhere( self): monster = NormalMonster(SOME_POSITION, SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) monster.play_turn(self.monsters_mock, WorldElements(set())) self.monsters_mock.move.assert_called_once_with( monster, Not(Position(0, 0)), SOME_ENERGY)
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)
def test_givenDeltaPosition_whenMovingMonster_thenMonsterIsMovedByDeltaPosition( self): initial_position = Position(X_1, Y_1) delta_position = Position(X_2, Y_2) moved_monster = NormalMonster(initial_position, SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME).move_by(delta_position) expected_monster = NormalMonster(Position(X_1 + X_2, Y_1 + Y_2), SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) self.assertEqual(hash(expected_monster), hash(moved_monster))
def test_givenMonsterDoesntHaveChanceToReproduce_whenPlayingTurn_thenMonsterDoesntReproduces( self, generate_fraction): generate_fraction.return_value = 2 * SOME_REPRODUCTION_PROBABILITY enough_energy_to_reproduce = Energy(10 - 1) monster = NormalMonster( SOME_POSITION, enough_energy_to_reproduce, RANGE_OF_MOTION, MONSTER_NAME, SOME_REPRODUCTION_PROBABILITY, ) monster.play_turn(self.monsters_mock, WorldElements(set())) self.monsters_mock.add.assert_not_called()
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)
def test_whenMovingElement_thenElementIsmovedToNewPosition(self): energy = create_autospec(Energy) world_elements = WorldElements({A_MONSTER}) world_elements.move(A_MONSTER, A_DELTA_POSITION, energy) expected_element = NormalMonster(A_POSITION + A_DELTA_POSITION, SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) self.assertIn(expected_element, world_elements)
def test_givenElementNotInRange_whenGettingSurroundingElements_thenElementNotInIterable( self): range_of_motion = 2 a_monster_in_range = NormalMonster( A_POSITION + Position(range_of_motion + 1, 0), SOME_ENERGY, range_of_motion, MONSTER_NAME, ) world_elements = WorldElements({a_monster_in_range}) surrouding_elements = world_elements.elements_surrounding( A_POSITION, range_of_motion) self.assertNotIn(a_monster_in_range, surrouding_elements)
STEP = 1 X_1 = 1 X_2 = 3 Y_1 = 2 Y_2 = 4 SOME_REPRODUCTION_PROBABILITY = 0.1 SOME_ENERGY_VALUE = 10 SOME_FOOD_ENERGY_VALUE = 15 SOME_ENERGY = Energy(SOME_ENERGY_VALUE) RANGE_OF_MOTION = 1 MONSTER_NAME = "name" SOME_POSITION = Position(X_1, Y_1) A_MONSTER = NormalMonster(SOME_POSITION, SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) DELTA_POSITION_WITHIN_RANGE = Position(RANGE_OF_MOTION, 0) class NormalMonsterTest(unittest.TestCase): def setUp(self): self.monsters_mock = create_autospec(WorldElements) def test_whenHashingMonsterWithSamePosition_thenHashesAreTheSame(self): monster_1 = NormalMonster(Position(X_1, Y_1), SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) monster_2 = NormalMonster(Position(X_1, Y_1), SOME_ENERGY, RANGE_OF_MOTION, MONSTER_NAME) self.assertEqual(hash(monster_1), hash(monster_2))