-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameArray.py
60 lines (49 loc) · 1.8 KB
/
GameArray.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
import numpy as np
import random
from numpy.lib.histograms import _histogram_bin_edges_dispatcher
class LifeArray:
""" This class represent the matrix array of the game of life and can be initialized at random or with a numpy array."""
def ini_at_random(self):
for p in np.ndenumerate(self.LifeArr):
random.seed(p)
rand = random.random()
if rand> .9:
self.LifeArr[p[0]] = 1
def __init__(self, height, width, initiate=None):
self.LifeArr = np.zeros((height,width))
self.width = width
self.height = height
if initiate:
self.ini_at_random()
def update(self):
temp = np.zeros(self.LifeArr.shape)
for p in np.ndenumerate(self.LifeArr):
i,j = p[0]
neighbors = self.LifeArr[max(0, i-1) : min(i+2,self.height),
max(0, j-1) : min(j+2,self.width)]
suma = np.sum(neighbors)
if self.LifeArr[p[0]]== 0 and suma == 3: # Creation
temp[p[0]] = 1
if self.LifeArr[p[0]]== 1 and suma in range(3,5): # Next generation
temp[p[0]] = 1
if self.LifeArr[p[0]]== 1 and suma > 4: # Overpopulation
temp[p[0]] = 0
if self.LifeArr[p[0]]== 1 and suma <= 2: # Underpopulation
temp[p[0]] = 0
self.LifeArr = temp
def print(self):
print(self.LifeArr)
print("")
def get(self,x,y):
return self.LifeArr[x,y]
def change(self,x,y):
self.LifeArr[x,y] = 1 - self.LifeArr[x,y]
return self.LifeArr[x,y]
def born(self, x, y):
self.LifeArr[x,y] = 1
if __name__ == '__main__':
arry = LifeArray(10,9,True)
arry.print()
arry.update()
arry.print()
arry.update()