forked from Red-0111/Anything-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconways_game_of_life.py
56 lines (43 loc) · 1.2 KB
/
conways_game_of_life.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
from random import choice
from turtle import *
from freegames import square
cells = {}
def initialize():
"""Randomly initialize the cells."""
for x in range(-200, 200, 10):
for y in range(-200, 200, 10):
cells[x, y] = False
for x in range(-50, 50, 10):
for y in range(-50, 50, 10):
cells[x, y] = choice([True, False])
def step():
"""Compute one step in the Game of Life."""
neighbors = {}
for x in range(-190, 190, 10):
for y in range(-190, 190, 10):
count = -cells[x, y]
for h in [-10, 0, 10]:
for v in [-10, 0, 10]:
count += cells[x + h, y + v]
neighbors[x, y] = count
for cell, count in neighbors.items():
if cells[cell]:
if count < 2 or count > 3:
cells[cell] = False
elif count == 3:
cells[cell] = True
def draw():
"""Draw all the squares."""
step()
clear()
for (x, y), alive in cells.items():
color = 'green' if alive else 'black'
square(x, y, 10, color)
update()
ontimer(draw, 100)
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
initialize()
draw()
done()