-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
64 lines (50 loc) · 1.8 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
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
import random
from Graphics import *
from time import sleep
from Patterns import *
def evolution(_world):
neg = []
_row = len(_world)
_column = len(_world[0])
for i in range(_row):
_ = []
for j in range(_column):
lives = 0
for id in range(i - 1, i + 2):
for jd in range(j - 1, j + 2):
if _world[0 if id == _row else id][0 if jd == _column else jd] == 1:
lives += 1
if _world[i][j] == 1:
lives -= 1
_.append(lives)
neg.append(_)
for i in range(_row):
for j in range(_column):
# Any live cell with fewer than two live neighbours, dies
if _world[i][j] == 1 and neg[i][j] < 2:
_world[i][j] = 0
# Any live cell with more than three live neighbours, dies
if _world[i][j] == 1 and neg[i][j] > 3:
_world[i][j] = 0
# Any live cell with two or three live neighbours lives, unchanged, to the next generation.
if _world[i][j] == 1 and (neg[i][j] == 2 or neg[i][j] == 3):
_world[i][j] = 1
# Any dead cell with exactly three live neighbours will come to life.
if _world[i][j] == 0 and neg[i][j] == 3:
_world[i][j] = 1
return _world
def run(init, draw_every = 1, delay = 0.1, gen = 500):
ui.draw(init, 1)
for ev in range(1, gen):
init = evolution(init)
print("\rEvolution:\t", ev, sep = "", end = "")
if ev % draw_every == 0:
sleep(delay)
ui.draw(init, ev)
ui.wait()
world = flower # random_world(60, 60, prob = 0.5)
ui = Graphics(len(world), len(world[0]), box_width=12)
run(init = world,
draw_every = 1,
delay = 0.3,
gen = 100)