-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlife.py
More file actions
executable file
·504 lines (429 loc) · 15.8 KB
/
life.py
File metadata and controls
executable file
·504 lines (429 loc) · 15.8 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env python
#
# life - A simple implementation of Conway's Game of Life
# Refer: http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
#
# Copyright (C) 2015 Michael Davies <michael@the-davies.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
from select import select
import getopt
import glob
import os
import random
import sys
import time
# Number of default iterations before we start over
NUM_ITERATIONS = 250
class Life:
def __init__(self, max_row, max_col):
self.max_row = max_row
self.max_col = max_col
self.clear_board()
def __str__(self):
output = ''
for row in range(self.max_row):
for col in range(self.max_col):
if self.board[row][col]:
output += 'O'
else:
output += ' '
output += '\n'
return output
def clear_board(self, board=None):
if board:
board = [[0 for x in range(b.max_col)] for x in range(b.max_row)]
else:
self.board = [[0 for x in range(self.max_col)]
for x in range(self.max_row)]
def how_many_neighbours(self, row, col):
neighbours = 0
for row_delta in [-1, 0, +1]:
for col_delta in [-1, 0, +1]:
new_row = row + row_delta
new_col = col + col_delta
if (new_row in range(self.max_row) and
new_col in range(self.max_col)):
if self.board[new_row][new_col]:
neighbours += 1
# Exclude ourselves
if self.board[row][col]:
neighbours -= 1
return neighbours
def create(self, row, col):
self.board[row][col] = 1
def check_for_life(self, row, col):
neighbours = self.how_many_neighbours(row, col)
if self.board[row][col] == 1:
if neighbours in [2, 3]:
return True
else:
if neighbours == 3:
return True
return False
def tick(self):
new_creation = Life(self.max_row, self.max_col)
for row in range(self.max_row):
for col in range(self.max_col):
if self.check_for_life(row, col):
new_creation.create(row, col)
self.board = new_creation.board
class CellQueue:
def __init__(self):
self.q = []
self.idx = 0
def add_filename(self, filename):
self.q.append(filename)
def length(self):
return len(self.q)
def next(self):
elem = self.q[self.idx]
if self.idx+1 < len(self.q):
self.idx += 1
else:
self.idx = 0
return elem
def build_acorn(life, base_row=40, base_col=12):
life.create(base_row+1, base_col+2)
life.create(base_row+2, base_col+4)
life.create(base_row+3, base_col+1)
life.create(base_row+3, base_col+2)
life.create(base_row+3, base_col+5)
life.create(base_row+3, base_col+6)
life.create(base_row+3, base_col+7)
def build_glider(life, base_row=40, base_col=12):
life.create(base_row+1, base_col+2)
life.create(base_row+2, base_col+3)
life.create(base_row+3, base_col+1)
life.create(base_row+3, base_col+2)
life.create(base_row+3, base_col+3)
def build_lwss(life, base_row=40, base_col=12):
life.create(base_row+2, base_col+3)
life.create(base_row+2, base_col+6)
life.create(base_row+3, base_col+2)
life.create(base_row+4, base_col+2)
life.create(base_row+4, base_col+6)
life.create(base_row+5, base_col+2)
life.create(base_row+5, base_col+3)
life.create(base_row+5, base_col+4)
life.create(base_row+5, base_col+5)
def build_beetle(life, base_row=40, base_col=12):
life.create(base_row+2, base_col+23)
life.create(base_row+3, base_col+20)
life.create(base_row+3, base_col+21)
life.create(base_row+3, base_col+22)
life.create(base_row+3, base_col+23)
life.create(base_row+4, base_col+15)
life.create(base_row+4, base_col+18)
life.create(base_row+4, base_col+20)
life.create(base_row+4, base_col+21)
life.create(base_row+5, base_col+15)
life.create(base_row+6, base_col+2)
life.create(base_row+6, base_col+3)
life.create(base_row+6, base_col+4)
life.create(base_row+6, base_col+5)
life.create(base_row+6, base_col+2)
life.create(base_row+6, base_col+14)
life.create(base_row+6, base_col+18)
life.create(base_row+6, base_col+20)
life.create(base_row+6, base_col+21)
life.create(base_row+7, base_col+2)
life.create(base_row+7, base_col+6)
life.create(base_row+7, base_col+12)
life.create(base_row+7, base_col+13)
life.create(base_row+7, base_col+15)
life.create(base_row+7, base_col+16)
life.create(base_row+7, base_col+18)
life.create(base_row+7, base_col+20)
life.create(base_row+7, base_col+22)
life.create(base_row+7, base_col+23)
life.create(base_row+7, base_col+24)
life.create(base_row+7, base_col+25)
life.create(base_row+7, base_col+26)
life.create(base_row+8, base_col+2)
life.create(base_row+8, base_col+12)
life.create(base_row+8, base_col+13)
life.create(base_row+8, base_col+15)
life.create(base_row+8, base_col+17)
life.create(base_row+8, base_col+19)
life.create(base_row+8, base_col+22)
life.create(base_row+8, base_col+23)
life.create(base_row+8, base_col+24)
life.create(base_row+8, base_col+25)
life.create(base_row+8, base_col+26)
life.create(base_row+9, base_col+3)
life.create(base_row+9, base_col+6)
life.create(base_row+9, base_col+9)
life.create(base_row+9, base_col+10)
life.create(base_row+9, base_col+13)
life.create(base_row+9, base_col+17)
life.create(base_row+9, base_col+18)
life.create(base_row+9, base_col+19)
life.create(base_row+9, base_col+22)
life.create(base_row+9, base_col+24)
life.create(base_row+9, base_col+25)
life.create(base_row+10, base_col+8)
life.create(base_row+10, base_col+11)
life.create(base_row+10, base_col+13)
life.create(base_row+10, base_col+14)
life.create(base_row+11, base_col+8)
life.create(base_row+11, base_col+13)
life.create(base_row+11, base_col+14)
life.create(base_row+12, base_col+8)
life.create(base_row+12, base_col+11)
life.create(base_row+12, base_col+13)
life.create(base_row+12, base_col+14)
life.create(base_row+13, base_col+3)
life.create(base_row+13, base_col+6)
life.create(base_row+13, base_col+9)
life.create(base_row+13, base_col+10)
life.create(base_row+13, base_col+13)
life.create(base_row+13, base_col+17)
life.create(base_row+13, base_col+18)
life.create(base_row+13, base_col+19)
life.create(base_row+13, base_col+22)
life.create(base_row+13, base_col+24)
life.create(base_row+13, base_col+25)
life.create(base_row+14, base_col+2)
life.create(base_row+14, base_col+12)
life.create(base_row+14, base_col+13)
life.create(base_row+14, base_col+15)
life.create(base_row+14, base_col+17)
life.create(base_row+14, base_col+19)
life.create(base_row+14, base_col+22)
life.create(base_row+14, base_col+23)
life.create(base_row+14, base_col+24)
life.create(base_row+14, base_col+25)
life.create(base_row+14, base_col+26)
life.create(base_row+15, base_col+2)
life.create(base_row+15, base_col+6)
life.create(base_row+15, base_col+12)
life.create(base_row+15, base_col+13)
life.create(base_row+15, base_col+15)
life.create(base_row+15, base_col+16)
life.create(base_row+15, base_col+18)
life.create(base_row+15, base_col+20)
life.create(base_row+15, base_col+22)
life.create(base_row+15, base_col+23)
life.create(base_row+15, base_col+24)
life.create(base_row+15, base_col+25)
life.create(base_row+15, base_col+26)
life.create(base_row+16, base_col+2)
life.create(base_row+16, base_col+3)
life.create(base_row+16, base_col+4)
life.create(base_row+16, base_col+5)
life.create(base_row+16, base_col+2)
life.create(base_row+16, base_col+14)
life.create(base_row+16, base_col+18)
life.create(base_row+16, base_col+20)
life.create(base_row+16, base_col+21)
life.create(base_row+17, base_col+15)
life.create(base_row+18, base_col+15)
life.create(base_row+18, base_col+18)
life.create(base_row+18, base_col+20)
life.create(base_row+18, base_col+21)
life.create(base_row+19, base_col+20)
life.create(base_row+19, base_col+21)
life.create(base_row+19, base_col+22)
life.create(base_row+19, base_col+23)
life.create(base_row+20, base_col+23)
def build_gosper_glider_gun(life, base_row=40, base_col=12):
life.create(base_row+0, base_col+24)
life.create(base_row+1, base_col+22)
life.create(base_row+1, base_col+24)
life.create(base_row+2, base_col+12)
life.create(base_row+2, base_col+13)
life.create(base_row+2, base_col+20)
life.create(base_row+2, base_col+21)
life.create(base_row+2, base_col+34)
life.create(base_row+2, base_col+35)
life.create(base_row+3, base_col+11)
life.create(base_row+3, base_col+15)
life.create(base_row+3, base_col+20)
life.create(base_row+3, base_col+21)
life.create(base_row+3, base_col+34)
life.create(base_row+3, base_col+35)
life.create(base_row+4, base_col+0)
life.create(base_row+4, base_col+1)
life.create(base_row+4, base_col+10)
life.create(base_row+4, base_col+16)
life.create(base_row+4, base_col+20)
life.create(base_row+4, base_col+21)
life.create(base_row+5, base_col+0)
life.create(base_row+5, base_col+1)
life.create(base_row+5, base_col+10)
life.create(base_row+5, base_col+14)
life.create(base_row+5, base_col+16)
life.create(base_row+5, base_col+17)
life.create(base_row+5, base_col+22)
life.create(base_row+5, base_col+24)
life.create(base_row+6, base_col+10)
life.create(base_row+6, base_col+16)
life.create(base_row+6, base_col+24)
life.create(base_row+7, base_col+11)
life.create(base_row+7, base_col+15)
life.create(base_row+8, base_col+12)
life.create(base_row+8, base_col+13)
def build_john_conway(life, base_row=40, base_col=12):
# https://xkcd.com/2293/
life.create(base_row+1, base_col+10)
life.create(base_row+1, base_col+11)
life.create(base_row+1, base_col+12)
life.create(base_row+2, base_col+10)
life.create(base_row+2, base_col+12)
life.create(base_row+3, base_col+10)
life.create(base_row+3, base_col+12)
life.create(base_row+4, base_col+11)
life.create(base_row+5, base_col+8)
life.create(base_row+5, base_col+10)
life.create(base_row+5, base_col+11)
life.create(base_row+5, base_col+12)
life.create(base_row+6, base_col+9)
life.create(base_row+6, base_col+11)
life.create(base_row+6, base_col+13)
life.create(base_row+7, base_col+11)
life.create(base_row+7, base_col+14)
life.create(base_row+8, base_col+10)
life.create(base_row+8, base_col+12)
life.create(base_row+8, base_col+10)
life.create(base_row+8, base_col+12)
def build_randomised(life, base_row=0, base_col=0):
# 1/n percentage of a square having life
n = 10
for row in range(life.max_row):
for col in range(life.max_col):
if random.randrange(0, n) == 1:
life.create(row, col)
def process_file(life, filename, base_row=40, base_col=12):
# Note(mrda): Blank lines are important
with open(filename) as f:
row = 0
for line in f:
length = len(line)
# Skip comment lines
if line[0] == '!':
continue
for col, ch in enumerate(line):
if ch == 'O':
life.create(base_row+row, base_col+col)
row += 1
def add_directory(life, queue, directory):
for fn in glob.glob(os.path.join(directory, "*.cells")):
queue.add_filename(fn)
def pick_builtin_sim(life, x, y):
# Randomly generate the initial life layout
dispatch = [
build_acorn,
build_glider,
build_lwss,
build_beetle,
build_gosper_glider_gun,
build_randomised,
build_john_conway,
]
dispatch[random.randrange(0, len(dispatch))](life, x, y)
def time_to_exit(timeout=None):
# Note(mrda): select() on stdin might not work on non-Unixes
exit = False
if not timeout:
timeout = 0.4 # seconds
rlist, wlist, xlist = select([sys.stdin], {}, {}, timeout)
if rlist:
s = sys.stdin.readline().rstrip()
if s in ['q', 'Q']:
exit = True
return exit
def print_exit_info(row, cols, timeout=None):
# Clear the screen in a platform independent way
os.system(['clear', 'cls'][os.name == 'nt'])
if not timeout:
timeout = 3 # seconds
message = "Life: Press 'q' and <RETURN> to exit"
center_spacing = (cols - len(message)) // 2
vertical_spacing = int(1.0/3 * rows)
print('\n' * vertical_spacing)
print(' ' * center_spacing + message)
time.sleep(timeout)
def reset_board(life, x, y, queue=None):
if queue is None or queue.length() == 0:
pick_builtin_sim(life, (rows//2-10), (cols//2-10))
else:
process_file(life, queue.next())
def usage():
myname = os.path.basename(sys.argv[0])
print("Usage: {} [-h|--help] [-i|--iterations <iterations>] "
"[-s|--speed <seconds>] "
"[files or directories]".format(myname))
print("\nYou can find definition files at https://www.conwaylife.com/")
if __name__ == '__main__':
# Get the terminal size
rows, cols = os.popen('stty size', 'r').read().split()
rows = int(rows)
cols = int(cols)
iterations = NUM_ITERATIONS
speed = 0.5 # seconds
# Command line processing
try:
opts, args = getopt.getopt(sys.argv[1:], 'hi:s:',
['help', 'iterations=', 'speed='])
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
for o, a in opts:
if o in ('-h', '--help'):
usage()
sys.exit()
elif o in ('-s', '--speed'):
try:
speed = float(a)
except Exception as e:
print(e)
usage()
sys.exit(3)
elif o in ('-i', '--iterations'):
try:
iterations = int(a)
except Exception as e:
print(e)
usage()
sys.exit(4)
# Create the petri dish
life = Life(rows, cols)
my_queue = CellQueue()
for ford in args:
if os.path.isfile(ford):
my_queue.add_filename(ford)
elif os.path.isdir(ford):
add_directory(life, my_queue, ford)
# TODO(mrda): We should check to see that the petri dish is large
# enough for the simulation that we're prepopulating.
init_x = rows//2-10
init_y = cols//2-10
# Design shouts Designer
reset_board(life, init_x, init_y, my_queue)
exit = False
while(not exit):
print_exit_info(rows, cols)
n = 0
while(n < iterations and not exit):
print(life)
life.tick()
n += 1
exit = time_to_exit(speed)
life.clear_board()
reset_board(life, init_x, init_y, my_queue)