-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.py
executable file
·323 lines (280 loc) · 11.2 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
import itertools
import random
import copy
class Minesweeper():
"""
Minesweeper game representation
"""
def __init__(self, height=8, width=8, mines=8):
# Set initial width, height, and number of mines
self.height = height
self.width = width
self.mines = set()
# Initialize an empty field with no mines
self.board = []
for i in range(self.height):
row = []
for j in range(self.width):
row.append(False)
self.board.append(row)
# Add mines randomly
while len(self.mines) != mines:
i = random.randrange(height)
j = random.randrange(width)
if not self.board[i][j]:
self.mines.add((i, j))
self.board[i][j] = True
# At first, player has found no mines
self.mines_found = set()
def print(self):
"""
Prints a text-based representation
of where mines are located.
"""
for i in range(self.height):
print("--" * self.width + "-")
for j in range(self.width):
if self.board[i][j]:
print("|X", end="")
else:
print("| ", end="")
print("|")
print("--" * self.width + "-")
def is_mine(self, cell):
i, j = cell
return self.board[i][j]
def nearby_mines(self, cell):
"""
Returns the number of mines that are
within one row and column of a given cell,
not including the cell itself.
"""
# Keep count of nearby mines
count = 0
# Loop over all cells within one row and column
for i in range(cell[0] - 1, cell[0] + 2):
for j in range(cell[1] - 1, cell[1] + 2):
# Ignore the cell itself
if (i, j) == cell:
continue
# Update count if cell in bounds and is mine
if 0 <= i < self.height and 0 <= j < self.width:
if self.board[i][j]:
count += 1
return count
def won(self):
"""
Checks if all mines have been flagged.
"""
return self.mines_found == self.mines
class Sentence():
"""
Logical statement about a Minesweeper game
A sentence consists of a set of board cells,
and a count of the number of those cells which are mines.
"""
def __init__(self, cells, count):
self.cells = set(cells)
self.count = count
def __eq__(self, other):
return self.cells == other.cells and self.count == other.count
def __str__(self):
return f"{self.cells} = {self.count}"
def known_mines(self):
"""
Returns the set of all cells in self.cells known to be mines.
"""
if self.count == len(self.set):
return self.cells
def known_safes(self):
"""
Returns the set of all cells in self.cells known to be safe.
"""
if self.count == 0:
return self.cells
def mark_mine(self, cell):
"""
Updates internal knowledge representation given the fact that
a cell is known to be a mine.
"""
if cell in self.cells:
self.cells.remove(cell)
self.count -= 1
def mark_safe(self, cell):
"""
Updates internal knowledge representation given the fact that
a cell is known to be safe.
"""
if cell in self.cells:
self.cells.remove(cell)
class MinesweeperAI():
"""
Minesweeper game player
"""
def __init__(self, height=8, width=8):
# Set initial height and width
self.height = height
self.width = width
# Keep track of which cells have been clicked on
self.moves_made = set()
# Keep track of cells known to be safe or mines
self.mines = set()
self.safes = set()
# List of sentences about the game known to be true
self.knowledge = []
def mark_mine(self, cell):
"""
Marks a cell as a mine, and updates all knowledge
to mark that cell as a mine as well.
"""
self.mines.add(cell)
for sentence in self.knowledge:
sentence.mark_mine(cell)
def mark_safe(self, cell):
"""
Marks a cell as safe, and updates all knowledge
to mark that cell as safe as well.
"""
self.safes.add(cell)
for sentence in self.knowledge:
sentence.mark_safe(cell)
def add_knowledge(self, cell, count):
"""
Called when the Minesweeper board tells us, for a given
safe cell, how many neighboring cells have mines in them.
This function should:
1) mark the cell as a move that has been made
2) mark the cell as safe
3) add a new sentence to the AI's knowledge base
based on the value of `cell` and `count`
4) mark any additional cells as safe or as mines
if it can be concluded based on the AI's knowledge base
5) add any new sentences to the AI's knowledge base
if they can be inferred from existing knowledge
"""
self.moves_made.add(cell) # mark the cell as a move that has been made
self.mark_safe(cell) # mark the cell as safe
# updates sentences that contain cell
# removing empty knowledge
for sent in self.knowledge:
if len(sent.cells) == 0:
self.knowledge.remove(sent)
# removing safes and mines from knowledge
for sent in self.knowledge:
for safe in self.safes:
if safe in sent.cells.copy():
# print(f'removing new safe {safe} from {sent.cells} = {sent.count}\n')
sent.cells.difference_update({safe})
for mine in self.mines:
if mine in sent.cells.copy():
# print(f'removing new mine {mine} from {sent.cells} = {sent.count}\n')
sent.cells.difference_update({mine})
sent.count -= 1
# add a new sentence to the AI's knowledge
neighbors = []
if cell[0] == 0:
if cell[1] == 0: #(0,0) corner
for n1 in range(0,2):
for n2 in range(0,2):
neighbors.append((cell[0]+n1,cell[1]+n2))
elif cell[1] == self.width-1: #(0,7) corner
for n1 in range(0,2):
for n2 in range(-1,1):
neighbors.append((cell[0]+n1,cell[1]+n2))
else:# (0,j) row
for n1 in range(0,2):
for n2 in range(-1,2):
neighbors.append((cell[0]+n1,cell[1]+n2))
elif cell[0] == self.height-1:
if cell[1] == 0:#(7,0) corner
for n1 in range(-1,1):
for n2 in range(0,2):
neighbors.append((cell[0]+n1,cell[1]+n2))
elif cell[1] == self.width-1:#(7,7) corner
for n1 in range(-1,1):
for n2 in range(-1,1):
neighbors.append((cell[0]+n1,cell[1]+n2))
else:#(7,j) row
for n1 in range(-1,1):
for n2 in range(-1,2):
neighbors.append((cell[0]+n1,cell[1]+n2))
elif (cell[1] == 0) and (cell[0]>0) and (cell[0]<self.width-1):#(i,0) column
for n1 in range(-1,2):
for n2 in range(0,2):
neighbors.append((cell[0]+n1,cell[1]+n2))
elif (cell[1] == self.height-1) and (cell[0]>0) and (cell[0]<self.width-1):#(i,7) column
for n1 in range(-1,2):
for n2 in range(-1,1):
neighbors.append((cell[0]+n1,cell[1]+n2))
else:
for n1 in range(-1,2):
for n2 in range(-1,2):
neighbors.append((cell[0]+n1,cell[1]+n2))
# remove safes from neighbors
aux = copy.deepcopy(self.safes)
for neigh in neighbors:
if neigh in self.safes:
neighbors.remove(neigh)
elif neigh in self.mines:
if count > 0:
count -= 1
neighbors.remove(neigh)
#else:
#print('Warning: negative count!')
self.knowledge.append(Sentence(neighbors,count))
# print(f'adding knowledge: {self.knowledge[-1].cells}={self.knowledge[-1].count}')
# mark any additional cells as safe
# {A,B}=0 => A=B=0
for sent in self.knowledge:
aux = []
if sent.count == 0:
for newsafe in sent.cells.copy():
self.mark_safe(newsafe)
aux.append(newsafe)
self.knowledge.remove(sent)
# print(f'{aux} safe cells added.')
# mark any additional cells as mines
#{A,B,C}=3 => A=B=C=1
elif sent.count == len(sent.cells):
aux = []
for newmine in sent.cells.copy():
self.mark_mine(newmine)
aux.append(newmine)
self.knowledge.remove(sent)
# print(f'{aux} mine cells added.')
# removing empty knowledge
for sent in self.knowledge:
if len(sent.cells) == 0:
self.knowledge.remove(sent)
# add any new sentences from inference to the AI's knowledge base
# {A,B,C,D,E}=3 and {A,B,C}=1 => {D,E}=2
for sent0 in self.knowledge:
for sent1 in self.knowledge:
if (sent0.cells < sent1.cells):
# print(f'getting infered knowledge from {sent0.cells} = {sent0.count} and {sent1.cells} = {sent1.count}\n')
self.knowledge.append(Sentence(sent1.cells.difference(sent0.cells),sent1.count-sent0.count))
self.knowledge.remove(sent1)
# print(f'infered knowledge added:{self.knowledge[-1].cells} = {self.knowledge[-1].count}. Removing {sent1.cells} = {sent1.count}')
def make_safe_move(self):
"""
Returns a safe cell to choose on the Minesweeper board.
The move must be known to be safe, and not already a move
that has been made.
This function may use the knowledge in self.mines, self.safes
and self.moves_made, but should not modify any of those values.
"""
if len(self.safes)-len(self.moves_made) > 0:
return self.safes.difference(self.moves_made).pop()
#return auxset.pop()
def make_random_move(self):
"""
Returns a move to make on the Minesweeper board.
Should choose randomly among cells that:
1) have not already been chosen, and
2) are not known to be mines
"""
if len(self.safes)-len(self.moves_made) < 1:
auxcell = (random.randint(0,self.width-1),random.randint(0,self.height-1))
# while (random move is not a safe or a mine) and (there are unchecked squares)
while (auxcell not in self.moves_made.union(self.mines)) and (len(self.safes)+len(self.mines)<(self.height-1)*(self.width-1)):
return auxcell