def test_get_available_case_at_center(): adjacent_cases = Coord(5, 5).adjacent(10, 10) assert len(adjacent_cases) == 4 assert Coord(4, 5) in adjacent_cases assert Coord(6, 5) in adjacent_cases assert Coord(5, 6) in adjacent_cases assert Coord(5, 4) in adjacent_cases
def test_two_rabbits_cant_breed_if_age_less_than_3(): rabbits_count = 2 starting_position = [Coord(0, 0), Coord(0, 2)] world = World(1, 3, rabbits_count, 0, CoordGenerator(starting_position)) world.launch_round() assert world.territories[1].new_child_rabbit() == 0
def two_fox_simulation(): fox_count = 2 starting_position = [Coord(0, 0), Coord(0, 2)] world = World(1, 3, 0, fox_count, CoordGenerator(starting_position)) world.territories[0].foxes[0].feed() world.territories[2].foxes[0].feed() world.launch_round() return world
def __init__(self, line=0, column=0): self.rabbits = [] self.temp_rabbit = [] self.foxes = [] self.temp_fox = [] self.born_foxes = 0 self.born_rabbits = 0 self.coord = Coord(line, column)
def __init__(self, coord=None, random=False, max_line=0, max_colum=0): if coord is None and not random: coord = [Coord(0, 0), Coord(0, 0), Coord(2, 2)] self.coord = coord self.index = 0 self.random = random self.max_line = max_line self.max_column = max_colum
def test_one_fed_and_one_hungry_fox_should_breed_no_child(): fox_count = 2 starting_position = [Coord(0, 0), Coord(0, 2)] world = World(1, 3, 0, fox_count, CoordGenerator(starting_position)) world.territories[0].foxes[0].feed() world.launch_round() assert world.territories[1].fox_count() == 2
def next_coord(self): if self.random: return Coord(random.choice(range(self.max_line)), random.choice(range(self.max_column))) coord = self.coord[self.index] self.index += 1 return coord
def test_rabbit_should_wait_three_rounds_before_procreating_again(): rabbits_count = 2 starting_position = [Coord(0, 0), Coord(0, 0)] world = World(1, 2, rabbits_count, 0, CoordGenerator(starting_position)) for i in range(4): world.launch_round() assert world.territories[0].rabbit_count() == 3 world.launch_round() assert world.territories[1].rabbit_count() == 3 world.launch_round() world.launch_round() world.launch_round() assert world.territories[0].rabbit_count() == 4
def test_multiple_rabbit_should_move_from_one_territory_to_another(): rabbit_count = 3 world = World(3, 3, rabbit_count, 0, CoordGenerator()) world.launch_round() assert rabbit_count_for(world, Coord(0, 0)) == 0 assert rabbit_count_for(world, Coord(2, 2)) == 0 assert rabbit_count_for(world, Coord(0, 1)) >= 1 or rabbit_count_for( world, Coord(1, 0)) >= 1 assert rabbit_count_for(world, Coord(1, 2)) == 1 or rabbit_count_for( world, Coord(2, 1)) == 1
class Territory: def __init__(self, line=0, column=0): self.rabbits = [] self.temp_rabbit = [] self.foxes = [] self.temp_fox = [] self.born_foxes = 0 self.born_rabbits = 0 self.coord = Coord(line, column) def add_rabbit(self, rabbit): self.rabbits += [rabbit] def add_new_rabbit(self, rabbit): self.temp_rabbit += [rabbit] def add_fox(self, fox): self.foxes += [fox] def add_new_fox(self, fox): self.temp_fox += [fox] def end_migration(self): self.rabbits = self.temp_rabbit self.foxes = self.temp_fox self.temp_rabbit = [] self.temp_fox = [] def remove_one_fox(self): fox = self.foxes[0] self.foxes = self.foxes[1::] fox.increment_age() return fox def remove_one_rabbit(self): rabbit = self.rabbits[0] self.rabbits = self.rabbits[1::] return rabbit def fox_count(self): return len(self.foxes) def rabbit_count(self): return len(self.rabbits) def is_occupied(self): return len(self.rabbits) != 0 or len(self.foxes) != 0 def life_happen(self): self.start_hunt() self.proceed_foxes() self.proceed_rabbits() def proceed_foxes(self): self.born_foxes = 0 fed_foxes = self.get_fed_foxes() breed = Breed(len(fed_foxes)) self.born_foxes = breed.new_children() for x in range(self.born_foxes * 2): fed_foxes[x].had_breed() def get_fed_foxes(self): return list(filter(lambda fox: fox.can_breed(), self.foxes)) def proceed_rabbits(self): self.born_rabbits = 0 mature_rabbits = self.get_mature_rabbits() breed = Breed(len(mature_rabbits)) self.born_rabbits = breed.new_children() [rabbit.did_procreate() for rabbit in mature_rabbits] def start_hunt(self): hungry_foxes = self.get_hungry_foxes() hunt = Hunt(hungry_foxes, self.rabbits) hunt.launch() self.rabbits = self.get_living_rabbits() def get_hungry_foxes(self): return list(filter(lambda fox: not fox.is_fed(), self.foxes)) def get_mature_rabbits(self): return list(filter(lambda rabbit: rabbit.can_breed(), self.rabbits)) def get_living_rabbits(self): return list(filter(lambda rabbit: not rabbit.is_dead(), self.rabbits)) def new_child_fox(self): return self.born_foxes def new_child_rabbit(self): return self.born_rabbits def adjacent(self, max_line, max_column): return self.coord.adjacent(max_line, max_column)
def test_one_rabbit_start_at_a_random_position(): rabbit_count = 1 world = World(10, 10, rabbit_count, 0, CoordGenerator()) assert rabbit_count_for(world, Coord(0, 0)) == 1 assert rabbit_count_for(world, Coord(4, 4)) == 0
def test_rabbit_move_to_random_case(): movement = RabbitMovement() input_coord = [Coord(0, 0), Coord(1, 1), Coord(7, 23)] next_case = movement.next_coord(input_coord) assert next_case in input_coord
def test_available_case_at_right_bottom_corner(): adjacent_cases = Coord(9, 9).adjacent(10, 10) assert len(adjacent_cases) == 2 assert Coord(8, 9) in adjacent_cases assert Coord(9, 8) in adjacent_cases
def test_one_fox_start_at_a_random_position(): fox_count = 1 world = World(10, 10, 0, fox_count, CoordGenerator()) assert fox_count_for(world, Coord(0, 0)) == 1 assert fox_count_for(world, Coord(3, 6)) == 0
def test_available_case_at_left_bottom_corner(): adjacent_cases = Coord(9, 0).adjacent(10, 10) assert len(adjacent_cases) == 2 assert Coord(8, 0) in adjacent_cases assert Coord(9, 1) in adjacent_cases
def test_availables_case_at_right_top_corner(): adjacent_cases = Coord(0, 9).adjacent(10, 10) assert len(adjacent_cases) == 2 assert Coord(0, 8) in adjacent_cases assert Coord(1, 9) in adjacent_cases
def test_availabe_case_at_non_corner_edge(): adjacent_cases = Coord(5, 9).adjacent(10, 10) assert len(adjacent_cases) == 3 assert Coord(5, 8) in adjacent_cases assert Coord(4, 9) in adjacent_cases assert Coord(6, 9) in adjacent_cases
def test_fox_move_to_random_case(): movement = FoxMovement() input_coord = [Coord(0,0), Coord(2,2), Coord(3,3)] next_case = movement.next_coord(input_coord) assert next_case in input_coord
def test_fox_should_move_from_one_territory_to_another(): fox_count = 1 world = World(1, 2, 0, fox_count, CoordGenerator()) world.launch_round() assert fox_count_for(world, Coord(0, 0)) == 0 assert fox_count_for(world, Coord(0, 1)) == 1