-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalgorithms.py
More file actions
39 lines (31 loc) · 1.13 KB
/
algorithms.py
File metadata and controls
39 lines (31 loc) · 1.13 KB
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
#!/usr/bin/python
def calc_status(grid, interface):
# Declare variables
totalAliveNeighbors = 0
gridLength = len(grid)
gridHeight = len(grid[0])
# Create temporary array based on grid size
tempGrid = [[False for x in range(gridHeight)] for x in range(gridLength)]
# Iterate through grid
for x in range(gridLength):
for y in range(gridHeight):
totalAliveNeighbors = 0
# Add neighbors of each cell
for z in range (len(grid[x][y].neighbors)):
if grid[x][y].neighbors[z].alive == True:
totalAliveNeighbors += 1
if totalAliveNeighbors > interface.populationLimit:
break
# Decide if cell is alive or dead
if totalAliveNeighbors < interface.populationMin:
tempGrid[x][y] = False
elif totalAliveNeighbors >= interface.populationMin and totalAliveNeighbors < interface.populationLimit:
tempGrid[x][y] = grid[x][y].alive
elif totalAliveNeighbors == interface.populationLimit:
tempGrid[x][y] = True
else:
tempGrid[x][y] = False
# Iterate through tempgrid and update grid
for x in range(gridLength):
for y in range(gridHeight):
grid[x][y].alive = tempGrid[x][y]