In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Random Board:
state = np.random.choice([0, 1], size=(60, 60), p=[4./5, 1./5])
def advance(state):
""" game of life step using padding """
hood = np.pad(state, 1, 'wrap')
neighbours = hood[0:-2,0:-2] + hood[1:-1,0:-2] + hood[2:,0:-2] +\
hood[0:-2,1:-1] + hood[2:,1:-1] +\
hood[0:-2,2:] + hood[1:-1,2:] + hood[2:,2:]
return np.where(np.logical_or(neighbours == 3, np.logical_and(neighbours == 2, state == 1)), 1, 0)
def draw(state):
""" Draw a game of life board """
plt.imshow(state, interpolation='nearest')
In [2]:
state=advance(state)
draw(state)
In [ ]: