-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_life.py
138 lines (95 loc) · 3.68 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
'''
Rules of Conway's Game of Life
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells,
each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated".
Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically,
or diagonally adjacent. At each step in time, the following transitions occur:
- Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
The initial pattern constitutes the seed of the system. The first generation is created by applying the
above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete
moment at which this happens is sometimes called a tick (in other words, each generation is a pure function
of the preceding one). The rules continue to be applied repeatedly to create further generations.
'''
'''
Implementation method:
- Have a matrix with values of 0 or 1
- If 0, then print ' '
- If 1, then print '•'
'''
import random, time, copy
import sys
#-- CONSTANTS --#
SEED = 42 # Seed
R = 10 # Number of rows
C = 50 # Number of columns
DEAD = 0 # Value for Dead cells
ALIVE = 1 # Value for Living cells
DELAY = 0.05 # Value for the delay (in seconds)
#-- AUXILIAR FUNCTIONS --#
def number_of_neighbours(matrix, i, j):
num_neighbours = 0
directions = [(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)]
for dr, dc in directions:
ni, nj = i + dr, j + dc
if 0 <= ni < R and 0 <= nj < C:
if matrix[ni][nj] == 1:
num_neighbours += 1
return num_neighbours
def liveability_cell(cell, nn):
if cell == ALIVE:
if nn < 2 or nn > 3:
return DEAD
else:
return ALIVE
else:
if nn == 3:
return ALIVE
return DEAD
#-- MAIN FUNCTIONS --#
def next_generation(matrix):
for i in range(R):
for j in range(C):
num_neighbours = number_of_neighbours(matrix, i, j)
matrix[i][j] = liveability_cell(matrix[i][j], num_neighbours)
return matrix
def print_cells(matrix):
matrix_chars=""
for i in range(R):
line = ""
for j in range(C):
line += "•" if matrix[i][j] else " "
matrix_chars += line + ("\n" if i < R else "")
print()
print(matrix_chars, end='', flush=True)
def constant_setter():
global SEED, R, C
try:
if "-s" in sys.argv:
SEED = int(sys.argv[sys.argv.index("-s") + 1])
if "-r" in sys.argv:
R = int(sys.argv[sys.argv.index("-r") + 1])
if "-c" in sys.argv:
C = int(sys.argv[sys.argv.index("-c") + 1])
except:
print("Usage: python3 game_of_life.py [-seed (int)] [-r (int)] [-c (int)]")
#-- main() --#
if __name__ == "__main__":
constant_setter()
print(f'SEED= {SEED}, ROWS= {R}, COLUMNS= {C}\n')
random.seed(SEED)
matrix = [[random.randint(0, 2) for _ in range(C)] for _ in range(R)]
previous_matrix = copy.deepcopy(matrix)
while(1):
matrix = next_generation(matrix)
print_cells(matrix)
time.sleep(DELAY)
if previous_matrix == matrix:
break
else:
previous_matrix = copy.deepcopy(matrix)
print("\033[F" * (R+1), end='')