def _next(b):
k = len(b)
nb = [[b[i][j] for j in range(k)] for i in range(k)]
for i in range(k):
for j in range(k):
s = 0
for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)):
x, y = i + di, j + dj
if 0 <= x < k and 0 <= y < k and b[x][y] > 3:
nb[x][y] -= 1
nb[i][j] += 1
return nb
def pretty_print(board):
return '\n'.join(''.join(" .:*"[x] for x in row) for row in board)
def hash(arr):
return '#'.join(','.join(map(str, row)) for row in arr)
def evolve(p, m):
n = 2 * p + 1
board = [[0 for _ in range(n)] for _ in range(n)]
board[p][p] = m
step = 0
lh = ''
while 1:
board = _next(board)
h = hash(board)
if lh == h:
break
lh = h
step += 1
state = pretty_print(board)
print(step, state)
evolve(6,200)
To embed this program on your website, copy the following code and paste it into your website's HTML: