-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life_ver_02.py
56 lines (43 loc) · 1.66 KB
/
game_of_life_ver_02.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
#Game of life implementation with periodic boundaries or wrapped up boundaries
#Hitarth Choubisa
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.ndimage import convolve
import pandas as pd
class Game_Of_Life():
def __init__(self, width, length, p=0.3):
self.width = width
self.length = length
self.state = np.zeros((length,width))
self.initial_distribution(1,length,width,p)
self.im = plt.imshow(self.state)
def initial_distribution(self,choice,length,width,p):
if choice==0:
self.state[1,2]=1;
self.state[2,2]=1;
self.state[3,2]=1;
else:
self.state = np.random.choice(a=[1, 0], size=(length,width), p=[p, 1 - p])
def start(self):
self.plot()
self.ani= FuncAnimation(plt.gcf(), self.evolution, repeat=False, interval=50 )
plt.show()
def evolution(self, n):
KERNEL = np.array([[1, 1, 1],[1, 0, 1],[1, 1, 1]])
c = convolve(self.state, KERNEL, mode='wrap')
new = np.zeros_like(self.state)
new[c == 3] = 1
new[c < 2] = 0
new[c > 3] = 0
new[(self.state == 1) & (c == 2)] = 1
temp_vector = np.reshape(new,(1,self.width*self.length))
f_handle = open('data.csv','a')
np.savetxt(f_handle,temp_vector,delimiter=',',newline='\n')
f_handle.close()
self.state = new
self.plot()
def plot(self):
self.im.set_data(self.state)
gol = Game_Of_Life(width=100,length=100)
gol.start()