-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_life.py
337 lines (318 loc) · 7.63 KB
/
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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import random
import os
import time
def random_state(height: int, width: int):
"""
Function takes in 2 args (height and width) and returns a board_state (list) in which every cell has been randomly
initalized to either alive (1) or dead (0). These boards are known as 'soups'
"""
random_board = []
for _ in range(height):
sub = []
for _ in range(width):
sub.append(random.randint(0,1))
random_board.append(sub)
return random_board
def dead_state(height: int, width: int):
#same as random_state only initializes to completely dead
dead_board = []
for _ in range(height):
sub = []
for _ in range(width):
sub.append(0)
dead_board.append(sub)
return dead_board
def next_board_state(board: list):
#determine width/height of board to set up nexted loop
next_board_height = len(board)
next_board_width = len(board[0])
#generate a dead_state board to build new state from
next_board = dead_state(next_board_height, next_board_width)
#build nested loop to analyze board
for i in range(next_board_height):
for j in range(next_board_width):
neighbors = walk_and_tally(board, next_board_height, next_board_width, i, j)
if(board[i][j] == 1): #alive
if(neighbors >= 2 and neighbors < 4):
next_board[i][j] = 1
else: #dead
if(neighbors == 3):
next_board[i][j] = 1
return next_board
def walk_and_tally(board: list, h: int, w: int, i: int, j: int):
#check for corner
neighbors = 0
tlcorner = (i == 0 and j == 0)
if(tlcorner):
#up/left (wrap)
if(board[h - 1][w - 1] == 1):
neighbors += 1
#up (wrap)
if(board[h - 1][j] == 1):
neighbors += 1
#up/right (wrap)
if(board[h - 1][j + 1] == 1):
neighbors += 1
#right
if(board[i][j + 1] == 1):
neighbors += 1
#right/down
if(board[i + 1][j + 1] == 1):
neighbors += 1
#down
if(board[i + 1][j] == 1):
neighbors += 1
#down/left (wrap)
if(board[i + 1][w - 1] == 1):
neighbors += 1
#left (wrap)
if(board[i][w - 1] == 1):
neighbors += 1
return neighbors
trcorner = (i == 0 and j == w - 1)
if(trcorner):
#up/left (wrap)
if(board[h - 1][j - 1] == 1):
neighbors += 1
#up (wrap)
if(board[h - 1][j] == 1):
neighbors += 1
#up/right (wrap)
if(board[h - 1][0] == 1):
neighbors += 1
#right (wrap)
if(board[i][0] == 1):
neighbors += 1
#down/right (wrap)
if(board[i + 1][0] == 1):
neighbors += 1
#down
if(board[i + 1][j] == 1):
neighbors += 1
#down/left
if(board[i + 1][j - 1] == 1):
neighbors += 1
#left
if(board[i][j - 1] == 1):
neighbors += 1
return neighbors
blcorner = (i == h - 1 and j == 0)
if(blcorner):
#up/left (wrap)
if(board[i - 1][w - 1] == 1):
neighbors += 1
#up
if(board[i - 1][j] == 1):
neighbors += 1
#up/right
if(board[i - 1][j + 1] == 1):
neighbors += 1
#right
if(board[i][j + 1] == 1):
neighbors += 1
#down/right (wrap)
if(board[0][j + 1] == 1):
neighbors += 1
#down (wrap)
if(board[0][j] == 1):
neighbors += 1
#down/left (wrap)
if(board[0][w - 1] == 1):
neighbors += 1
#left (wrap)
if(board[i][w - 1] == 1):
neighbors += 1
return neighbors
brcorner = (i == h - 1 and j == w - 1)
if(brcorner):
#up/left
if(board[i - 1][j - 1] == 1):
neighbors += 1
#up
if(board[i - 1][j] == 1):
neighbors += 1
#up/right (wrap)
if(board[i - 1][0] == 1):
neighbors += 1
#right (wrap)
if(board[i][0] == 1):
neighbors += 1
#down/right (wrap)
if(board[0][0] == 1):
neighbors += 1
#down (wrap)
if(board[0][j] == 1):
neighbors += 1
#down/left (wrap)
if(board[0][j - 1] == 1):
neighbors += 1
#left
if(board[i][j - 1] == 1):
neighbors += 1
return neighbors
#check for edge
tedge = (i == 0)
if(tedge):
#up/left (wrap)
if(board[h - 1][j - 1] == 1):
neighbors += 1
#up (wrap)
if(board[h - 1][j] == 1):
neighbors += 1
#up/right (wrap)
if(board[h - 1][j + 1] == 1):
neighbors += 1
#right
if(board[i][j + 1] == 1):
neighbors += 1
#down/right
if(board[i + 1][j + 1] == 1):
neighbors += 1
#down
if(board[i + 1][j] == 1):
neighbors += 1
#down/left
if(board[i + 1][j - 1] == 1):
neighbors += 1
#left
if(board[i][j - 1] == 1):
neighbors += 1
return neighbors
redge = (j == w - 1)
if(redge):
#up/left
if(board[i - 1][j - 1] == 1):
neighbors += 1
#up
if(board[i - 1][j] == 1):
neighbors += 1
#up/right (wrap)
if(board[i - 1][0] == 1):
neighbors += 1
#right (wrap)
if(board[i][0] == 1):
neighbors += 1
#down/right (wrap)
if(board[i + 1][0] == 1):
neighbors += 1
#down
if(board[i + 1][j] == 1):
neighbors += 1
#down/left
if(board[i + 1][j - 1] == 1):
neighbors += 1
#left
if(board[i][j -1] == 1):
neighbors += 1
return neighbors
bedge = (i == h - 1)
if(bedge):
#up/left
if(board[i - 1][j - 1] == 1):
neighbors += 1
#up
if(board[i - 1][j] == 1):
neighbors += 1
#up/right
if(board[i - 1][j + 1] == 1):
neighbors += 1
#right
if(board[i][j + 1] == 1):
neighbors += 1
#down/right (wrap)
if(board[0][j + 1] == 1):
neighbors += 1
#down (wrap)
if(board[0][j] == 1):
neighbors += 1
#down/left (wrap)
if(board[0][j - 1] == 1):
neighbors += 1
#left
if(board[i][j - 1] == 1):
neighbors += 1
return neighbors
ledge = (j == 0)
if(ledge):
#up/left (wrap)
if(board[i - 1][w - 1] == 1):
neighbors += 1
#up
if(board[i - 1][j] == 1):
neighbors += 1
#up/right
if(board[i - 1][j + 1] == 1):
neighbors += 1
#right
if(board[i][j + 1] == 1):
neighbors += 1
#down/right
if(board[i + 1][j + 1] == 1):
neighbors += 1
#down
if(board[i + 1][j] == 1):
neighbors += 1
#down/left (wrap)
if(board[i + 1][w - 1] == 1):
neighbors += 1
#left (wrap)
if(board[i][w - 1] == 1):
neighbors += 1
return neighbors
#handle central
#up/left
if(board[i - 1][j - 1] == 1):
neighbors += 1
#up
if(board[i - 1][j] == 1):
neighbors += 1
#up/right
if(board[i - 1][j + 1] == 1):
neighbors += 1
#right
if(board[i][j + 1] == 1):
neighbors += 1
#down/right
if(board[i + 1][j + 1] == 1):
neighbors += 1
#down
if(board[i + 1][j] == 1):
neighbors += 1
#down/left
if(board[i + 1][j - 1] == 1):
neighbors += 1
#left
if(board[i][j - 1] == 1):
neighbors += 1
return neighbors
def pretty_print(board: list):
def print_horiz_border(board_width: int):
for _ in range(board_width + 2):
print('-', end='')
print()
board_height = len(board)
board_width = len(board[0])
#print top of board
print_horiz_border(board_width)
#print interior rows of board
for i in range(board_height):
print('|', end='')
for j in range(board_width):
if board[i][j] == 1:
print('*', end='')
else:
print(' ', end='')
print('|')
#print bottom of board
print_horiz_border(board_width)
def main():
height = int(input("Enter board height: "))
width = int(input("Enter board width: "))
time_between_frames = input("Number of seconds between frames (float): ")
soup = random_state(int(height), int(width))
while(1):
pretty_print(soup)
soup = next_board_state(soup)
time.sleep(float(time_between_frames))
os.system('clear')
main()