-
Notifications
You must be signed in to change notification settings - Fork 0
/
life_game.py
executable file
·71 lines (66 loc) · 1.57 KB
/
life_game.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
68
69
70
71
def alive(table, place):
living_nbrs = 0
N = len(table)
NBRS = 3
X, Y = place[0], place[1]
END_VAL = [2,3] if table[X][Y] else [3]
for x in range(NBRS):
for y in range(NBRS):
if (x,y) == place: continue
i = (X + x - NBRS + 2) % N
j = (Y + y - NBRS + 2) % N
if table[i][j]: living_nbrs += 1
for end_val in END_VAL:
if living_nbrs == end_val: return True
return False
def is_diff(table1,table2):
N = len(table1)
for x in range(N):
for y in range(N):
if table1[x][y] != table2[x][y]:
return True
return False
def Life(table):
while True:
new_table = [row[:] for row in table]
N = len(table)
for x in range(N):
for y in range(N):
new_table[x][y] = alive(table, (x,y))
yield new_table
table = new_table
def LifeC(table, prev_table = None):
should_stop = False
N = len(table)
life = Life(table)
while not should_stop:
prev_table = [row[:] for row in table]
table = next(life)
if is_diff(table, prev_table):
yield table
else:
should_stop = True
def LifeD(table):
should_stop = False
first = [row[:] for row in table]
N = len(table)
life = Life(table)
while not should_stop:
table = next(life)
if is_diff(first, table):
yield table
else:
should_stop = True
def UltimateLife(table):
life = Life(table)
lives = []
should_stop = False
while not should_stop:
yield table
new_table = [row[:] for row in table]
lives.append(new_table)
table = next(life)
for a_life in lives:
if not is_diff(a_life, table):
should_stop = True
break