Beispiel #1
0
    def test_living_neighbors(self):
        g = Generation(5, 5)
        g.assign_neighbors()

        cells = [[0, 1, 0, 0, 1], [0, 0, 0, 1, 0], [0, 1, 1, 1, 1],
                 [0, 0, 1, 1, 1], [1, 1, 1, 1, 1]]

        for row in range(5):
            for column in range(5):
                if cells[row][column] == 1:
                    g._cells[row][column].live()

        correctCount = [[1, 0, 2, 2, 1], [2, 3, 5, 4, 4], [1, 2, 5, 6, 4],
                        [3, 6, 7, 8, 5], [1, 3, 4, 5, 3]]

        for cell in g.cells():
            self.assertEqual(
                cell.living_neighbors(), correctCount[cell.row][cell.column],
                f'cell[{row}][{column}] != {correctCount[row][column]}')
Beispiel #2
0
 def test_assign_neighbors(self):
     rows = 3
     columns = 3
     g = Generation(rows, columns)
     g.populate_cells()
     #
     # Do we have the correct number of neighbors?
     #
     correctNeighborCount = [3, 5, 3, 5, 8, 5, 3, 5, 3]
     for index, cell in enumerate(g.cells()):
         self.assertEqual(len(cell.neighbors), correctNeighborCount[index])
     #
     # Tediously test each cell
     #
     c = g._cells
     #
     #    x..
     #    ...
     #    ...
     #
     neighbors = c[0][0].neighbors
     correctNeighbors = [c[0][1], c[1][0], c[1][1]]
     self.assertEqual(set(neighbors), set(correctNeighbors),
                      'error at c[0][0]')
     #
     #    .x.
     #    ...
     #    ...
     #
     neighbors = c[0][1].neighbors
     correctNeighbors = [c[0][0], c[0][2], c[1][0], c[1][1], c[1][2]]
     self.assertEqual(set(neighbors), set(correctNeighbors),
                      'error at c[0][1]')
     #
     #    ..x
     #    ...
     #    ...
     #
     neighbors = c[0][2].neighbors
     correctNeighbors = [c[0][1], c[1][1], c[1][2]]
     self.assertEqual(set(neighbors), set(correctNeighbors),
                      'error at c[0][2]')
     #
     #    ...
     #    x..
     #    ...
     #
     neighbors = c[1][0].neighbors
     correctNeighbors = [c[0][0], c[0][1], c[1][1], c[2][0], c[2][1]]
     self.assertEqual(set(neighbors), set(correctNeighbors),
                      'error at c[1][0]')
     #
     #    ...
     #    .x.
     #    ...
     #
     neighbors = c[1][1].neighbors
     correctNeighbors = [
         c[0][0], c[0][1], c[0][2], c[1][0], c[1][2], c[2][0], c[2][1],
         c[2][2]
     ]
     self.assertEqual(set(neighbors), set(correctNeighbors),
                      'error at c[1][1]')
     #
     #    ...
     #    ..x
     #    ...
     #
     neighbors = c[1][2].neighbors
     correctNeighbors = [c[0][1], c[0][2], c[1][1], c[2][1], c[2][2]]
     self.assertEqual(set(neighbors), set(correctNeighbors),
                      'error at c[1][2]')
     #
     #    ...
     #    ...
     #    x..
     #
     neighbors = c[2][0].neighbors
     correctNeighbors = [c[1][0], c[1][1], c[2][1]]
     self.assertEqual(set(neighbors), set(correctNeighbors),
                      'error at c[2][0]')
     #
     #    ...
     #    ...
     #    .x.
     #
     neighbors = c[2][1].neighbors
     correctNeighbors = [c[1][0], c[1][1], c[1][2], c[2][0], c[2][2]]
     self.assertEqual(set(neighbors), set(correctNeighbors),
                      'error at c[2][1]')
     #
     #    ...
     #    ...
     #    ..x
     #
     neighbors = c[2][2].neighbors
     correctNeighbors = [c[1][1], c[1][2], c[2][1]]
     self.assertEqual(set(neighbors), set(correctNeighbors),
                      'error at c[2][2]')