-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLifeConvolution.py
66 lines (48 loc) · 2.49 KB
/
GameOfLifeConvolution.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
import numpy as np
import matplotlib.pyplot as plt
import imageio
import os
from scipy import signal
# Matrix size (N x N)
N = 50;
#present = np.random.randint(0, 2, [N, N]); # random N x N matrix with 0 or 1
present = np.zeros(N*N).reshape(N, N) # N x N matrix with all elements = 0
def block(matrix, x, y):
matrix[x: x+2, y: y+2] = np.array([[1, 1], [1, 1]]);
def glider(matrix, x, y):
matrix[x: x+3, y: y+3] = np.array([[0, 1, 0], [0, 0, 1], [1, 1, 1]]);
def spaceship(matrix, x, y):
matrix[x: x+4, y: y+6] = np.array([[0,0,0,1,1,0], [1,1,1,0,1,1], [1,1,1,1,1,0], [0,1,1,1,0,0]])
def toad(matrix, x, y):
matrix[x: x+2, y: y+4] = np.array([[0,1,1,1], [1,1,1,0]]);
def dieHard(matrix, x, y):
matrix[x: x+3, y: y+8] = np.array([[0,0,0,0,0,0,1,0], [1,1,0,0,0,0,0,0], [0,1,0,0,0,1,1,1]]);
def dora_cell(matrix, x, y):
matrix[x: x+2, y: y+5] = np.array([[0, 0, 1, 1, 1], [1, 1, 1, 0, 0]]);
def glider_gun(matrix, x, y):
matrix[x: x+9, y: y+36] = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1], [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1], [1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]])
kernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]);
glider_gun(present, 10, 8)
files = [];
plt.imshow(present, cmap="binary");
plt.title(f"Game of Life - Generation 0");
filename1 = f"{0}.png";
files.append(filename1);
plt.savefig(filename1);
for n in range(1, 180):
convol_world = signal.convolve2d(present, kernel, mode="same", boundary="wrap" );
present = (((present == 1) & (convol_world > 1) & (convol_world < 4)) | ((present == 0) & (convol_world == 3)));
plt.imshow(present, cmap="binary");
plt.title(f"Game of Life - Generation {n}");
filename = f"{n}.png";
files.append(filename);
plt.savefig(filename);
with imageio.get_writer('dieHard.gif', mode='I') as writer:
for filename in files:
image = imageio.imread(filename);
writer.append_data(image);
for filename in set(files):
os.remove(filename);