def test_nearest_in_partition(self, positions_and_item): positions, (position, character) = positions_and_item roster = Roster.partitioned( positions, area_containing(positions), partition_func=character_colour, ) partition_key = "blue" assume( any(char.colour == partition_key and char != character for (_, char) in positions.items())) nearest = roster.nearest_to(position, key=partition_key) assert nearest is not None nearest_position, nearest_character = nearest.position, nearest.character assert nearest_position != position assert nearest_character != character best_distance = min((p - position).distance for p, c in positions.items() if c != character and c.colour == partition_key) assert best_distance == (nearest_position - position).distance
def test_zombie_approaches_human(self): zombie = default_zombie() human = default_human() characters = {Point(0, 0): zombie, Point(2, 2): human} area = Area(Point(0, 0), Point(3, 3)) roster = Roster.partitioned(characters, area=area, partition_func=LifeState.for_character) roster = Tick(roster).next() assert sorted(roster.positions) == [(Point(1, 1), zombie), (Point(2, 2), human)]
def rosters(draw, inhabitants=st.one_of(st.builds(default_human), st.builds(default_zombie))): width, height = draw(world_dimensions), draw(world_dimensions) assume(width > 0) assume(height > 0) area = Area(Point(0, 0), Point(width, height)) points = st.builds( Point, st.integers(min_value=0, max_value=width - 1), st.integers(min_value=0, max_value=height - 1), ) characters = draw(st.dictionaries(points, inhabitants)) return Roster.partitioned(characters, area=area, partition_func=LifeState.for_character)
def __init__( self, area: Area, population: Iterable[Optional[Character]], barriers: Barriers = Barriers.NONE, ): character_positions = (p for p in area if not barriers.occupied(p)) starting_positions = { point: character for point, character in zip(character_positions, population) if character is not None } self._roster = Roster.partitioned( starting_positions, area=area, partition_func=LifeState.for_character)