2020-05-22 06:10:11 +00:00
|
|
|
"""Conway's Game Of Life, Author Anurag Kumar(mailto:anuragkumarak95@gmail.com)
|
2017-10-26 06:41:32 +00:00
|
|
|
|
|
|
|
Requirements:
|
|
|
|
- numpy
|
|
|
|
- random
|
|
|
|
- time
|
|
|
|
- matplotlib
|
|
|
|
|
|
|
|
Python:
|
|
|
|
- 3.5
|
|
|
|
|
|
|
|
Usage:
|
2023-07-31 21:24:12 +00:00
|
|
|
- $python3 game_of_life <canvas_size:int>
|
2017-10-26 06:41:32 +00:00
|
|
|
|
|
|
|
Game-Of-Life Rules:
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2017-10-26 06:41:32 +00:00
|
|
|
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.
|
2024-03-13 06:52:41 +00:00
|
|
|
"""
|
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
import random
|
|
|
|
import sys
|
|
|
|
|
2017-10-26 06:41:32 +00:00
|
|
|
import numpy as np
|
|
|
|
from matplotlib import pyplot as plt
|
|
|
|
from matplotlib.colors import ListedColormap
|
|
|
|
|
2023-05-10 09:34:36 +00:00
|
|
|
usage_doc = "Usage of script: script_name <size_of_canvas:int>"
|
2017-10-26 06:41:32 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
choice = [0] * 100 + [1] * 10
|
2017-10-26 06:41:32 +00:00
|
|
|
random.shuffle(choice)
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-10-22 09:45:30 +00:00
|
|
|
def create_canvas(size: int) -> list[list[bool]]:
|
2019-10-05 05:14:13 +00:00
|
|
|
canvas = [[False for i in range(size)] for j in range(size)]
|
2017-10-26 06:41:32 +00:00
|
|
|
return canvas
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2021-10-22 09:45:30 +00:00
|
|
|
def seed(canvas: list[list[bool]]) -> None:
|
2019-10-05 05:14:13 +00:00
|
|
|
for i, row in enumerate(canvas):
|
|
|
|
for j, _ in enumerate(row):
|
|
|
|
canvas[i][j] = bool(random.getrandbits(1))
|
|
|
|
|
2017-10-26 06:41:32 +00:00
|
|
|
|
2021-10-22 09:45:30 +00:00
|
|
|
def run(canvas: list[list[bool]]) -> list[list[bool]]:
|
2023-07-31 21:24:12 +00:00
|
|
|
"""
|
|
|
|
This function runs the rules of game through all points, and changes their
|
2020-06-16 08:09:19 +00:00
|
|
|
status accordingly.(in the same canvas)
|
2017-10-26 06:41:32 +00:00
|
|
|
@Args:
|
|
|
|
--
|
|
|
|
canvas : canvas of population to run the rules on.
|
|
|
|
|
|
|
|
@returns:
|
|
|
|
--
|
2023-07-31 21:24:12 +00:00
|
|
|
canvas of population after one step
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2021-10-22 09:45:30 +00:00
|
|
|
current_canvas = np.array(canvas)
|
|
|
|
next_gen_canvas = np.array(create_canvas(current_canvas.shape[0]))
|
|
|
|
for r, row in enumerate(current_canvas):
|
2017-10-26 06:41:32 +00:00
|
|
|
for c, pt in enumerate(row):
|
2019-10-05 05:14:13 +00:00
|
|
|
next_gen_canvas[r][c] = __judge_point(
|
2021-10-22 09:45:30 +00:00
|
|
|
pt, current_canvas[r - 1 : r + 2, c - 1 : c + 2]
|
2019-10-05 05:14:13 +00:00
|
|
|
)
|
|
|
|
|
2023-07-31 21:24:12 +00:00
|
|
|
return next_gen_canvas.tolist()
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-10-26 06:41:32 +00:00
|
|
|
|
2021-10-22 09:45:30 +00:00
|
|
|
def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
|
2019-10-05 05:14:13 +00:00
|
|
|
dead = 0
|
2017-10-26 06:41:32 +00:00
|
|
|
alive = 0
|
|
|
|
# finding dead or alive neighbours count.
|
|
|
|
for i in neighbours:
|
|
|
|
for status in i:
|
2019-10-05 05:14:13 +00:00
|
|
|
if status:
|
|
|
|
alive += 1
|
|
|
|
else:
|
|
|
|
dead += 1
|
2017-10-26 06:41:32 +00:00
|
|
|
|
|
|
|
# handling duplicate entry for focus pt.
|
2019-10-05 05:14:13 +00:00
|
|
|
if pt:
|
|
|
|
alive -= 1
|
|
|
|
else:
|
|
|
|
dead -= 1
|
|
|
|
|
2017-10-26 06:41:32 +00:00
|
|
|
# running the rules of game here.
|
|
|
|
state = pt
|
|
|
|
if pt:
|
2019-10-05 05:14:13 +00:00
|
|
|
if alive < 2:
|
|
|
|
state = False
|
2023-07-22 10:05:10 +00:00
|
|
|
elif alive in {2, 3}:
|
2019-10-05 05:14:13 +00:00
|
|
|
state = True
|
|
|
|
elif alive > 3:
|
|
|
|
state = False
|
2024-03-28 17:25:41 +00:00
|
|
|
elif alive == 3:
|
|
|
|
state = True
|
2017-10-26 06:41:32 +00:00
|
|
|
|
|
|
|
return state
|
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) != 2:
|
|
|
|
raise Exception(usage_doc)
|
|
|
|
|
2017-10-26 06:41:32 +00:00
|
|
|
canvas_size = int(sys.argv[1])
|
|
|
|
# main working structure of this module.
|
2019-10-05 05:14:13 +00:00
|
|
|
c = create_canvas(canvas_size)
|
2017-10-26 06:41:32 +00:00
|
|
|
seed(c)
|
|
|
|
fig, ax = plt.subplots()
|
2019-10-05 05:14:13 +00:00
|
|
|
fig.show()
|
|
|
|
cmap = ListedColormap(["w", "k"])
|
2017-10-26 06:41:32 +00:00
|
|
|
try:
|
|
|
|
while True:
|
2019-10-05 05:14:13 +00:00
|
|
|
c = run(c)
|
|
|
|
ax.matshow(c, cmap=cmap)
|
2017-10-26 06:41:32 +00:00
|
|
|
fig.canvas.draw()
|
2019-10-05 05:14:13 +00:00
|
|
|
ax.cla()
|
2017-10-26 06:41:32 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
# do nothing.
|
|
|
|
pass
|