Example #1
0
def _get_stable_world():
    world = World()
    stable_location_cluster = _get_location_cluster(
        Cell.STABLE_NEIGHBOR_RANGE[0] + 1)

    for location in stable_location_cluster:
        world.set_living_at(location)

    return world
Example #2
0
    def test_is_alive_at_not_dependent_on_location_instance(self):
        coordinates = (0, 0)
        self.location = Location(*coordinates)
        world = World()
        world.set_living_at(self.location)

        location = Location(*coordinates)

        self.assertTrue(world.is_alive_at(location))
Example #3
0
 def test_world_can_render_with_arbitrary_characters(self):
     world = World.empty()
     world.set_living_at(Location(0, 0))
     world.set_living_at(Location(2, 0))
     render = WorldRenderer(world).render()
     expected = 'XOX'
     self.assertEqual(render, expected)
Example #4
0
    def test_world_renders_according_to_dimensions(self):
        world = World.empty(min_location=Location(0, 0),
                            max_location=Location(2, 0))

        render = WorldRenderer(world).render()
        expected = ('---')
        self.assertEqual(render, expected)
Example #5
0
 def test_world_that_is_vertical_renders_properly(self):
     world = World.empty()
     world.set_living_at(Location(0, 0))
     world.set_living_at(Location(0, 2))
     render = WorldRenderer(world).render()
     expected = ('+\n' '-\n' '+')
     self.assertEqual(render, expected)
Example #6
0
 def test_world_with_live_and_dead_cell_renders_as_expected(self):
     world = World.empty()
     world.set_living_at(Location(0, 0))
     world.set_living_at(Location(2, 0))
     render = WorldRenderer(world).render()
     expected = '+-+'
     self.assertEqual(render, expected)
Example #7
0
    def test_a_world_with_one_cell_is_empty_after_a_tick(self):
        # Potential smell here, given that we have to use real location
        # instance here for this to pass. Mock approach will not work.
        self.location = Location(0, 0)

        world = World.empty()
        world.set_living_at(self.location)
        next_world = world.tick()
        self.assertTrue(next_world.is_empty)
Example #8
0
def play_demo():
    """
    Plays demo of Game of Life over 20 turns to stdout.
    """
    world = World.random(min_location=Location(0, 0),
                         max_location=Location(20, 20),
                         cell_count=50)

    for rendering in turn_renderings(world, turns=20):
        clear_stdout()
        print(rendering)
Example #9
0
 def test_a_cell_can_be_added_to_the_world(self):
     world = World()
     world.set_living_at(self.location)
     self.assertTrue(world.is_alive_at(self.location))
Example #10
0
 def test_a_cell_can_be_set_dead_at_location_if_never_set_living(self):
     world = World()
     world.set_dead_at(self.location)
     self.assertFalse(world.is_alive_at(self.location))
Example #11
0
 def test_a_world_with_a_living_cell_is_not_empty(self):
     world = World()
     world.set_living_at(self.location)
     self.assertFalse(world.is_empty)
Example #12
0
 def test_a_new_worlds_cell_is_not_alive(self):
     world = World()
     self.assertFalse(world.is_alive_at(self.location))
Example #13
0
 def test_an_empty_world_stays_empty_after_a_tick(self):
     world = World.empty()
     next_world = world.tick()
     self.assertTrue(next_world.is_empty)
Example #14
0
 def test_an_empty_world_with_10_by_10_dimensions_has_100_dead_cells(self):
     world = World.empty(min_location=Location(0, 0),
                         max_location=Location(9, 9))
     self.assertEqual(world.dead_cell_count, 100)
Example #15
0
    def test_set_living_at_below_min_location_y_lowers_min_location(self):
        world = World.empty(min_location=Location(0, 0),
                            max_location=Location(3, 3))

        world.set_living_at(Location(0, -1))
        self.assertEqual(world.dimensions, (4, 5))
Example #16
0
 def test_can_get_cell_at_location(self):
     world = World()
     world.set_living_at(self.location)
     self.assertIsInstance(world.get_cell_at(self.location), Cell)
Example #17
0
 def test_world_dimensions_come_from_min_max_locations(self):
     world = World.empty(min_location=Location(0, 0),
                         max_location=Location(3, 3))
     self.assertEqual((4, 4), world.dimensions)
Example #18
0
 def test_empty_world_renders_as_expected(self):
     world = World.empty()
     render = WorldRenderer(world).render()
     expected = '-'
     self.assertEqual(render, expected)
Example #19
0
 def test_world_has_default_dimensions(self):
     world = World.empty()
     self.assertEqual((1, 1), world.dimensions)
Example #20
0
 def test_2_by_2_world_with_one_living_cell_has_3_dead_cells(self):
     world = World.empty(min_location=Location(0, 0),
                         max_location=Location(1, 1))
     world.set_living_at(self.location)
     self.assertEqual(world.dead_cell_count, 3)
Example #21
0
 def test_an_empty_world_with_2_by_2_dimensions_has_4_dead_cells(self):
     world = World.empty(min_location=Location(0, 0),
                         max_location=Location(1, 1))
     self.assertEqual(world.dead_cell_count, 4)
Example #22
0
 def test_cell_can_be_set_dead_and_retrieved_if_never_set_living(self):
     world = World()
     world.set_dead_at(self.location)
     self.assertIsInstance(world.get_cell_at(self.location), Cell)
Example #23
0
 def test_empty_world_has_0_living_cell_count(self):
     world = World.empty()
     self.assertEqual(world.living_cell_count, 0)
Example #24
0
 def test_living_locations_includes_living_cells(self):
     world = World.empty()
     world.set_living_at(self.location)
     self.assertEqual(world.living_locations, [self.location])
Example #25
0
    def test_can_get_a_random_world(self):
        world = World.random(min_location=Location(0, 0),
                             max_location=Location(3, 3),
                             cell_count=12)

        self.assertEqual(world.living_cell_count, 12)
Example #26
0
 def test_get_cell_at_location_without_cell_returns_dead_cell(self):
     world = World()
     self.assertIsInstance(world.get_cell_at(self.location), Cell)
     self.assertFalse(world.get_cell_at(self.location).is_alive)
Example #27
0
 def test_a_new_world_is_empty(self):
     world = World()
     self.assertTrue(world.is_empty)
Example #28
0
    def test_set_living_at_beyond_max_location_y_expands_max_location(self):
        world = World.empty(min_location=Location(0, 0),
                            max_location=Location(3, 3))

        world.set_living_at(Location(0, 4))
        self.assertEqual(world.dimensions, (4, 5))
Example #29
0
 def test_dead_locations_not_in_living_locations(self):
     world = World.empty()
     world.set_dead_at(self.location)
     self.assertEqual(world.living_locations, [])