def init_cells(self): self.cells = [] # First cell counter = 1 for row in range(0, self.size): new_cells = [] for column in range(0, self.size): # See rotation matrix on Wikipedia if ever in doubt about this calculation angle = -math.pi / 4 x_pos = column y_pos = -row x = x_pos * math.cos(angle) - y_pos * math.sin(angle) y = x_pos * math.sin(angle) + y_pos * math.cos(angle) cell = Cell(counter, pos=(x, y), row=row, column=column) left_neighbour = new_cells[-1:] top_neighbours = [] if row != 0: top_neighbours = self.cells[row - 1][column:column + 2] neighbours = top_neighbours + left_neighbour cell.add_neighbour(*neighbours) new_cells.append(cell) counter += 1 self.cells.append(new_cells)
class CellTest(unittest.TestCase): def setUp(self): self.cell = Cell(True) self.cell2 = Cell(True) pass def tearDown(self): pass def testCellKnowsItsOwnState(self): assert self.cell.is_alive() == True, 'Cell has forgotten that it is alive' pass def testCellCanHaveNeigbhours(self): self.cell.add_neighbour(self.cell2) assert self.cell.has_neighbour() == True, 'Cell has no neighbour' pass def testCellShouldKnowItsCoordinates(self): coordinates = [3,5] cell = Cell(True, coordinates) assert cell.get_x() == 3, 'Cell remembered its x-coordinate incorrectly' assert cell.get_y() == 5, 'Cell remembered its y-coordinate incorrectly' def testCellCountsNumberOfNeighbours(self): assert self.cell.get_number_of_neighbours() == 0, 'Cell has not 0 neighbour' self.cell.add_neighbour(self.cell2) assert self.cell.get_number_of_neighbours() == 1, 'Cell has not 1 neighbour' pass def testCellCanDetectWheatherAnotherCellIsNeighboured(): cellA = Cell(True, [2,2]) cellB = Cell(True, [3,2]) cellC = Cell(True, [4,3]) assert cellA.is_neighboured_to(cellB) == True, 'A Cell has not detected another cell correctly as neighbour' assert cellB.is_neighboured_to(cellC) == True assert cellA.is_neighboured_to(cellC) == False
def init_cells(self): self.cells = [[Cell(1)]] # First cell counter = 2 for row in range(1, self.size): new_cells = [] for column in range(0, row + 1): x_pos = column y_pos = -row x = x_pos - row * 0.5 y = y_pos cell = Cell(counter, pos=(x, y), row=row, column=column) column_start = max(column - 1, 0) column_end = column + 1 top_neighbours = self.cells[row - 1][column_start:column_end] left_neighbour = new_cells[-1:] neighbours = top_neighbours + left_neighbour cell.add_neighbour(*neighbours) new_cells.append(cell) counter += 1 self.cells.append(new_cells)