This repository was archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.py
65 lines (51 loc) · 1.73 KB
/
rules.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
from random import randint
def generate_grid(fill=50):
resolution = 50
grid = []
for y in range(resolution):
temp = []
for x in range(resolution):
if x in [0, resolution-1] or y in [0, resolution-1]:
state = False
else:
if randint(0, 100) <= fill:
state = True
else:
state = False
temp.append(state)
grid.append(temp)
return grid
def ConwaysGameOfLifeRule(current, num_neighbours):
"""
Any live cell with fewer than two live neighbours dies
Any live cell with two or three live neighbours lives
Any live cell with more than three live neighbours dies
Any dead cell with exactly three live neighbours becomes a live cell
"""
if current == True:
if num_neighbours < 2:
return False
if num_neighbours == 2 or num_neighbours == 3:
return True
if num_neighbours > 3:
return False
else:
if num_neighbours == 3:
return True
def run_single_step(grid, rule=ConwaysGameOfLifeRule):
temp = generate_grid(0)
for y in range(1, len(grid) - 1):
for x in range(1, len(grid[0]) - 1):
state = grid[y][x]
n = [
bool(grid[y-1][x ]),
bool(grid[y+1][x ]),
bool(grid[y ][x-1]),
bool(grid[y ][x+1]),
bool(grid[y-1][x-1]),
bool(grid[y+1][x+1]),
bool(grid[y-1][x+1]),
bool(grid[y+1][x-1])
]
temp[y][x] = rule(state, n.count(True))
return temp