forked from mrda/junkcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlife.py
More file actions
executable file
·316 lines (284 loc) · 10.6 KB
/
life.py
File metadata and controls
executable file
·316 lines (284 loc) · 10.6 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
#!/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 os
import random
import sys
import time
# Number of iterations before we start over
NUM_ITERATIONS = 150
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
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_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 initialise_life(life, x, y):
# Randomly generate the initial life layout
dispatch = [
build_acorn,
build_glider,
build_lwss,
build_beetle,
]
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.2 # 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 = 5 # 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)
if __name__ == '__main__':
# Get the terminal size
rows, cols = os.popen('stty size', 'r').read().split()
rows = int(rows)
cols = int(cols)
# Create the petri dish
life = Life(rows, cols)
# Give an approximate starting location
initialise_life(life, (rows/2-10), (cols/2-10))
exit = False
while(not exit):
print_exit_info(rows, cols)
n = 0
while(n < NUM_ITERATIONS and not exit):
print life
life.tick()
n += 1
exit = time_to_exit()
# Design shouts Designer
build_randomised(life)