-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathMain.py
360 lines (277 loc) · 12 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import time
import mouseOperation
import random
import imageProcess
import cv2
import time
class BoomMine:
__inited = False
blocks_x, blocks_y = -1, -1
width, height = -1, -1
img_cv, img = -1, -1
blocks_img = [[-1 for i in range(blocks_y)] for i in range(blocks_x)]
blocks_num = [[-3 for i in range(blocks_y)] for i in range(blocks_x)]
blocks_is_mine = [[0 for i in range(blocks_y)] for i in range(blocks_x)]
is_new_start = True
next_steps = []
@staticmethod
def rgb_to_bgr(rgb):
return rgb[2], rgb[1], rgb[0]
@staticmethod
def equal(arr1, arr2):
if arr1[0] == arr2[0] and arr1[1] == arr2[1] and arr1[2] == arr2[2]:
return True
return False
def is_in_form(self, location):
x, y = location[0], location[1]
if x < self.left or x > self.right or y < self.top or y > self.bottom:
return False
return True
def iterate_blocks_image(self, func):
if self.__inited:
for y in range(self.blocks_y):
for x in range(self.blocks_x):
# args are: self, [0]singleBlockImage, [1]location(as an array)
func(self, self.blocks_img[x][y], (x, y))
def iterate_blocks_number(self, func):
if self.__inited:
for y in range(self.blocks_y):
for x in range(self.blocks_x):
# args are: self, [0]singleBlockNumber, [1]location(as an array)
func(self, self.blocks_num[x][y], (x, y))
def analyze_block(self, block, location):
x, y = location[0], location[1]
now_num = self.blocks_num[x][y]
# if 1:
if not now_num == -2 and not 0 < now_num < 9:
block = imageProcess.pil_to_cv(block)
block_color = block[8, 8]
# -1:Not opened
# -2:Opened but blank
# -3:Un initialized
# Opened
if self.equal(block_color, self.rgb_to_bgr((192, 192, 192))):
if not self.equal(block[8, 1], self.rgb_to_bgr((255, 255, 255))):
self.blocks_num[x][y] = -2
self.is_started = True
else:
self.blocks_num[x][y] = -1
elif self.equal(block_color, self.rgb_to_bgr((0, 0, 255))):
self.blocks_num[x][y] = 1
elif self.equal(block_color, self.rgb_to_bgr((0, 128, 0))):
self.blocks_num[x][y] = 2
elif self.equal(block_color, self.rgb_to_bgr((255, 0, 0))):
self.blocks_num[x][y] = 3
elif self.equal(block_color, self.rgb_to_bgr((0, 0, 128))):
self.blocks_num[x][y] = 4
elif self.equal(block_color, self.rgb_to_bgr((128, 0, 0))):
self.blocks_num[x][y] = 5
elif self.equal(block_color, self.rgb_to_bgr((0, 128, 128))):
self.blocks_num[x][y] = 6
elif self.equal(block_color, self.rgb_to_bgr((0, 0, 0))):
if self.equal(block[6, 6], self.rgb_to_bgr((255, 255, 255))):
# Is mine
self.blocks_num[x][y] = 9
elif self.equal(block[5, 8], self.rgb_to_bgr((255, 0, 0))):
# Is flag
self.blocks_num[x][y] = 0
else:
self.blocks_num[x][y] = 7
elif self.equal(block_color, self.rgb_to_bgr((128, 128, 128))):
self.blocks_num[x][y] = 8
else:
self.blocks_num[x][y] = -3
self.is_mine_form = False
if self.blocks_num[x][y] == -3 or not self.blocks_num[x][y] == -1:
self.is_new_start = False
def detect_mine(self, block, location):
def generate_kernel(k, k_width, k_height, block_location):
ls = []
loc_x, loc_y = block_location[0], block_location[1]
for now_y in range(k_height):
for now_x in range(k_width):
if k[now_y][now_x]:
rel_x, rel_y = now_x - 1, now_y - 1
ls.append((loc_y + rel_y, loc_x + rel_x))
return ls
def count_unopen_blocks(blocks):
count = 0
for single_block in blocks:
if self.blocks_num[single_block[1]][single_block[0]] == -1:
count += 1
return count
def mark_as_mine(blocks):
for single_block in blocks:
if self.blocks_num[single_block[1]][single_block[0]] == -1:
self.blocks_is_mine[single_block[1]][single_block[0]] = 1
x, y = location[0], location[1]
if self.blocks_num[x][y] > 0:
kernel_width, kernel_height = 3, 3
# Kernel mode:[Row][Col]
kernel = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
# Left border
if x == 0:
for i in range(kernel_height):
kernel[i][0] = 0
# Right border
if x == self.blocks_x - 1:
for i in range(kernel_height):
kernel[i][kernel_width - 1] = 0
# Top border
if y == 0:
for i in range(kernel_width):
kernel[0][i] = 0
# Bottom border
if y == self.blocks_y - 1:
for i in range(kernel_width):
kernel[kernel_height - 1][i] = 0
# Generate the search map
to_visit = generate_kernel(kernel, kernel_width, kernel_height, location)
unopen_blocks = count_unopen_blocks(to_visit)
if unopen_blocks == self.blocks_num[x][y]:
mark_as_mine(to_visit)
def detect_to_click_block(self, block, location):
def generate_kernel(k, k_width, k_height, block_location):
ls = []
loc_x, loc_y = block_location[0], block_location[1]
for now_y in range(k_height):
for now_x in range(k_width):
if k[now_y][now_x]:
rel_x, rel_y = now_x - 1, now_y - 1
ls.append((loc_y + rel_y, loc_x + rel_x))
return ls
def count_mines(blocks):
count = 0
for single_block in blocks:
if self.blocks_is_mine[single_block[1]][single_block[0]] == 1:
count += 1
return count
def mark_to_click_block(blocks):
for single_block in blocks:
# Not Mine
if not self.blocks_is_mine[single_block[1]][single_block[0]] == 1:
# Click-able
if self.blocks_num[single_block[1]][single_block[0]] == -1:
# Source Syntax: [y][x] - Converted
if not (single_block[1], single_block[0]) in self.next_steps:
self.next_steps.append((single_block[1], single_block[0]))
x, y = location[0], location[1]
if block > 0:
kernel_width, kernel_height = 3, 3
# Kernel mode:[Row][Col]
kernel = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
# Left border
if x == 0:
for i in range(kernel_height):
kernel[i][0] = 0
# Right border
if x == self.blocks_x - 1:
for i in range(kernel_height):
kernel[i][kernel_width - 1] = 0
# Top border
if y == 0:
for i in range(kernel_width):
kernel[0][i] = 0
# Bottom border
if y == self.blocks_y - 1:
for i in range(kernel_width):
kernel[kernel_height - 1][i] = 0
# Generate the search map
to_visit = generate_kernel(kernel, kernel_width, kernel_height, location)
mines_count = count_mines(to_visit)
if mines_count == block:
mark_to_click_block(to_visit)
def rel_loc_to_real(self, block_rel_location):
return self.left + 16 * block_rel_location[0] + 8, self.top + 16 * block_rel_location[1] + 8
def __init__(self):
self.next_steps = []
self.left = 0
self.top = 0
self.right = 0
self.bottom = 0
self.continue_random_click = False
self.is_mine_form = True
self.is_started = False
self.have_solve = False
if self.process_once():
self.__inited = True
def show_map(self):
if self.__inited:
for y in range(self.blocks_y):
line = ""
for x in range(self.blocks_x):
if self.blocks_num[x][y] == -1:
line += " "
else:
line += str(self.blocks_num[x][y]) + " "
print(line)
def show_mine(self):
if self.__inited:
for y in range(self.blocks_y):
line = ""
for x in range(self.blocks_x):
if self.blocks_is_mine[x][y] == 0:
line += " "
else:
line += str(self.blocks_is_mine[x][y]) + " "
print(line)
def process_once(self):
# Initialize
result = imageProcess.get_frame()
if result == -1:
print("Minesweeper Arbiter Window Not Detected!")
return False
self.img, self.blocks_img, form_size, img_size, form_location = result
self.blocks_num = [[-1 for i in range(self.blocks_y)] for i in range(self.blocks_x)]
self.blocks_is_mine = [[0 for i in range(self.blocks_y)] for i in range(self.blocks_x)]
self.next_steps = []
self.is_new_start = True
self.is_mine_form = True
self.blocks_x, self.blocks_y = form_size[0], form_size[1]
self.width, self.height = img_size[0], img_size[1]
self.img_cv = imageProcess.pil_to_cv(self.img)
self.left, self.top, self.right, self.bottom = form_location
# Analyze the number of blocks
self.iterate_blocks_image(BoomMine.analyze_block)
# Mark all mines
self.iterate_blocks_number(BoomMine.detect_mine)
# Calculate where to click
self.iterate_blocks_number(BoomMine.detect_to_click_block)
self.have_solve = False
if len(self.next_steps) > 0:
self.have_solve = True
if self.is_in_form(mouseOperation.get_mouse_point()):
for to_click in self.next_steps:
on_screen_location = self.rel_loc_to_real(to_click)
mouseOperation.mouse_move(on_screen_location[0], on_screen_location[1])
mouseOperation.mouse_click()
# If your computer's performance is not high, enable this:
# time.sleep(0.001)
if not self.have_solve and self.is_mine_form:
rand_location = (random.randint(0, self.blocks_x - 1), random.randint(0, self.blocks_y - 1))
rand_x, rand_y = rand_location[0], rand_location[1]
iter_times = 0
if len(self.blocks_is_mine) > 0:
while self.blocks_is_mine[rand_x][rand_y] or not self.blocks_num[rand_x][
rand_y] == -1 and iter_times < 50:
rand_location = (random.randint(0, self.blocks_x - 1), random.randint(0, self.blocks_y - 1))
rand_x, rand_y = rand_location[0], rand_location[1]
iter_times += 1
screen_location = self.rel_loc_to_real((rand_location[0], rand_location[1]))
if self.is_in_form(mouseOperation.get_mouse_point()):
mouseOperation.mouse_move(screen_location[0], screen_location[1])
mouseOperation.mouse_click()
else:
self.is_mine_form = False
cv2.imshow("Sweeper Screenshot", self.img_cv)
if cv2.waitKey(1) & 0xFF == ord('q'):
return False
return True
miner = BoomMine()
while 1:
try:
miner.process_once()
except:
pass
# miner.show_map()
# print(miner.next_steps)