-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameoflife.py
111 lines (86 loc) · 3.28 KB
/
gameoflife.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
# # game of life
# # rules
#
# * Any live cell with fewer than two live neighbors dies (underpopulation).
# * Any live cell with two or three live neighbors continues to live.
# * Any live cell with more than three live neighbors dies (overpopulation).
# * Any dead cell with exactly three live neighbors becomes a live cell (reproduction).
import random
import time
from hashlib import sha256
statuses = [0, 0, 0, 1] # 0.25 of the grid will start live
def build_canvas(row_num: int, col_num: int) -> list:
canvas = []
for i in range(0, row_num):
row_list = []
for j in range(0, col_num):
row_list.append(random.choice(statuses))
canvas.append(row_list)
return canvas
def get_cell_properties(row_value: int, col_value: int) -> tuple:
live = canvas[row_value][col_value]
canvas_height = len(canvas)
canvas_width = len(canvas[0])
# count neighbours
count = 0
first_col = True if col_value == 0 else False
last_col = True if col_value == canvas_width - 1 else False
first_row = True if row_value == 0 else False
last_row = True if row_value == canvas_height - 1 else False
if not first_row and not first_col:
count += canvas[row_value - 1][col_value - 1]
if not first_row:
count += canvas[row_value - 1][col_value]
if not first_row and not last_col:
count += canvas[row_value - 1][col_value + 1]
if not first_col:
count += canvas[row_value][col_value - 1]
if not last_col:
count += canvas[row_value][col_value + 1]
if not last_row and not first_col:
count += canvas[row_value + 1][col_value - 1]
if not last_row:
count += canvas[row_value + 1][col_value]
if not last_row and not last_col:
count += canvas[row_value + 1][col_value + 1]
return live, count
def run_cycle(canvas_input: list) -> list:
buffer = canvas # make a list of lists to update
for i in range(0, len(canvas)):
# print(f'row is {i}')
for j in range(0, len(canvas[1])):
# print(f'col is {j}')
cell_properties = get_cell_properties(i, j)
if cell_properties[0] == 1: # cell is alive
if cell_properties[1] < 2:
status = 0
elif cell_properties[1] > 3:
status = 0
else:
status = 1
elif cell_properties[0] == 0: # cell is dead
if cell_properties[1] == 3:
status = 1
else:
status = 0
# print(f'was {cell_properties[0]} now {status}')
buffer[i][j] = status # set the status of the cell in the buffer
return buffer
canvas = build_canvas(15, 15)
seen_again = 0
while seen_again < 10:
# for i in range(0, 500):
this_hash = sha256(str(canvas).encode("utf-8")).hexdigest()
run_cycle(canvas)
next_hash = sha256(str(canvas).encode("utf-8")).hexdigest()
if next_hash == this_hash:
seen_again += 1
else:
seen_again = 0
this_hash = next_hash
for row in canvas:
row = ["*" if x == 1 else " " for x in row]
print("".join(row))
# print("".join(str(row)).replace("0", " ").replace("1", "X"))
# time.sleep(0.1)
print("\033c") # clear console