-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (109 loc) · 4.68 KB
/
main.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
import os
import argparse
import numpy as np
class World:
def __init__(self, width: int, height: int) -> None:
self.width = width
self.height = height
self.world = np.zeros((height, width), dtype=np.int0)
def _get_world(self, i: int, j: int) -> np.int0:
return self.world[i][j]
def __str__(self) -> str:
output = ""
for row in self.world:
for grid in row:
output += "0" if grid else " "
output += "\n"
return output
def step(self) -> None:
new_world = np.copy(self.world)
for i in range(self.height):
for j in range(self.width):
if i == 0 and j == 0:
count = self._get_world(i + 1, j) + \
self._get_world(i, j + 1) + \
self._get_world(i + 1, j + 1)
elif i == 0 and j == self.width - 1:
count = self._get_world(i, j - 1) + \
self._get_world(i + 1, j - 1) + \
self._get_world(i + 1, j)
elif i == self.height - 1 and j == 0:
count = self._get_world(i - 1, j) + \
self._get_world(i - 1, j + 1) + \
self._get_world(i, j + 1)
elif i == self.height - 1 and j == self.width - 1:
count = self._get_world(i, j - 1) + \
self._get_world(i - 1, j - 1) + \
self._get_world(i - 1, j)
elif i == self.height - 1:
count = self._get_world(i, j - 1) + \
self._get_world(i - 1, j - 1) + \
self._get_world(i - 1, j) + \
self._get_world(i - 1, j + 1) + \
self._get_world(i, j + 1)
elif j == self.width - 1:
count = self._get_world(i - 1, j) + \
self._get_world(i - 1, j - 1) + \
self._get_world(i, j - 1) + \
self._get_world(i + 1, j - 1) + \
self._get_world(i + 1, j)
elif i == 0:
count = self._get_world(i, j - 1) + \
self._get_world(i + 1, j - 1) + \
self._get_world(i + 1, j) + \
self._get_world(i + 1, j + 1) + \
self._get_world(i, j + 1)
elif j == 0:
count = self._get_world(i - 1, j) + \
self._get_world(i - 1, j + 1) + \
self._get_world(i, j + 1) + \
self._get_world(i + 1, j + 1) + \
self._get_world(i + 1, j)
else:
count = self._get_world(i, j - 1) + \
self._get_world(i - 1, j - 1) + \
self._get_world(i - 1, j) + \
self._get_world(i - 1, j + 1) + \
self._get_world(i, j + 1) + \
self._get_world(i + 1, j + 1) + \
self._get_world(i + 1, j) + \
self._get_world(i + 1, j - 1)
if self._get_world(i, j):
if count < 2 or count > 3:
new_world[i][j] = False
elif not self._get_world(i, j) and count == 3:
new_world[i][j] = True
self.world = new_world
def initial_population(self, number: int) -> None:
assert number < self.width * self.height
i = 0
while i < number:
row = np.random.randint(0, self.height)
col = np.random.randint(0, self.width)
while self.world[row][col]:
row = np.random.randint(0, self.height)
col = np.random.randint(0, self.width)
self.world[row][col] = True
i += 1
def run(self) -> None:
try:
while True:
os.system("clear")
print(self)
self.step()
except KeyboardInterrupt:
os.system("clear")
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--width', help="width of the world", type=int,
default=50)
parser.add_argument('--height', help="height of the world", type=int,
default=50)
parser.add_argument('--init_pop', help="initial population of life cells",
type=int, default=1000)
args = parser.parse_args()
env = World(args.width, args.height)
env.initial_population(args.init_pop)
env.run()
if __name__ == "__main__":
main()