forked from oskar-skog/anonymine
/
test.py
294 lines (257 loc) · 8.7 KB
/
test.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/python
'''
`runmoore`, `runhex` and `runneumann` are demos to be played in a
80x24 terminal. The `output` function sets the output frequency.
runmoore(x=78, y=18, m=225)
runhex(x=39, y=18, m=112)
runneumann(x=78, y=18, m=225)
`slomobug` simulates a field known to trigger the bug discovered in
0.0.24 (fixed since 0.0.25).
`run623` is a generic field of 6x6x6 with 22 mines.
`run10` appears to be collecting some statistics of a 16x16 with 40 mines.
'''
import time
import sys
import anonymine_fields
import anonymine_solver
import anonymine_engine
import random
import signal
import pprint
import os
def output(field, argument):
print(field)
time.sleep(.04) # 25 Hz
def pypybug():
size = [5, 5]
start = (1, 1)
mines = [(3, 4), (4, 4), (4, 0)]
field = anonymine_fields.generic_field(size, True, True)
solver = anonymine_solver.solver()
solver.field = field
field.fill(mines)
field.reveal(start)
field.set_callback('input', output, None)
print(field)
print(solver.solve())
def slomobug():
print('A')
mines = [(28,5),(4,8),(16,3),(0,1),(25,4),(5,13),(12,6),(22,2),(15,6),
(14,5),(7,9),(22,4),(29,4),(14,14),(8,10),(24,0),(15,12),(8,15),(24,9),(13,3),
(2,13),(14,4),(6,1),(27,8),(6,6),(20,9),(2,8),(23,4),(22,11),(24,1),(5,2),
(0,3),(9,3),(29,7),(11,0),(11,8),(14,10),(14,11),(12,7),(7,8),(4,0),(5,15),
(22,12),(28,10),(0,12),(22,10),(6,10),(6,3),(26,2),(20,15),(2,12),(28,6),
(13,11),(23,9),(16,5),(26,6),(12,0),(16,15),(11,9),(26,12),(22,8),(4,1),(28,7),
(6,4),(16,11),(17,13),(2,7),(5,6),(0,11),(0,15),(21,7),(27,10),(28,9),(0,2),
(6,5),(27,9),(17,1),(25,8),(4,14),(2,6),(6,15),(26,11),(18,15),(9,4),(15,13),
(20,4),(18,9),(15,7),(2,9),(24,2),(16,7),(28,0),(5,14),(21,14),(29,5),(23,0),
(23,10),(9,2),(20,7)]
start = (9, 13)
field = anonymine_fields.generic_field([30, 16], True, True)
solver = anonymine_solver.solver()
solver.field = field
field.fill(mines)
field.reveal(start)
field.set_callback('input', output, None)
print(field)
print(solver.solve())
def runmoore(x=78, y=18, m=225):
field = anonymine_fields.generic_field([x, y])
field.set_callback('input', output, None)
print(field)
mines = field.all_cells()
random.shuffle(mines)
field.fill(mines[:m])
for mine in mines[m:]:
for neighbour in field.get_neighbours(mine):
if neighbour in mines[:m]:
break
else:
field.reveal(mine)
break
solver = anonymine_solver.solver()
solver.field = field
print(solver.solve())
def profile_solver(x, y, m):
field = anonymine_fields.generic_field([x, y])
mines = field.all_cells()
random.shuffle(mines)
field.fill(mines[:m])
for mine in mines[m:]:
for neighbour in field.get_neighbours(mine):
if neighbour in mines[:m]:
break
else:
field.reveal(mine)
break
solver = anonymine_solver.solver()
solver.field = field
solver.solve()
def run623():
field = anonymine_fields.generic_field([6, 6, 6])
mines = field.all_cells()
random.shuffle(mines)
field.fill(mines[:22])
field.reveal(mines[22])
solver = anonymine_solver.solver()
solver.field = field
ret = solver.solve()
if ret[0]:
print(field.field)
print(ret)
def runneumann(x=78, y=18, m=225):
field = anonymine_fields.generic_field([x, y], False)
field.set_callback('input', output, None)
mines = field.all_cells()
random.shuffle(mines)
field.fill(mines[:m])
for mine in mines[m:]:
for neighbour in field.get_neighbours(mine):
if neighbour in mines[:m]:
break
else:
field.reveal(mine)
break
solver = anonymine_solver.solver()
solver.field = field
print(solver.solve())
def runhex(x=39, y=18, m=112):
field = anonymine_fields.hexagonal_field(x, y)
field.set_callback('input', output, None)
mines = field.all_cells()
random.shuffle(mines)
field.fill(mines[:m])
for mine in mines[m:]:
for neighbour in field.get_neighbours(mine):
if neighbour in mines[:m]:
break
else:
field.reveal(mine)
break
solver = anonymine_solver.solver()
solver.field = field
print(solver.solve())
def bug1():
# BUG:
# sys.stdin.readline() in `ask` dies with IOError and
# errno=EINTR when the terminal gets resized after curses has
# been de-initialized.
# 1: SIGWINCH is sent by the terminal when the screen has been
# resized.
# 2: curses forgot to restore the signal handling of SIGWINCH
# to the default of ignoring the signal.
# NOTE: signal.getsignal(signal.SIGWINCH) incorrectly
# returns signal.SIG_DFL. (Default is to be ignored.)
# 3: Python fails to handle EINTR when reading from stdin.
# REFERENCES:
# Issue 3949: https://bugs.python.org/issue3949
# PEP 0457: https://www.python.org/dev/peps/pep-0475/
def handler(foo, bar):
print("Resized")
signal.signal(signal.SIGWINCH, handler)
while True:
sys.stdin.readline()
def init_time(width, height, n_mines, runs=10):
i = 0
dellen = 0
times = []
while i < runs:
i += 1
e = anonymine_engine.game_engine(
'testcfg',
width=width, height=height, mines=n_mines
)
coord = random.randint(0, width - 1), random.randint(0, height - 1)
start = time.time()
try:
e.init_field((coord))
except anonymine_engine.security_alert as e:
sys.stderr.write('\n{}\n'.format(e.message))
x = time.time() - start
del e
times.append(x)
times.sort()
msg = '{}: {} This: {} Avg: {} Median: {} Min/Max: {}/{}'.format(
'{}@{}x{}'.format(n_mines, width, height),
'{}/{}'.format(i, runs),
x,
sum(times)/len(times),
times[len(times)//2],
times[0], times[-1],
)
#sys.stderr.write('\b' * dellen)
sys.stderr.write('\x1b[2K\r')
dellen = len(msg) + 1
sys.stderr.write(msg)
sys.stderr.flush()
sys.stderr.write('\n')
return times
def run2(path):
f = open(path, 'w')
data = {
'20@10x10': init_time(10, 10, 20, 1000),
'80@20x20': init_time(20, 20, 80, 1000),
'180@30x30': init_time(30, 30, 180, 1000),
}
f.write(pprint.pformat(data))
f.close()
def chance(width, height, n_mines, runs):
spinner = '\\|/-'
solver = anonymine_solver.solver()
field = anonymine_fields.generic_field([width, height])
solver.field = field
starttime = time.time()
i = 0
success = 0.0
while i < runs:
i += 1
mines = field.all_cells()
random.shuffle(mines)
field.clear()
field.fill(mines[:n_mines])
for mine in mines[n_mines:]:
for neighbour in field.get_neighbours(mine):
if neighbour in mines[:n_mines]:
break
else:
field.reveal(mine)
break
progress = int(40.0 * i/runs)
success += solver.solve()[0]
ETA = (runs - i) * (time.time() - starttime)/i
sys.stderr.write('\x1b[2K\r{}@{}x{}:\t{}%\t[{}{}]{}\tETA: {}+{}'.format(
n_mines, width, height,
int(100.0 * success/i + 0.5),
'=' * progress, ' ' * (40 - progress),
spinner[i % len(spinner)],
int(ETA/86400), time.strftime('%H:%M:%S', time.gmtime(ETA))
))
sys.stderr.write('\n')
return success/runs
def run3(path):
os.system('clear')
f = open(path, 'w')
#data = {
#'20@10x10': chance(10, 10, 20, 1000),
#'45@15x15': chance(15, 15, 45, 1000),
#'80@20x20': chance(20, 20, 80, 1000),
#'125@25x25': chance(25, 25, 125, 1000),
#'180@30x30': chance(30, 30, 180, 1000),
#'245@35x35': chance(35, 35, 245, 1000),
#'320@40x40': chance(40, 40, 320, 1000),
#'405@45x45': chance(45, 45, 405, 1000),
#'500@50x50': chance(50, 50, 500, 1000),
#}
data = {}
times = {}
#for x in (20, 40, 80, 160):
#for x in (120, 60, 30, 15):
for x in (100, 50, 25):
mines = int(.2 * x**2)
key = '{}@{}x{}'.format(mines, x, x)
start = time.time()
#data[key] = chance(x, x, mines, 3000)
data[key] = chance(x, x, mines, 2000)
times[key] = time.time() - start
f.write(pprint.pformat({'times': times, 'data': data}))
f.close()