/
ai.py
194 lines (183 loc) · 8.51 KB
/
ai.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
import pygame
from pygame.locals import *
from sdlBased import SDLBoard
from time import sleep
from random import sample
class MineSweeper:
def __init__(self, board):
self.board = board
self.safeSquares = set()
self.probablySafeSquares = []
self.bombSquares = set()
self.uncovered = set()
self.allSquares = {(x, y) for x in range(self.board.width) for y in range(self.board.height)}
self.clickedSquare = (-1, -1)
def getBombNeighbors(self, (x, y)):
s = set()
for (dx, dy) in [(0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, 0), (1, -1), (1, 1)]:
x_ = x + dx
y_ = y + dy
if 0 <= x_ < self.board.width and 0 <= y_ < self.board.height and self.board.board2D[y_][x_] is None and (x_, y_) in self.bombSquares:
s.add((x_, y_))
return s
def getNonBombNeighbors(self, (x, y)):
s = set()
for (dx, dy) in [(0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, 0), (1, -1), (1, 1)]:
x_ = x + dx
y_ = y + dy
if 0 <= x_ < self.board.width and 0 <= y_ < self.board.height and self.board.board2D[y_][x_] is None and (x_, y_) not in self.bombSquares:
s.add((x_, y_))
return s
def get9Block(self, (x, y)):
s = set()
for (dx, dy) in [(0, 1), (-1, 1), (0, 0), (-1, 0), (-1, -1), (0, -1), (1, 0), (1, -1), (1, 1)]:
x_ = x + dx
y_ = y + dy
if 0 <= x_ < self.board.width and 0 <= y_ < self.board.height:
s.add((x_, y_))
return s
def get8Block(self, (x, y)):
s = set()
for (dx, dy) in [(0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, 0), (1, -1), (1, 1)]:
x_ = x + dx
y_ = y + dy
if 0 <= x_ < self.board.width and 0 <= y_ < self.board.height:
s.add((x_, y_))
return s
def step(self, maxBorderSize = 20, clearAllSafe = False):
if self.safeSquares:
if clearAllSafe:
for (bestX, bestY) in self.safeSquares:
uncovered = self.board.dig(bestX, bestY)
if uncovered is not None:
self.uncovered |= uncovered
self.safeSquares = set()
else:
for (bestX, bestY) in self.safeSquares:
uncovered = self.board.dig(bestX, bestY)
if uncovered is not None:
self.uncovered |= uncovered
break
self.safeSquares.remove((bestX, bestY))
self.clickedSquare = (bestX, bestY)
else:
self.board.update2DBoard()
covered = self.allSquares - self.uncovered - self.bombSquares - self.safeSquares
if len(self.uncovered) > len(covered):
uncoveredEdge = set()
for pos in covered:
uncoveredEdge |= self.get8Block(pos) & self.uncovered
else:
uncoveredEdge = self.uncovered
for (x, y) in uncoveredEdge:
val = self.board.board2D[y][x]
if val != 0:
bombNeighbors = self.getBombNeighbors((x, y))
nonBombNeighbors = self.getNonBombNeighbors((x, y))
if val == len(bombNeighbors) + len(nonBombNeighbors):
for (x_, y_) in nonBombNeighbors:
self.bombSquares.add((x_, y_))
elif val == len(bombNeighbors):
for (x_, y_) in nonBombNeighbors:
self.safeSquares.add((x_, y_))
if self.safeSquares:
return
else:
boundary = set()
covered = self.allSquares - self.uncovered - self.bombSquares - self.safeSquares
for pos in self.uncovered:
boundary |= self.get9Block(pos)
boundary &= covered
boundaryMapping = {cell: {cell} for cell in boundary}
changes = True
while changes:
changes = False
for cell in self.uncovered:
neighbors = self.get9Block(cell) & boundary
if neighbors:
piece = set()
for ncell in neighbors:
piece |= boundaryMapping[ncell]
for ncell in neighbors:
if boundaryMapping[ncell] != piece:
changes = True
boundaryMapping[ncell] = piece
boundaryPieces = []
alreadyAdded = set()
for pieceSeed in boundaryMapping:
if pieceSeed not in alreadyAdded:
piece = boundaryMapping[pieceSeed]
boundaryPieces.append(piece)
alreadyAdded |= piece
self.backupBombSquares = self.bombSquares.copy()
self.probablySafeSquares = []
boundaryPieces.sort(key = lambda t: len(t))
for piece in boundaryPieces:
n = len(piece)
if n < maxBorderSize:
pieceL = list(piece)
countD = {x : [0, 0] for x in piece}
relevantUncovered = set()
for cell in pieceL:
relevantUncovered |= self.get9Block(cell) & self.uncovered
totalCount = 0
for i in range(2**n):
idx = 0
while i:
if i%2:
self.bombSquares.add(pieceL[idx])
i = int(i/2)
idx += 1
for (x, y) in relevantUncovered:
if len(self.getBombNeighbors((x, y))) != self.board.board2D[y][x]:
break
else:
for cell in countD:
if cell in self.bombSquares:
countD[cell][0] += 1
countD[cell][1] += 1
self.bombSquares = self.backupBombSquares.copy()
for cell in piece:
if countD[cell][0] == 0:
self.safeSquares.add(cell)
else:
countD[cell][0] /= countD[cell][1]
if countD[cell][0] >= 0.99:
self.bombSquares.add(cell)
else:
self.probablySafeSquares.append((cell, countD[cell][0]))
if self.safeSquares:
self.probablySafeSquares.sort(key = lambda t: t[1], reverse = True)
return
self.probablySafeSquares.sort(key = lambda t: t[1], reverse = True)
if self.safeSquares:
return
elif self.probablySafeSquares:
self.safeSquares.add(self.probablySafeSquares.pop()[0])
else:
self.safeSquares.add(sample(self.allSquares - self.uncovered - self.bombSquares, 1)[0])
return
if __name__ == "__main__":
width = 10
height = 10
bombs = 10
mineSize = 30
states = {'victory': 0, 'defeat': 0}
while True:
sdlBoard = SDLBoard(width, height, bombs, mineSize)
minesweeper = MineSweeper(sdlBoard)
sdlBoard.refreshScreen(minesweeper.bombSquares, minesweeper.safeSquares)
i = 0
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
minesweeper.step(clearAllSafe = True)
sdlBoard.check()
i += 1
if False and i % 10000 == 0:
sdlBoard.refreshScreen(minesweeper.bombSquares, {minesweeper.clickedSquare})
if sdlBoard.state != "ongoing":
states[sdlBoard.state] += 1
print(states)
break