示例#1
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)
示例#2
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)
示例#3
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)
示例#4
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)
示例#5
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))
示例#6
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)
示例#7
0
 def test_spaced_living_row_cells_create_proper_world(self):
     world = render_to_world('+-+')
     for living, coordinates in [(True, (0, 0)),
                                 (False, (1, 0)),
                                 (True, (2, 0))]:
         with self.subTest(coordinates=coordinates):
             self.assertEqual(living,
                              world.is_alive_at(Location(*coordinates)))
示例#8
0
 def test_column_cells_create_proper_world(self):
     world = render_to_world('+\n'
                             '+')
     for living, coordinates in [(True, (0, 0)),
                                 (True, (0, 1))]:
         with self.subTest(coordinates=coordinates):
             self.assertEqual(living,
                              world.is_alive_at(Location(*coordinates)))
示例#9
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)
示例#10
0
    def test_locations_keyed_and_sorted_together(self):
        sorted_locations = sort_locations(
            [Location(0, 0),
             Location(0, 1),
             Location(1, 0),
             Location(1, 1)])
        expected = {
            0: [Location(0, 0), Location(1, 0)],
            1: [Location(0, 1), Location(1, 1)]
        }

        self.assertEqual(dict(sorted_locations), expected)
示例#11
0
 def test_complicated_rendering_creates_proper_world(self):
     world = render_to_world('-+-\n'
                             '+--\n'
                             '-++')
     for living, coordinates in [(0, (0, 2)), (1, (1, 2)), (0, (2, 2)),
                                 (1, (0, 1)), (0, (1, 1)), (0, (2, 1)),
                                 (0, (0, 0)), (1, (1, 0)), (1, (2, 0))]:
         with self.subTest(coordinates=coordinates):
             self.assertEqual(bool(living),
                              world.is_alive_at(Location(*coordinates)))
示例#12
0
    def test_locations_sorted_properly_by_desc_y_index(self):
        sorted_locations = sort_locations(
            [Location(0, 5), Location(0, 3),
             Location(0, 7)])
        expected = OrderedDict([(7, [Location(0, 7)]), (5, [Location(0, 5)]),
                                (3, [Location(0, 3)])])

        self.assertEqual(sorted_locations, expected)
示例#13
0
 def test_returns_proper_locations(self):
     actual = LocationGrid(Location(0, 0), Location(1, 1)).locations
     expected = [
         Location(0, 0),
         Location(1, 0),
         Location(0, 1),
         Location(1, 1)
     ]
     self.assertEqual(set(actual), set(expected))
示例#14
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)
示例#15
0
 def setUp(self):
     self.location = Location(0, 0)
示例#16
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))
示例#17
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))
示例#18
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)
示例#19
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)
示例#20
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)
示例#21
0
 def test_with_proper_minimum_and_maximum(self):
     actual = LocationGrid(Location(0, 0), Location(2, 2)).rows
     expected = {
         0: [Location(0, 0), Location(1, 0),
             Location(2, 0)],
         1: [Location(0, 1), Location(1, 1),
             Location(2, 1)],
         2: [Location(0, 2), Location(1, 2),
             Location(2, 2)]
     }
     self.assertEqual(dict(actual), expected)
示例#22
0
 def test_lone_location_returns_lone_location(self):
     actual = sort_locations([Location(0, 0)])
     expected = {0: [Location(0, 0)]}
     self.assertEqual(dict(actual), expected)
示例#23
0
def _get_location_cluster(cluster_number):
    location = Location(0, 0)
    return [location] + location.neighbors[:cluster_number]
示例#24
0
 def test_instances_of_location_with_same_coordinates_are_equal(self):
     location_a, location_b = Location(0, 0), Location(0, 0)
     self.assertTrue(location_a == location_b)
示例#25
0
 def test_with_one_location(self):
     actual = get_max_coordinates_location([Location(0, 0)])
     self.assertEqual(actual, Location(0, 0))
示例#26
0
 def test_locations_at_same_y_coordinate_sorted_by_asc_x_coordinate(self):
     sorted_locations = sort_locations(
         [Location(0, 0), Location(-1, 0),
          Location(1, 0)])
     expected = {0: [Location(-1, 0), Location(0, 0), Location(1, 0)]}
     self.assertEqual(dict(sorted_locations), expected)
示例#27
0
 def test_locations_indexed_by_y_axis(self):
     sorted_locations = sort_locations([Location(0, 1), Location(0, 0)])
     expected = {0: [Location(0, 0)], 1: [Location(0, 1)]}
     self.assertEqual(dict(sorted_locations), expected)
示例#28
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)
示例#29
0
 def test_with_multiple_locations(self):
     actual = get_max_coordinates_location(
         [Location(1, 0), Location(0, 7),
          Location(5, -3)])
     self.assertEqual(actual, Location(5, 7))
示例#30
0
 def test_same_start_end(self):
     actual = LocationGrid(Location(0, 0), Location(0, 0)).rows
     expected = OrderedDict({0: [Location(0, 0)]})
     self.assertEqual(actual, expected)