-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOfLifeImpl.py
More file actions
70 lines (58 loc) · 2.05 KB
/
Copy pathGameOfLifeImpl.py
File metadata and controls
70 lines (58 loc) · 2.05 KB
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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# array = np.zeros((100, 100))
# center = (50, 50)
# radius = 10
# x, y = np.indices((100, 100))
# distance = np.sqrt((x - center[0]) ** 2 + (y - center[1]) ** 2)
# array[distance <= radius] = 1
array = np.random.randint(2, size=(150, 150)) < 0.001
array = array.astype(int)
# array = np.zeros((100, 100))
# array[1][40:60] = 1
# array[99][40:60] = 1
age = np.zeros_like(array)
age[1][1] = 1
# Function to update the plot in each animation frame, with GoL rules
def update(frame):
global array, age
new_array = array.copy()
kernel = np.ones((3, 3))
conv_sum = 0
for i in range(array.shape[0]):
for j in range(array.shape[1]):
conv_sum = 0
# kernel_sum = np.sum(array[max(0, i - 1):min(100, i + 2), max(0, j - 1):min(100, j + 2)])
# conv_sum = kernel_sum - array[i][j]
kernel_sum = 0
for m in range(kernel.shape[0]):
for n in range(kernel.shape[1]):
row_idx = ((i + m) % array.shape[0]) - 1
col_idx = ((j + n) % array.shape[1]) - 1
kernel_sum += array[row_idx, col_idx]
conv_sum = kernel_sum - array[i][j]
if array[i][j] == 0 and conv_sum == 3:
age[i][j] += 1
new_array[i][j] = 1
elif array[i][j] == 1 and (conv_sum < 2 or conv_sum > 3):
new_array[i][j] = 0
age[i][j] = 0
elif array[i][j] == 1:
age[i][j] += 1
array = new_array
normalized_age = age / np.max(age)
age_status = 0
if age_status == 0:
im.set_array(array)
elif age_status == 1:
im.set_array(normalized_age)
return [im]
# Create a figure and axis
fig, ax = plt.subplots()
# Display the initial array as an image
im = ax.imshow(array, cmap='hot', animated=True)
# Set up the animation
ani = FuncAnimation(fig, update, frames=range(10000), interval=100, blit=True)
plt.colorbar(im)
plt.show()