-
Notifications
You must be signed in to change notification settings - Fork 5
/
minesweeper.py
432 lines (374 loc) · 15.4 KB
/
minesweeper.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import cv2
import pyautogui
import time
import numpy as np
import random
class Minesweeper():
__author__: "EnriqueMoran"
def __init__(self, difficulty=1, mute=False, speed=0.85):
self.difficulty = difficulty # 0 = Easy, 1 = Normal, 2 = Hard
self.configImage = "./images/config.png" # images folder must be in the same directory as this script
self.muteImage = "./images/mute.png"
self.oneImage = "./images/one.png"
self.twoImage = "./images/two.png"
self.threeImage = "./images/three.png"
self.fourImage = "./images/four.png"
self.fiveImage = "./images/five.png"
self.mute = mute # mute game
self.speed = speed
self.gameWindow = None
self.cells = {} # {(x, y) : (posX, posY), cont} cont = ? (unknow), F (flag), 0 (safe), n (number)
self.nBombs = None
self.size = None # cell size
self.rows = None # number of rows
self.cols = None # number of columns
def getGameWindow(self):
def clearWindow(corners): # remove wrong detections
res = []
coords = [coord for coords in corners for coord in coords]
for coord in coords:
if coords.count(coord) > 1:
res.append(coord)
return [res[k:k+2] for k in range(0, len(res), 2)] # group by 2
screenshot = pyautogui.screenshot()
image = np.array(screenshot) # convert to opencv compatible format
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # filter by color
lower_green = np.array([80, 60, 80])
upper_green = np.array([81, 255, 255])
mask = cv2.inRange(hsv, lower_green, upper_green)
result = cv2.bitwise_and(image, image, mask=mask)
gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY) # filter by shape
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
threshold = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
im2, contours, hierarchy = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for i in contours: # filter by min size
size = cv2.contourArea(i)
if size > 10000: # get corners
gray = np.float32(gray)
mask = np.zeros(gray.shape, dtype="uint8")
cv2.fillPoly(mask, [i], (255, 255, 255))
dst = cv2.cornerHarris(mask, 5, 3, 0.04)
ret, dst = cv2.threshold(dst, 0.1 * dst.max(), 255, 0)
dst = np.uint8(dst)
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria)
if len(corners) == 5: # if its a square
return clearWindow(corners)
print("Game window not found!")
def initializeCells(self):
x0 = self.gameWindow[0][1]
y0 = self.gameWindow[0][0]
if self.difficulty == 0:
self.size = 45 # px
self.rows = 8
self.cols = 10
self.nBombs = 10
elif self.difficulty == 1:
self.size = 30
self.rows = 14
self.cols = 18
self.nBombs = 40
elif self.difficulty == 2:
self.size = 25
self.rows = 20
self.cols = 24
self.nBombs = 99
for row in range(self.rows):
for col in range(self.cols):
x = x0 + (row * self.size) + (self.size / 2)
y = y0 + (col * self.size) + (self.size / 2)
self.cells[(col, row)] = [(x, y), '?']
def muteSound(self): # find mute button and click on it if its not muted
try:
muteButton = pyautogui.locateOnScreen(self.muteImage, confidence=0.9)
mute_x = pyautogui.center(muteButton).x
mute_y = pyautogui.center(muteButton).y
pyautogui.moveTo(mute_x, mute_y, 0.2)
pyautogui.click()
except:
print("Mute sound button not found! (might be already muted)")
self.mute = False
def setDifficulty(self):
if self.difficulty not in [0, 1, 2]:
raise ValueError("Difficulty must be 0 (easy), 1 (normal) or 2 (hard)!")
try:
configButton = pyautogui.locateOnScreen(self.configImage, confidence=0.9)
config_x = pyautogui.center(configButton).x
config_y = pyautogui.center(configButton).y
pyautogui.moveTo(config_x, config_y, 0.2)
pyautogui.click()
if self.difficulty == 0: # easy
pyautogui.moveTo(config_x, config_y + 25, 0.5)
elif self.difficulty == 1: # normal
pyautogui.moveTo(config_x, config_y + 50, 0.5)
else: # hard
pyautogui.moveTo(config_x, config_y + 75, 0.5)
pyautogui.click()
except:
raise Exception("Flag image not found!")
def getScore(self, image):
image = cv2.resize(image, (30, 30))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
res = 0
resConfidence = 0
cont = 0
scores = [self.oneImage, self.twoImage, self.threeImage, self.fourImage, self.fiveImage]
matchTemplateMethod = eval('cv2.TM_CCOEFF_NORMED')
for score in scores:
cont += 1
score = cv2.imread(score, 0)
match = cv2.matchTemplate(image, score, matchTemplateMethod)
_, confidence, _, _ = cv2.minMaxLoc(match)
if confidence > 0.75 and confidence > resConfidence:
resConfidence = confidence
res = cont
return res
def getColor(self, color):
r, g, b = color
if 20 <= r <= 35: # check blue color
if 115 <= g <= 130:
if 205 <= b <= 220:
return 1
if 50 <= r <= 65: # check green color
if 135 <= g <= 150:
if 55 <= b <= 70:
return 2
if 205 <= r <= 220: # check red color
if 40 <= g <= 55:
if 40 <= b <= 55:
return 3
if 120 <= r <= 135: # check purple color
if 25 <= g <= 30:
if 155 <= b <= 170:
return 4
if 240 <= r <= 255: # check orange color
if 135 <= g <= 150:
if 0 <= b <= 15:
return 5
if 240 <= r <= 255: # check orange color
if 135 <= g <= 150:
if 0 <= b <= 15:
return 5
if 0 <= r <= 15: # check blue color
if 145 <= g <= 160:
if 160 <= b <= 175:
return 6
return 0 # No match
def updateCells(self):
screenshot = pyautogui.screenshot()
x0 = self.gameWindow[0][1]
y0 = self.gameWindow[0][0]
thr = [10 if self.difficulty == 0 else 6 if self.difficulty == 1 else 4][0] # cell border thickness
for row in range(self.rows):
for col in range(self.cols):
x = x0 + (row * self.size + self.size / 2)
y = y0 + (col * self.size + self.size / 2)
x1 = x0 + (row * self.size + thr)
y1 = y0 + (col * self.size + thr)
if self.cells[(col, row)][1] == 'F':
pass
elif screenshot.getpixel((int(y1), int(x1))) not in [(162, 209, 73), (170, 215, 81)]: # not an unkwnown cell
if screenshot.getpixel((int(y1), int(x1))) in [(229, 194, 159), (215, 184, 153), (236, 209, 183), (225, 202, 179)]: # not a green cell
colors = [screenshot.getpixel((int(y) - thy, int(x) - thx)) for thy in range(-4, 4) for thx in range(-4, 4)]
scores = [self.getColor(color) for color in colors]
scores.sort(reverse=True)
score = scores[0]
self.cells[(col, row)] = [(y + self.size / 2, x + self.size / 2), score]
def getNeighbors(self, col, row): # return unknown neighbors
res = []
try:
if self.cells[col - 1, row - 1][1] == '?':
res.append((col - 1, row - 1))
except:
pass
try:
if self.cells[col, row - 1][1] == '?':
res.append((col, row - 1))
except:
pass
try:
if self.cells[col + 1, row - 1][1] == '?':
res.append((col + 1, row - 1))
except:
pass
try:
if self.cells[col - 1, row][1] == '?':
res.append((col - 1, row))
except:
pass
try:
if self.cells[col + 1, row][1] == '?':
res.append((col + 1, row))
except:
pass
try:
if self.cells[col - 1, row + 1][1] == '?':
res.append((col - 1, row + 1))
except:
pass
try:
if self.cells[col, row + 1][1] == '?':
res.append((col, row + 1))
except:
pass
try:
if self.cells[col + 1, row + 1][1] == '?':
res.append((col + 1, row + 1))
except:
pass
return res
def getFlags(self, col, row): # return flagged neighbors
res = []
try:
if self.cells[col - 1, row - 1][1] == 'F':
res.append((col - 1, row - 1))
except:
pass
try:
if self.cells[col, row - 1][1] == 'F':
res.append((col, row - 1))
except:
pass
try:
if self.cells[col + 1, row - 1][1] == 'F':
res.append((col + 1, row - 1))
except:
pass
try:
if self.cells[col - 1, row][1] == 'F':
res.append((col - 1, row))
except:
pass
try:
if self.cells[col + 1, row][1] == 'F':
res.append((col + 1, row))
except:
pass
try:
if self.cells[col - 1, row + 1][1] == 'F':
res.append((col - 1, row + 1))
except:
pass
try:
if self.cells[col, row + 1][1] == 'F':
res.append((col, row + 1))
except:
pass
try:
if self.cells[col + 1, row + 1][1] == 'F':
res.append((col + 1, row + 1))
except:
pass
return res
def selectCell(self, lastCell):
col, th_x = lastCell[0], lastCell[0]
row, th_y = lastCell[1], lastCell[1]
dx = 0
dy = -1
cont = 0 # cells checked (in board)
while cont < self.rows * self.cols:
if col < self.cols and row < self.rows and col >= 0 and row >= 0:
score = self.cells[(col, row)][1]
if score != 0 and score != 'F':
try:
neighbors = self.getNeighbors(col, row)
flaggeds = self.getFlags(col, row)
# print((col, row), score, len(neighbors), len(flaggeds)) # debug
if len(flaggeds) < int(score) and len(neighbors) + len(flaggeds) <= int(score):
return (neighbors, 0) # select cells to place flag
elif len(flaggeds) == int(score) and len(neighbors) > 0:
return (neighbors, 1) # select cells to click
except:
pass
cont += 1
col -= th_x
row -= th_y
if col == row or (col < 0 and col == -row) or (col > 0 and col == 1 - row):
dx, dy = -dy, dx
col, row = col + dx + th_x, row + dy + th_y
return (None, 2) # select random cell to click
def start(self):
x0 = self.gameWindow[0][1]
y0 = self.gameWindow[0][0]
pyautogui.moveTo(y0 + 5, x0 + 5)
pyautogui.click()
def placeFlag(self, cell):
x, y = self.cells[(cell[0], cell[1])][0]
pyautogui.moveTo(y, x)
pyautogui.click(button='right')
self.cells[(cell[0], cell[1])][1] = 'F'
def showGrid(self):
print("")
cont = 0
cells = list(self.cells.values())
for i in range(self.rows):
for j in range(self.cols):
print(cells[cont][1], end="")
cont += 1
print("")
def randomCell(self): # click on random unknown cell
unknownCells = {cell: value[0] for cell, value in self.cells.items() if value[1] == '?'}
cell = random.choice(list(unknownCells.keys()))
x, y = self.cells[cell][0][0], self.cells[cell][0][1]
pyautogui.moveTo(y, x)
pyautogui.click()
print("random!")
return cell
def checkLose(self, nBombs):
lose = False
# time.sleep(0.1 * nBombs) # ensure score screen is shown
screenshot = pyautogui.screenshot()
if self.difficulty == 0:
x, y = self.cells[(2, 0)][0]
elif self.difficulty == 1:
x, y = self.cells[(5, 1)][0]
elif self.difficulty == 2:
x, y = self.cells[(7, 4)][0]
color = screenshot.getpixel((int(y), int(x)))
if color == (77, 193, 249):
lose = True
return lose
def play(self): # game loop
print("To stop this script place the mouse on top-left corner of the screen.")
if self.mute:
self.muteSound()
self.setDifficulty()
self.gameWindow = self.getGameWindow()
self.initializeCells()
self.start()
time.sleep(0.85)
self.updateCells()
# self.updateCells() # remove false positive
# self.showGrid()
lastCell = (0, 0) # last clicked cell
while self.nBombs >= 0:
cells, click = self.selectCell(lastCell)
if click == 0:
for cell in cells:
self.placeFlag(cell)
self.nBombs -= 1
lastCell = cell
elif click == 1:
for cell in cells:
x, y = self.cells[(cell[0], cell[1])][0]
pyautogui.moveTo(y, x)
pyautogui.click()
lastCell = cell
elif click == 2: # click random cell
lastCell = self.randomCell()
if self.checkLose(self.nBombs):
print("I lose")
break
time.sleep(self.speed)
self.updateCells()
# self.updateCells()
# self.showGrid()
print("I won!")
return
if __name__ == "__main__":
difficulty = 1 # normal difficulty
mute = True # mute sound
speed = 0.05 # time between each move
minesweeper = Minesweeper(difficulty, mute, speed)
minesweeper.play()