/
life.py
228 lines (169 loc) · 5.38 KB
/
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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import csplot
import time
import random
from copy import deepcopy
from copy import copy
def createBoard(width, height=None):
#creates a 2d array
if height is None: height=width
return [ [0]*width for R in range(height) ]
def update1a(B):
#adds a diagonal strip of "on" cells from SW corner
width = len(B[0])
height = len(B)
Bnew = deepcopy(B)
for row in range(height):
for col in range(width):
if row == col:
Bnew[row][col] = 1
return Bnew
def update1b(B):
#adds a diagonal strip of "on" cells from NW corner
width = len(B[0])
height = len(B)
Bnew = deepcopy(B)
for row in range(height):
for col in range(width):
if (height-1 - row) == col:
Bnew[row][col] = 1
return Bnew
def update1c(B):
#adds a diagonal strip of "on" cells from SE corner
width = len(B[0])
height = len(B)
Bnew = deepcopy(B)
for row in range(height):
for col in range(width):
if row == (width-1 - col):
Bnew[row][col] = 1
return Bnew
def update1d(B):
#adds a diagonal strip of "on" cells from NE corner
width = len(B[0])
height = len(B)
Bnew = deepcopy(B)
for row in range(height):
for col in range(width):
if (height - row) == (width - col):
Bnew[row][col] = 1
return Bnew
def diagnals(B):
# applies diagnal lines form all corners
Bnew = deepcopy(B)
Bnew = update1a(Bnew)
Bnew = update1b(Bnew)
Bnew = update1c(Bnew)
return update1d(Bnew)
def randomBoard(B):
# returns a board generated randomly of the same size as input
width = len(B[0])
height = len(B)
Bnew = deepcopy(B)
for row in range(height):
for col in range(width):
Bnew[row][col] = random.choice([0,1])
return Bnew
def randomBoard2(B):
# returns a board generated randomly of the same size as input
return [[random.choice([0,1])for col in row] for row in B]
def randomBoard3(B):
# returns a board generated randomly of the same size as input
width = len(B[0])
height = len(B)
return [[random.choice([0,1])for x in ([0]*width)] for z in [0]*height]
def fill(B):
# returns a board that is all 1s, same dimentions as B
width = len(B[0])
height = len(B)
return [[1]*width for R in [1]*height ]
def blank(B):
# returns a board that is all 0s, same dimentions as B
width = len(B[0])
height = len(B)
return createBoard(width, height)
def border(B, wide=1, bordervalue=0):
''' returns a board that has a border added on top of board B to it
of width: wide and of value: bordervalue'''
width = len(B[0])
height = len(B)
exception = False
if width < wide: exception = True
if height < wide: exception = True
if exception:
if bordervalue:
return fill(B)
else:
return blank(B)
Bnew = deepcopy(B)
for row in range(wide) + range(height - wide, height):
for col in range(width):
Bnew[row][col] = bordervalue
for col in range(wide) + range(width - wide, width):
for row in range(height):
Bnew[row][col] = bordervalue
return Bnew
def invert(B):
# returns a board that is the inverse of the input
width = len(B[0])
height = len(B)
Bnew = deepcopy(B)
for row in range(height):
for col in range(width):
if B[row][col]:
Bnew[row][col] = 0
else :
Bnew[row][col] = 1
return Bnew
def update2(B):
return border(fill(B))
def updateR(B):
return border(RandomBoard(B))
#print createBoard(3,3)
def life(width, height=None):
# will become John Conway's Game of Life...
if height is None: height = width
B = randomBoard(createBoard(width, height))
while True: # run forever
csplot.show(B) # show current B
time.sleep(0.25) # pause a bit
B = randomBoard(B) # sets the new board correctly
def neighbors(B , row , col , wide = 1):
'''returns the number of neighbors that are within a radius of wide
of cell B[row][col]'''
width = len(B[0])
height = len(B)
# error catching
rowL = max(0, row-wide)
rowU = min(row+wide+1, height)
colL = max(0, col-wide)
colU = min(col+wide+1, width)
sum = -B[row][col]
for r in range(rowL, rowU):
for c in range (colL, colU):
sum += B[r][c]
return sum
def updateNextLife(B):
width = len(B[0])
height = len(B)
newB = blank(B)
for row in range(height):
for col in range(width):
n = neighbors(B , row , col)
#if ( n > 3 ) or ( n < 2 ):
#newB[row][col] = 0
if n == 3 :
newB[row][col] = 1
elif n == 2:
newB[row][col] = copy(B[row][col])
else :
newB[row][col] = 0
return newB
def life2(B):
""" will become John Conway's Game of Life... """
csplot.showAndClickInIdle(B)
# hold s and click on the grid to bring a cell to life
# then close the grid and life will play
while True:
csplot.show(B) # show B
time.sleep(0.25) # pause a bit
B = updateNextLife(oldB) # sets the new board correctly