forked from AdaGold/DebuggingContinued
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
39 lines (32 loc) · 1.29 KB
/
main.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
from gameoflife import width, height, stage, new_stage, print_stage, count_neighbors
def init_stage(stage):
for v_pos in range(0, height):
for h_pos in range(0, width):
if h_pos == 1:
stage[v_pos][h_pos] = True
else:
stage[v_pos][h_pos] = False
# Generation Rules:
# 1. Any live cell with < 2 neighbors dies
# 2. Any live cell with 2-3 neighbors lives
# 3. Any live cell with > 3 neighbors dies
# 4. Any dead cell with 3 neighbors comes alive
def one_generation(stage):
for v_pos in range(len(stage)):
for h_pos in range(len(stage[0])):
neighbors = count_neighbors(stage, v_pos, h_pos)
if not stage[v_pos][h_pos] and neighbors == 3:
new_stage[v_pos][h_pos] = True
elif stage[v_pos][h_pos] and neighbors < 2:
new_stage[v_pos][h_pos] = False
elif stage[v_pos][h_pos] and (neighbors == 2 or neighbors == 3):
new_stage[v_pos][h_pos] = True
elif stage[v_pos][h_pos] and neighbors > 3:
new_stage[v_pos][h_pos] = False
return new_stage
init_stage(stage)
print("First Generation:")
print_stage(stage)
the_next_stage = one_generation(stage)
print("Second Generation:")
print_stage(the_next_stage)