def game_of_life_loop(board): while True: to_update = [] new_board = Board(cols, rows) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: board.reset() draw_grid(board) draw_start_pattern(board) return for x in range(cols): for y in range(rows): cell = board.get_cell(x, y) state = next_state(cell, board) if state: new_board.get_cell(x, y).alive = True to_update.append(new_board.get_cell(x, y)) elif not state: new_board.get_cell(x, y).alive = False to_update.append(new_board.get_cell(x, y)) board = new_board for s in to_update: update_cell(s) pygame.display.flip() # Higher number, higher speed clock.tick(12)
def apply_action(self, board: Board): mod_cell = board.get_cell(self.x(), self.y()) if mod_cell.is_initial(): return new_cell = mod_cell.clear_possible_value(self._value) board.set_cell(new_cell)
def apply_action(self, board: Board): mod_cell = board.get_cell(self.x(), self.y()) if mod_cell.is_initial(): return if mod_cell.value() is None: return new_cell = mod_cell.clear_value(board) board.set_cell(new_cell)
def apply_action(self, board: Board): mod_cell = board.get_cell(self.x(), self.y()) if mod_cell.is_initial(): return new_cell = mod_cell.set_value(self._value, self._initial) board.set_cell(new_cell) link_cells = board.linked_cells(mod_cell.x(), mod_cell.y()) link_cells.remove(new_cell) for cell in link_cells: _clear_possible_action(board, cell, self._value)
def set_board(self, board: board_func.Board) -> None: if self.__board.max_val() != board.max_val(): raise ValueError( "GUIBoard cannot adjust to different-sized boards!") self.__board = board # The GUI board owns the GUI cells for x in range(board.max_val()): for y in range(board.max_val()): board_cell = board.get_cell(x, y) self.__gui_cells[x][y].set_cell(board_cell)