-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
35 lines (26 loc) · 1001 Bytes
/
game.py
File metadata and controls
35 lines (26 loc) · 1001 Bytes
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
from copy import deepcopy
DELTA = (-1, 0, 1)
class GameOfLife:
def __init__(self, cells):
self.cells = cells
def evolve(self):
new_iteration = deepcopy(self.cells)
for i, row in enumerate(self.cells):
for j, _ in enumerate(row):
r, c = self.get_row(i), self.get_col(j)
live_neighbours = self.sum_of_block(i, j)
if live_neighbours == 3:
new_iteration[r][c] = True
elif live_neighbours != 4:
new_iteration[r][c] = False
self.cells = new_iteration
def sum_of_block(self, row, col):
return sum(self.get_cell(row + i, col + j)
for i in DELTA
for j in DELTA)
def get_cell(self, row, col):
return self.cells[self.get_row(row)][(self.get_col(col))]
def get_row(self, row):
return row % len(self.cells)
def get_col(self, col):
return col % len(self.cells[0])