-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathgame_of_life.py
237 lines (193 loc) · 6.18 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
"""Conway's Game Of Life, Author Anurag Kumar(mailto:[email protected])
Requirements:
- numpy
- random
- time
- matplotlib
Python:
- 3.5
Usage:
- $python3 game_of_life <canvas_size:int>
Game-Of-Life Rules:
1.
Any live cell with fewer than two live neighbours
dies, as if caused by under-population.
2.
Any live cell with two or three live neighbours lives
on to the next generation.
3.
Any live cell with more than three live neighbours
dies, as if by over-population.
4.
Any dead cell with exactly three live neighbours be-
comes a live cell, as if by reproduction.
"""
import doctest
import random
import sys
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
usage_doc = "Usage of script: script_name <size_of_canvas:int>"
choice = [0] * 100 + [1] * 10
random.shuffle(choice)
def create_canvas(size: int) -> list[list[bool]]:
"""
For creating a nested list of boolean values,
based on the size parameter provided
Args:
size: integer
Returns:
A nested list of boolean values
Examples:
>>> create_canvas(1)
[[False]]
>>> create_canvas(2)
[[False, False], [False, False]]
>>> create_canvas(3)
[[False, False, False], [False, False, False], [False, False, False]]
>>> create_canvas(0)
[]
>>> create_canvas(-1)
[]
"""
canvas = [[False for i in range(size)] for j in range(size)]
return canvas
def seed(canvas: list[list[bool]]) -> None:
for i, row in enumerate(canvas):
for j, _ in enumerate(row):
canvas[i][j] = bool(random.getrandbits(1))
def run(canvas: list[list[bool]]) -> list[list[bool]]:
"""
This function runs the rules of game through all points, and changes their
status accordingly.(in the same canvas)
Args:
canvas : canvas of population to run the rules on.
Returns:
canvas of population after one step
Example #1:
>>> canvas=[[False, False, False], [False, False, False], [False, False, False]]
>>> run(canvas)
[[False, False, False], [False, False, False], [False, False, False]]
Example #2:
>>> canvas=[[True, False, False], [True, False, False], [False, False, False]]
>>> run(canvas)
[[False, False, False], [False, False, False], [False, False, False]]
Example #3:
>>> canvas=[[True, True, True], [True, False, False], [False, False, False]]
>>> run(canvas)
[[False, False, False], [False, False, False], [False, False, False]]
Example #4:
>>> canvas=[[True, False, False], [False, False, True], [False, True, False]]
>>> run(canvas)
[[False, False, False], [False, True, False], [False, False, False]]
Example #5:
>>> canvas=[[True, True, True], [True, True, True], [True, True, True]]
>>> run(canvas)
[[False, False, False], [False, False, False], [False, False, True]]
"""
current_canvas = np.array(canvas)
next_gen_canvas = np.array(create_canvas(current_canvas.shape[0]))
for r, row in enumerate(current_canvas):
for c, pt in enumerate(row):
next_gen_canvas[r][c] = __judge_point(
pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2]
)
return next_gen_canvas.tolist()
def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
"""
Update canvas provided
Args:
pt: boolean
neighbours: canvas
Returns:
Updated canvas
Example #1:
Tests pt = True, and alive < 2; expected 'alive' count = 0
>>> pt=True
>>> canvas=[[False, False, False], [False, False, False], [False, False, False]]
>>> __judge_point(pt, canvas)
False
Example #2:
Tests pt = True, and alive < 2; expected 'alive' count = 1
>>> pt=True
>>> canvas=[[True, False, False], [True, False, False], [False, False, False]]
>>> __judge_point(pt, canvas)
False
Example #3:
Tests pt = True, and alive 'in' 2
>>> pt=True
>>> canvas=[[True, True, True], [False, False, False], [False, False, False]]
>>> __judge_point(pt, canvas)
True
Example #4:
Tests pt = True, and alive 'in' 3
>>> pt=True
>>> canvas=[[True, True, True], [True, False, False], [False, False, False]]
>>> __judge_point(pt, canvas)
True
Example #5:
Tests pt = True, and alive > 3; expected 'alive' count = 4
>>> pt=True
>>> canvas=[[True, True, True], [True, False, False], [False, False, True]]
>>> __judge_point(pt, canvas)
False
Example #6:
Tests pt = False, and alive == 3
>>> pt=False
>>> canvas=[[True, False, False], [False, False, True], [False, True, False]]
>>> __judge_point(pt, canvas)
True
Example #7:
Tests pt = False, and alive != 3; expected 'alive' count = 0
>>> pt=False
>>> canvas=[[False, False, False], [False, False, False], [False, False, False]]
>>> __judge_point(pt, canvas)
False
"""
dead = 0
alive = 0
# finding dead or alive neighbours count.
for i in neighbours:
for status in i:
if status:
alive += 1
else:
dead += 1
# handling duplicate entry for focus pt.
if pt:
alive -= 1
else:
dead -= 1
# running the rules of game here.
state = pt
if pt:
if alive < 2:
state = False
elif alive in {2, 3}:
state = True
elif alive > 3:
state = False
elif alive == 3:
state = True
return state
if __name__ == "__main__":
if len(sys.argv) != 2:
raise Exception(usage_doc)
doctest.testmod()
canvas_size = int(sys.argv[1])
# main working structure of this module.
c = create_canvas(canvas_size)
seed(c)
fig, ax = plt.subplots()
fig.show()
cmap = ListedColormap(["w", "k"])
try:
while True:
c = run(c)
ax.matshow(c, cmap=cmap)
fig.canvas.draw()
ax.cla()
except KeyboardInterrupt:
# do nothing.
pass