-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_of_life.py
More file actions
42 lines (32 loc) · 849 Bytes
/
game_of_life.py
File metadata and controls
42 lines (32 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def iterate(Z):
# Count neighbors
N = (
Z[0:-2,0:-2] + Z[0:-2, 1:-1] + Z[0:-2,2:] +
Z[1:-1,0:-2] + Z[1:-1,2:] +
Z[2: ,0:-2] + Z[2: ,1:-1] + Z[2: ,2:]
)
# Apply rules
birth = (N==3) & (Z[1:-1, 1:-1]==0)
survive = ((N==2) | (N==3)) & (Z[1:-1, 1:-1]==1)
Z[...] = 0
Z[1:-1, 1:-1][birth | survive] = 1
return Z
Z = np.random.randint(0,2,(300, 300))
Z[:125,:] = 0
Z[175:,:] = 0
Z[:,:125] = 0
Z[:,175:] = 0
#Z[50:250]=0
# Set up plot
fig, ax = plt.subplots()
img = ax.imshow(Z, cmap='binary')
def update(frame):
global Z
Z = iterate(Z)
img.set_data(Z)
return [img]
ani = FuncAnimation(fig, update, frames=100000, interval=.0001, blit=True)
plt.show()