#!/usr/bin/python import numpy as np import matplotlib.pyplot as plt GRIDSIZE = 50 # grid size in x- and y-direction INIT_CELLS = 1000 # number of cells initially set 'alive' # query number of 'living' neighbours def get_neighbours(grid, i, j): living = 0 """ Check all neighbours for status """ """ Insert your code here """ living = grid[i-1,j] + grid[i-1,j+1] + grid[i,j+1] + grid[i+1,j+1] + grid[i+1,j] + grid[i+1,j-1] + grid[i,j-1] + grid[i-1,j-1] return living # state transition t -> t + dt def update(grid_old, grid_new): for i in range(1, GRIDSIZE+1): for j in range(1, GRIDSIZE+1): nln = get_neighbours(grid_old, i, j) """ Update cells according to rules using value 'nln' """ """ Insert your code here """ if grid_old[i,j] == 0 and nln == 3: grid_new[i,j] = 1 elif grid_old[i,j] == 1 and nln < 2: grid_new[i,j] = 0 elif grid_old[i,j] == 1 and (nln == 2 or nln == 3): grid_new[i,j] = 1 elif grid_old[i,j] == 1 and nln > 3: grid_new[i,j] == 0 # allocate memory and initialise grids grid_old = np.zeros((GRIDSIZE+2, GRIDSIZE+2), dtype=np.int32) grid_new = np.zeros((GRIDSIZE+2, GRIDSIZE+2), dtype=np.int32) # set random starting points no_repeat = [] for k in range(INIT_CELLS): i = int(float(GRIDSIZE)*np.random.random())+1 j = int(float(GRIDSIZE)*np.random.random())+1 grid_old[i,j] = 1 # run updates plt.ion() plt.imshow(grid_old) plt.pause(0.1) for t in range(50): plt.clf() update(grid_old, grid_new) for i in range(GRIDSIZE+2): for j in range(GRIDSIZE+2): grid_old[i,j] = grid_new[i,j] grid_new[i,j] = 0 plt.imshow(grid_old) plt.pause(0.1) plt.ioff()