-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (63 loc) · 2.03 KB
/
main.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys, argparse
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def randomGrid(N, alive_ratio=0.2):
assert alive_ratio <= 1, "alive_ratio must be <= 1"
return np.random.choice([1, 0], N * N, p=[alive_ratio, 1 - alive_ratio]).reshape(
N, N
)
def update(frameNum, img, grid, N):
newGrid = grid.copy()
for i in range(N):
for j in range(N):
top_left = grid[(i - 1) % N, (j - 1) % N]
top = grid[(i - 1) % N, j]
top_right = grid[(i - 1) % N, (j + 1) % N]
left = grid[i, (j - 1) % N]
right = grid[i, (j + 1) % N]
bottom_left = grid[(i + 1) % N, (j - 1) % N]
bottom = grid[(i + 1) % N, j]
bottom_right = grid[(i + 1) % N, (j + 1) % N]
neighbors_count = (
top_left
+ top
+ top_right
+ left
+ right
+ bottom_left
+ bottom
+ bottom_right
)
if grid[i, j] == 1 and (neighbors_count < 2 or neighbors_count > 3):
newGrid[i, j] = 0
elif neighbors_count == 3:
newGrid[i, j] = 1
img.set_data(newGrid)
grid[:] = newGrid[:]
return (img,)
def main():
parser = argparse.ArgumentParser(description="Conway's Game of Life")
parser.add_argument(
"--size", dest="grid_size", type=int, default=16, help="the grid size"
)
parser.add_argument(
"--interval", dest="interval", type=int, default=20, help="the update interval"
)
args = parser.parse_args()
GRID_SIZE = args.grid_size
interval = args.interval
grid = randomGrid(GRID_SIZE)
fig, ax = plt.subplots()
plt.axis("off")
img = ax.imshow(grid, interpolation="nearest")
ani = animation.FuncAnimation(
fig,
update,
fargs=(img, grid, GRID_SIZE),
frames=10,
interval=interval,
)
plt.show()
if __name__ == "__main__":
main()