def test_correctly_snapshot_ticks_a_3x3_grid_with_centered_cell_and_2_adjacent_cells(): grid = Grid(width=3, height=3) grid.populate_cells([(0, 0), (0, 1), (1, 0)]) grid.advance_generation() assert len(grid.matrix) == 3*3 # space at position (1,1) spawned a cell as it has 3 adjancent assert grid.matrix == [1, 1, 0, 1, 1, 0, 0, 0, 0]
def test_given_a_1x1_grid_a_full_snapshot_tick_returns_another_1x1_grid(): grid = Grid(width=1, height=1) grid.populate_cells([(0, 0)]) grid.advance_generation() assert len(grid.matrix) == 1 assert grid.matrix[0] == 0
def test_on_a_3x3_grid_a_topleft_cell_space_doesnt_reproduces_if_has_less_than_3_adjacent_cells(): grid = Grid(width=3, height=3) grid.populate_cells([(0, 1), (1, 0)]) grid.advance_generation() assert grid.get_cell(0, 0) == 0
def test_on_a_3x3_grid_a_topleft_cell_dies_if_has_no_adjacent_cells(): grid = Grid(width=3, height=3) grid.populate_cells([(0, 0)]) grid.advance_generation() assert grid.get_cell(0, 0) == 0
def test_on_a_3x3_grid_a_topleft_cell_survives_if_has_exactly_2_adjacent_cells(): grid = Grid(width=3, height=3) grid.populate_cells([(0, 0), (0, 1), (1, 0)]) grid.advance_generation() assert grid.get_cell(0, 0) == 1
class GameOfLife(): def __init__(self, width, height, fullscreen=False): self.width = width self.height = height self.screen = pygame.display.set_mode((width, height)) self.paused = True if fullscreen: pygame.display.toggle_fullscreen() def start(self): self.screen.fill((0, 0, 0)) self.grid = Grid(self.width, self.height) self.rows_range = range(self.grid.height) self.cols_range = range(self.grid.width) # self._random_populated_cells(self.width, self.height) self._create_r_pentomino(63, 30) self._create_r_pentomino(63, 60) def run(self): clock = pygame.time.Clock() self.screen.fill((0, 0, 0)) self._draw_grid() pygame.display.flip() while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() self._check_keyboard() if not self.paused: self._update_grid() clock.tick(30) def _draw_grid(self): for y in self.rows_range: for x in self.cols_range: if self.grid.get_cell(x, y) == 1: self.screen.set_at((x, y), (0, 255, 0)) def _update_grid(self): self.grid.advance_generation() self.screen.fill((0, 0, 0)) self._draw_grid() pygame.display.flip() def _check_keyboard(self): key = pygame.key.get_pressed() if key[K_ESCAPE]: sys.exit() elif key[K_SPACE]: self.paused = False def _random_populated_cells(self, grid_width, grid_height, num_cells=None): cells = [] if num_cells is None: min_cells = grid_width + grid_height - 1 max_cells = ((grid_width - 1) * (grid_height - 1)) // 4 num_cells = random.randint(min_cells, max_cells) for index in range(num_cells): cells.append((random.randint(0, grid_width - 1), random.randint(0, grid_height - 1))) self.grid.populate_cells(cells) # More patterns here: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Examples_of_patterns def _create_r_pentomino(self, start_x, start_y): cells = [] cells.append((start_x + 1, start_y)) cells.append((start_x + 2, start_y)) cells.append((start_x, start_y + 1)) cells.append((start_x + 1, start_y + 1)) cells.append((start_x + 1, start_y + 2)) self.grid.populate_cells(cells)