-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgameoflife.py
56 lines (45 loc) · 1.46 KB
/
gameoflife.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
import scipy.signal
import numpy as np
import json
cfile = "config.json"
class Life:
def __init__(self):
with open(cfile) as f:
data = f.read()
self.patterns = json.loads(data)
self.cur_board = None
self.iters = 0
self.alive = 0
self.pattern_name = None
self.nextgrid=[]
def lstPatterns(self):
return self.patterns
def select(self, pattern_name):
if pattern_name.lower() not in self.patterns:
return False
else:
self.pattern_name = pattern_name
self.cur_board = np.array(self.patterns[pattern_name.lower()], dtype=np.uint8)
self.alive = 0
self.iters = 0
return True
def applyRule(self):
conv = np.ones((3,3), dtype=np.uint8)
if self.cur_board is None:
return False
new_board = scipy.signal.convolve2d(self.cur_board, conv, mode="same")
(m, n) = self.cur_board.shape
for i in range(m):
for j in range(n):
if new_board[i,j] == 3:
new_board[i, j] = 1
elif new_board[i, j] == 4:
new_board[i, j] = self.cur_board[i, j]
else:
new_board[i, j] = 0
self.cur_board = new_board
self.iters += 1
self.alive = sum(sum(self.cur_board))
return True
def get_board(self):
return self.cur_board