-
Notifications
You must be signed in to change notification settings - Fork 0
/
CGLmyversion.py
93 lines (81 loc) · 2.11 KB
/
CGLmyversion.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Version of Conway's Game of life
# Author: Matthew Pfannenstiel matt.c.pfannenstiel@gmail.com, Jan 2016
import golly as g
def neighborcount(x, y):
c = 0
j = x -1
while j <= x + 1:
k = y - 1
while k <= y +1:
if k == y and j == x:
k +=1
break
if g.getcell(j, k) == 1:
c +=1
k +=1
j +=1
# if g.getcell(x - 1, y) == 1:
# c += 1
# if g.getcell(x - 1, y - 1) == 1:
# c += 1
# if g.getcell(x, y - 1) == 1:
# c += 1
# if g.getcell(x - 1, y + 1) == 1:
# c += 1
# if g.getcell(x, y + 1) == 1:
# c += 1
# if g.getcell(x + 1, y + 1) == 1:
# c += 1
# if g.getcell(x + 1, y - 1) == 1:
# c += 1
# if g.getcell(x + 1, y) == 1:
# c += 1
return c
def rules(x, y, u):
i = neighborcount(x, y)
if i == 3 and g.getcell(x, y) == 0:
u[x][y] = 1
if i > 3 and g.getcell(x, y) == 1:
u[x][y] = 0
if i < 2 and g.getcell(x, y) == 1:
u[x][y] = 0
if i == (2 | 3) and g.getcell(x, y) == 1:
u[x][y] = g.getcell(x, y)
return u
def main(width, height, update):
w = width
h = height
x = 0
i = 0
while x < w:
y = 0
while y < h:
update = rules(x, y, update)
y += 1
x += 1
while i < w:
j = 0
while j < h:
g.setcell(i, j, update[i][j])
j += 1
i += 1
g.update()
return update
try:
g.new("Conway's Game of life")
g.setrule("Life")
g.setcolors([1, 255, 255, 255])
g.setcolors([0, 0, 0, 0])
maxsize = 100000
count = 0
width = int( g.getstring("Enter a width for the game of life:", "100") )
height = int( g.getstring("Enter a height for the game of life:", "100") )
g.select([0, 0, width, height])
g.randfill(50)
update = [[0 for x in range(width + 1)] for x in range(height + 1)]
while count < maxsize:
g.show("In main " + str(count))
update = main(width, height, update)
count += 1
finally:
g.note("Goodbye")