-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.py
67 lines (53 loc) · 1.76 KB
/
simulation.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
from grid import Grid
class Simulation:
def __init__(self, width, height, cell_size):
self.grid = Grid(width, height, cell_size)
self.temp_grid = Grid(width, height, cell_size)
self.rows = height // cell_size
self.columns = width // cell_size
self.run = False
def draw(self, window):
self.grid.draw(window)
def count_live_neighbors(self, grid, row, column):
live_neighbors = 0
neighbor_offsets = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
for offset in neighbor_offsets:
new_row = (row + offset[0]) % self.rows
new_column = (column + offset[1]) % self.columns
if self.grid.cells[new_row][new_column] == 1:
live_neighbors += 1
return live_neighbors
def update(self):
if self.is_running():
for row in range(self.rows):
for column in range(self.columns):
live_neighbors = self.count_live_neighbors(self.grid, row, column)
cell_value = self.grid.cells[row][column]
if cell_value == 1:
if live_neighbors > 3 or live_neighbors < 2:
self.temp_grid.cells[row][column] = 0
else:
self.temp_grid.cells[row][column] = 1
else:
if live_neighbors == 3:
self.temp_grid.cells[row][column] = 1
else:
self.temp_grid.cells[row][column] = 0
for row in range(self.rows):
for column in range(self.columns):
self.grid.cells[row][column] = self.temp_grid.cells[row][column]
def is_running(self):
return self.run
def start(self):
self.run = True
def stop(self):
self.run = False
def clear(self):
if self.is_running() == False:
self.grid.clear()
def create_random_state(self):
if self.is_running() == False:
self.grid.fill_random()
def toggle_cell(self, row, column):
if self.is_running() == False:
self.grid.toggle_cell(row, column)