-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
196 lines (159 loc) · 4.62 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
import pyautogui as pg
import pygetwindow as pw
from random import choice
try:
window = pw.getWindowsWithTitle('Minesweeper X')[0]
except IndexError:
print("Game Not Running")
exit()
width = window.width
height = window.height
region = window.box
l = window.left
t = window.top
gridLeft = l + 15
gridTop = t + 101
columns = int((width - 30) / 16)
rows = int((height - 116) / 16)
print(f"Game Size:\nRows={rows}\nColumns={columns}")
# all the different things that will appear
# non-numbers:
flag = "Buttons/flag.png"
emptySpace = "Buttons/emptySpace.png"
unchecked = "Buttons/unchecked.png"
mine = "Buttons/mine.png"
hitMine = "Buttons/hitMine.png"
wrongFlag = "Buttons/wrongFlag.png"
# numbers:
one = "Buttons/one.png"
two = "Buttons/two.png"
three = "Buttons/three.png"
four = "Buttons/four.png"
five = "Buttons/five.png"
six = "Buttons/six.png"
seven = "Buttons/seven.png"
eight = "Buttons/eight.png"
# misc
win = "Buttons/win.png"
lose = "Buttons/lose.png"
reset = "Buttons/reset.png"
numbers = [one, two, three, four, five, six, seven, eight]
btnList = [unchecked, emptySpace, flag, mine, hitMine, wrongFlag] + numbers
def cell(box, ty):
left = box[0] - gridLeft
top = box[1] - gridTop
center = pg.center(box)
row = top // 16
column = left // 16
typ = {
unchecked: '-', emptySpace: ' ', one: '1', two: '2', three: '3', four: '4', five: '5',
six: '6', seven: '7', eight: '8', flag: 'F', mine: '*', hitMine: '!', wrongFlag: 'x'
}
d = {
"row": row,
"column": column,
"type": typ[ty],
'raw': ty,
"point": (row, column),
"center": center,
}
return d
def click(r, c, g, b):
if b == 'l':
for i in g:
if i["point"] == (r, c):
pg.click(i["center"])
elif b == 'r':
for i in g:
if i["point"] == (r, c):
pg.click(i["center"], button='right')
else:
for i in g:
if i["point"] == (r, c):
pg.click(i["center"], button='middle')
def newGame():
try:
pg.click(reset)
except:
try:
pg.click(win)
except:
pg.click(lose)
main()
def scan():
grid = []
for x in btnList:
for i in pg.locateAllOnScreen(x, confidence=0.95, region=region, grayscale=True):
grid.append(cell(i, x))
return grid
def square(r, c):
threeXthree = [(r + 1, c - 1), (r + 1, c), (r + 1, c + 1),
(r, c - 1), (r, c), (r, c + 1),
(r - 1, c - 1), (r - 1, c), (r - 1, c + 1)]
return threeXthree
def showGame(g):
for x in g:
if x['column'] == columns - 1:
print(f" {x['type']} ")
else:
print(f" {x['type']} ", end='')
print("")
def play(g):
notFound = True
for i in g:
r = i['row']
c = i['column']
if i['raw'] in numbers:
v = int(i['type'])
sq = square(r, c)
uCount = []
fCount = []
for j in g:
if j['raw'] == flag and j['point'] in sq:
fCount.append(j)
if j['raw'] == unchecked and j['point'] in sq:
uCount.append(j)
if len(uCount) + len(fCount) == v and uCount:
for x in uCount:
if x['raw'] != flag:
click(x["row"], x["column"], g, 'r')
x['raw'] = flag
x['type'] = 'F'
notFound = False
uCount.remove(x)
fCount.append(x)
else:
ot = oneTwoRule(sq)
if ot:
click(ot['row'],ot['column'],g,'r')
ot['raw'] = flag
ot['type'] = 'F'
notFound = False
uCount.remove(ot)
fCount.append(ot)
if len(fCount) == v and uCount:
click(r, c, g, 'm')
for x in uCount:
notFound = False
uCount.remove(x)
if notFound:
unchecks = [x for x in g if x["raw"] == unchecked]
if unchecks:
r = choice(unchecks)
click(r["row"], r["column"], g, 'l')
print("Random Choice")
else:
print("Game Won")
newGame()
main()
def oneTwoRule(sq):
pass
def main():
game = sorted(scan(), key=lambda d: d['point'])
for x in game:
if x["type"] in ('*', '!'):
print("Game Over")
newGame()
play(game)
if __name__ == '__main__':
main()