-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.py
72 lines (60 loc) · 2.62 KB
/
life.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
67
68
69
70
71
72
import random
from pprint import pprint, pformat
class Life:
def __init__(self, sizex, sizey, init_state=None):
self.sizex = sizex
self.sizey = sizey
self._live_neighbors = None
if not init_state:
self._life_board = self._init_empty()
else:
self._life_board = init_state
self.compute_live_neighbors()
def __repr__(self):
return '\n'.join([
pformat(self._life_board),
# pformat(self._live_neighbors)
])
def size(self):
return self.sizex, self.sizey
def get(self, x, y):
return self._life_board[x][y]
def set(self, x, y, value):
self._life_board[x][y] = value
def live_neighbors(self, x, y):
return self._live_neighbors[x][y]
def _init_empty(self):
return [[random.choice([0, 1]) for i in range(self.sizex)] for j in range(self.sizey)]
def compute_live_neighbors(self):
self._live_neighbors = self._init_empty()
for i in range(self.sizex):
for j in range(self.sizey):
neighbors = [
self._life_board[i - 1][j - 1], self._life_board[i - 1][j], self._life_board[i - 1][(j + 1) % self.sizey],
self._life_board[i][j - 1], self._life_board[i][(j + 1) % self.sizey],
self._life_board[(i + 1) % self.sizex][j - 1], self._life_board[(i + 1) % self.sizex][j], self._life_board[(i + 1) % self.sizex][(j + 1) % self.sizey],
]
self._live_neighbors[i][j] = neighbors.count(True)
def run_life(self):
"""
Any live cell with two or three live neighbours survives.
Any dead cell with three live neighbours becomes a live cell.
All other live cells die in the next generation. Similarly, all other dead cells stay dead.
"""
output = Life(self.sizex, self.sizey, self._life_board)
for i in range(self.sizex):
for j in range(self.sizey):
curr_cell = output.get(i, j)
neighbors_count = output.live_neighbors(i, j)
if curr_cell and neighbors_count in [2, 3]:
output.set(i, j, 1)
print(f"Cell {i},{j} ({curr_cell}) with {neighbors_count} live neighbors survives")
elif neighbors_count == 3:
output.set(i, j, 1)
print(f"Dead cell {i},{j} ({curr_cell}) with {neighbors_count} live neighbors revived")
else:
output.set(i, j, 0)
return output
if __name__ == '__main__':
life = Life(5, 5)
pprint(life._life_board)