-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
166 lines (132 loc) · 4.26 KB
/
game.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import sys
import os.path
import random
import time
def grid_dims(grid):
return len(grid), len(grid[0])
def read_grid(inp):
with open(inp) as f:
w, h = map(int, f.readline().split())
grid = []
for y in range(h):
grid.append([0] * w)
for line in f:
y, x = map(int, line.split())
grid[y][x] = 1
return grid
def save_grid(grid, filename):
with open(filename, "w") as f:
w, h = grid_dims(grid)
f.write(f"{w} {h}\n")
for y, row in enumerate(grid):
for x, cell in enumerate(row):
if cell:
f.write(f"{y} {x}\n")
def save_grid_as_rle(grid, filename, overwrite=False):
if os.path.exists(filename) and not overwrite:
raise FileExistsError
w, h = grid_dims(grid)
with open(filename, "w") as f:
f.write(f"x = {w}, y = {h}, rule = B3/S23:P{w},{h}\n")
str = ""
val = None
run = 0
for y, row in enumerate(grid):
for x, cell in enumerate(row):
if val is None:
val = cell
run = 1
elif val == cell:
run += 1
else:
str += "{}{}".format(run if run > 1 else "", "o" if val == 1 else "b")
if len(str) > 68:
f.write(f"{str}\n")
str = ""
val = cell
run = 1
if val is not None:
str += "{}{}".format(run if run > 1 else "", "o" if val == 1 else "b")
val = None
str += "$"
if len(str) > 68:
f.write(f"{str}\n")
str = ""
f.write(f"{str}!\n")
def create_grid(w, h, alive=0.5, filename=None, overwrite=False):
if alive < 0 or alive > 1.0:
raise ValueError("Invalid alive percentage.")
if not filename:
filename = f"input_{w}x{h}_{alive}.txt"
if os.path.exists(filename) and not overwrite:
raise FileExistsError
m = round(w * h * alive)
skip = {}
with open(filename, "w") as f:
f.write(f"{w} {h}\n")
while m > 0:
x, y = random.randrange(w), random.randrange(h)
idx = y * w + x
if idx not in skip:
f.write(f"{y} {x}\n")
skip[idx] = True
m -= 1
def tick(grid):
w, h = grid_dims(grid)
temp = []
for y in range(h):
temp.append([0] * w)
for y, row in enumerate(grid):
for x, cell in enumerate(row):
count = 0
if y > 0:
count += grid[y - 1][x - 1] if x > 0 else 0
count += grid[y - 1][x]
count += grid[y - 1][x + 1] if x < w - 1 else 0
count += grid[y][x - 1] if x > 0 else 0
count += grid[y][x + 1] if x < w - 1 else 0
if y < h - 1:
count += grid[y + 1][x - 1] if x > 0 else 0
count += grid[y + 1][x]
count += grid[y + 1][x + 1] if x < w - 1 else 0
if cell and count >= 2 and count <= 3:
cell = 1
elif not cell and count == 3:
cell = 1
else:
cell = 0
temp[y][x] = cell
return temp
def main():
try:
inp = sys.argv[1]
except IndexError:
sys.exit("No input filename.")
try:
out = sys.argv[2]
except IndexError:
sys.exit("No output filename.")
try:
print(f"Reading {inp}...")
grid = read_grid(inp)
except FileNotFoundError:
sys.exit("Input file not found.")
size = sys.getsizeof(grid)
for row in grid:
size += sys.getsizeof(row)
print("Size of grid: {} MB".format(round(size / 2**20, 2)))
try:
n = int(sys.argv[3])
except IndexError:
sys.exit("No number of generations.")
except ValueError:
sys.exit("Invalid number of generations.")
start = time.time()
for i in range(n):
print(f"Tick {i}...")
grid = tick(grid)
print("{} seconds elapsed for {} generations.".format(round(time.time() - start, 2), n))
print(f"Writing {out}...")
save_grid(grid, out)
if __name__ == "__main__":
main()