-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
191 lines (136 loc) · 5.55 KB
/
bot.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
import random
import threading
import pyautogui
import numpy
from pynput.keyboard import Key, Listener
from settings import CELL_EXPLOADED_MINE, CELL_MINE, MINESWEEPER_MACOS
from util import find_game, identify_tile
class MinesweeperBot:
def __init__(self, settings=MINESWEEPER_MACOS):
self.board, self.board_size, self.mine_count, is_valid_game = find_game()
self.game_state = numpy.zeros(self.board_size)
self.mines = set([])
self.status = "playing"
if not is_valid_game:
print("Sorry - no game found. Exiting")
return
def keyboard_listener(self):
def on_press(key):
pass
def on_release(key):
print('{0} release'.format(
key))
if key== Key.f5:
with open("debug.txt", "w") as f:
f.write(numpy.array2string(self.game_state))
if key == Key.esc:
# Stop listener
return False
# Collect events until released
listener = Listener(on_press=on_press, on_release=on_release)
thread = threading.Thread(target=listener.start)
thread.start()
def click(self, coordinates, button="left"):
left, top, right, bottom = self.board[coordinates[1], coordinates[0]]
#For Macs with Retna display - there are 4 pixels per mouse coordinate
# Divide pixel by 2 to get correct mouse coordinate
x = ((left + right) // 2) // 2
y = ((bottom + top) // 2) // 2
pyautogui.click(x, y, button=button)
def read_board(self):
# Take a screenshot of the whole board once
tile_image = pyautogui.screenshot()
for i in range(self.board_size[1]):
for j in range(self.board_size[0]):
if self.game_state[j, i] == CELL_EXPLOADED_MINE:
self.status = "lost"
return
if self.game_state[j, i] == 0:
# If all the mines have been found, then any covered squares can be clicked
if len(self.mines) == self.mine_count:
self.click((j, i))
continue
left, top, right, bottom = self.board[i, j]
# Crop the relevant section from the screenshot
cropped = tile_image.crop((left, top, right, bottom))
tile = identify_tile(cropped)
if tile == 10:
filename = f"./tiles/unknown{i}-{j}.png"
cropped.save(filename)
self.game_state[j, i] = -999
else:
self.game_state[j, i] = tile
def get_neighbors(self, x, y):
directions = [
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)
]
neighbors = []
for dx, dy in directions:
new_x, new_y = x + dx, y + dy
if 0 <= new_x < self.board_size[1] and 0 <= new_y < self.board_size[0]:
neighbors.append((new_x, new_y))
return neighbors
def deduce_obvious_mines(self, coordinates):
x, y = coordinates
cell_value = self.game_state[y, x]
covered = []
for neighbor_x, neighbor_y in self.get_neighbors(x, y):
neighbor_value = self.game_state[neighbor_y, neighbor_x]
if neighbor_value == -1:
cell_value -= 1
elif neighbor_value == 0:
covered.append((neighbor_x, neighbor_y))
if cell_value == len(covered):
self.mines.update(covered)
for mine in covered:
if not self.game_state[mine[0], mine[1]] == CELL_MINE:
self.click(mine, button="right")
self.game_state[mine[0], mine[1]] = CELL_MINE
def find_safe_move(self, coordinates):
x, y = coordinates
cell_value = self.game_state[y, x]
covered = []
for neighbor_x, neighbor_y in self.get_neighbors(x, y):
neighbor_value = self.game_state[neighbor_y, neighbor_x]
if neighbor_value == -1:
cell_value -= 1
elif neighbor_value == 0:
covered.append((neighbor_x, neighbor_y))
if cell_value == 0:
return covered
return []
def determine_move(self):
safe_set = set([])
covered_unkown = []
for i in range(self.board_size[1]):
for j in range(self.board_size[0]):
if self.game_state[j, i] == 0:
covered_unkown.append((i, j))
continue
self.deduce_obvious_mines((i, j))
safe = self.find_safe_move((i, j))
safe_set.update(safe)
if len(safe_set) > 0:
return safe_set
else:
random = self.guess(covered_unkown)
print(covered_unkown)
print(random)
return [random]
def guess(self, covered):
return random.choice(covered)
game = MinesweeperBot()
#once to bring into focus
game.keyboard_listener()
game.click((0, 0))
game.click((3, 5))
while len(game.mines) < game.mine_count or game.status == "playing":
game.read_board()
safe = game.determine_move()
if len(safe) == 0:
game.guess()
for cell in safe:
game.click(cell)
game.read_board()