/
conway.py
90 lines (74 loc) · 2.47 KB
/
conway.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
from matrixutils import rand_bool_matrix_2d
from geomutils import neighbours, POINTS8
import numpy as np
import printBoolArray
import argparse
# we can have negative coordinates... as we merely check for neighbours we don't make elaborate checking a= [3,4] -2 is a valid index, -3 is not ... our function would fail for such a case
def valid(coord, dim):
if len(coord) != len(dim):
return False
return all([a < b for a, b in zip(coord, dim)])
def force_lesser_than(coord, dim):
x, y = coord
l, c = dim
return (x % l), (y % c)
def torus_neighbours(coord, dim):
neighs = neighbours(coord, POINTS8)
res = []
for neigh in neighs:
res += [force_lesser_than(neigh, dim)]
return res
def get_neighbours_state(matr, coord):
dim = matr.shape
neighs = torus_neighbours(coord, dim)
res = []
for neigh in neighs:
res += [matr[neigh]]
return res
def about_next_life(matr, coord):
neigh_states = get_neighbours_state(matr, coord)
nb = sum(neigh_states)
if matr[coord] == True:
if nb < 2:
return False
if nb in (2, 3):
return True
return False
return nb == 3
def conway(matr):
res = np.copy(matr)
for i, row in enumerate(matr):
for j, cell in enumerate(row):
res[(i, j)] = about_next_life(matr, (i, j))
return res
def type_probability(p):
str_p = p
p = float(p)
if 0.0 <= p <= 1.0:
return p
else:
msg = str_p + ' is not a probability (between 0 and 1)'
raise argparse.ArgumentTypeError(msg)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Conway''s game of Life')
parser.add_argument('n', type=int, help='number of rows')
parser.add_argument('m', type=int, help='number of columns')
parser.add_argument('p', type=type_probability, help='probability of life at start')
parser.add_argument('--nb-steps', type=int, help='number of generations')
args = parser.parse_args()
n = args.n
m = args.m
p = args.p
nb_steps = args.nb_steps
# glider
# matr = np.array([[False,False,False,False,False],
# [False,False,True,False,False],
# [False,False,False,True,False],
# [False,True,True,True,False],
# [False,False,False,False,False]])
matr = rand_bool_matrix_2d(n, m, p)
printBoolArray.printMatrix(matr)
c = matr
for i in range(nb_steps):
c = conway(c)
printBoolArray.printMatrix(c)