-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
359 lines (313 loc) · 11.2 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
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
import pygame
import sys
import time
import argparse
import math
from minesweeper import *
def positive_nonzero_int(value):
ivalue = int(value)
if ivalue <= 0:
raise argparse.ArgumentTypeError(f"{value} is not a positive non-zero integer")
return ivalue
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--setting",type=positive_nonzero_int, default=[], nargs=3,
help="Board hight, Board width, The number of mines in the board")
args = parser.parse_args()
if not args.setting:
h = 9
w = 9
m = 10
else:
h, w, m = args.setting
if (h * w) // 2 < m:
print("too many mines!")
exit(1)
if h < 3 or w < 3:
print("the size of board is too small!")
exit(1)
HEIGHT = h
WIDTH = w
MINES = m
print("HEIGHT =", h)
print("WIDTH =", w)
print("MINES =", m)
# Colors
BLACK = (0, 0, 0)
GRAY = (180, 180, 180)
WHITE = (255, 255, 255)
YELLOW = (240, 230, 140)
NUM_COLOR = [(0, 0, 255), (0, 128, 0), (255, 0, 0), (0, 0, 128),
(128, 0, 0), (0, 128, 128), (0, 0, 0), (128, 128, 128)]
# Create game
pygame.init()
size = width, height = 900, 500
screen = pygame.display.set_mode(size)
# Fonts
OPEN_SANS = "assets/fonts/OpenSans-Regular.ttf"
smallFont = pygame.font.Font(OPEN_SANS, 20)
mediumFont = pygame.font.Font(OPEN_SANS, 28)
largeFont = pygame.font.Font(OPEN_SANS, 40)
# Compute board size
BOARD_PADDING = 20
board_width = ((2 / 3) * width) - (BOARD_PADDING * 2)
board_height = height - (BOARD_PADDING * 2)
cell_size = int(min(board_width / WIDTH, board_height / HEIGHT))
board_origin = (BOARD_PADDING, BOARD_PADDING)
# Add images
flag = pygame.image.load("assets/images/flag.png")
flag = pygame.transform.scale(flag, (cell_size, cell_size))
mine = pygame.image.load("assets/images/mine.png")
mine = pygame.transform.scale(mine, (cell_size, cell_size))
# Create game and AI agent
game = Minesweeper(height=HEIGHT, width=WIDTH, mines=MINES)
ai = MinesweeperAI(height=HEIGHT, width=WIDTH, game=game)
# Keep track of revealed cells, flagged cells, and if a mine was hit
revealed = set()
flags = set()
safes = set()
lost = False
stuck = False
win = False
revealed_count = 0
ans_board = game.ans_board
print("initiate board:")
for b in game.ans_board:
print(b)
# Show instructions initially
instructions = True
init_flag = True
while True:
# Check if game quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(BLACK)
# Show game instructions
if instructions:
# Title
title = largeFont.render("Play Minesweeper", True, WHITE)
titleRect = title.get_rect()
titleRect.center = ((width / 2), 50)
screen.blit(title, titleRect)
# Rules
rules = [
"Click a cell to reveal it.",
"Right-click a cell to mark it as a mine.",
"Mark all mines successfully to win!"
]
for i, rule in enumerate(rules):
line = smallFont.render(rule, True, WHITE)
lineRect = line.get_rect()
lineRect.center = ((width / 2), 150 + 30 * i)
screen.blit(line, lineRect)
# Play game button
buttonRect = pygame.Rect((width / 4), (3 / 4) * height, width / 2, 50)
buttonText = mediumFont.render("Play Game", True, BLACK)
buttonTextRect = buttonText.get_rect()
buttonTextRect.center = buttonRect.center
pygame.draw.rect(screen, WHITE, buttonRect)
screen.blit(buttonText, buttonTextRect)
# Check if play button clicked
click, _, _ = pygame.mouse.get_pressed()
if click == 1:
mouse = pygame.mouse.get_pos()
if buttonRect.collidepoint(mouse):
instructions = False
time.sleep(0.3)
pygame.display.flip()
continue
# Draw board
cells = []
for i in range(HEIGHT):
row = []
for j in range(WIDTH):
# Draw rectangle for cell
rect = pygame.Rect(
board_origin[0] + j * cell_size,
board_origin[1] + i * cell_size,
cell_size, cell_size
)
if (i, j) in revealed:
pygame.draw.rect(screen, WHITE, rect)
else:
pygame.draw.rect(screen, GRAY, rect)
pygame.draw.rect(screen, WHITE, rect, 3)
# Add a mine, flag, or number if needed
if game.is_mine((i, j)) and lost:
screen.blit(mine, rect)
elif (i, j) in flags:
screen.blit(flag, rect)
elif game.is_mine((i, j)) and stuck:
screen.blit(mine, rect)
elif (i, j) in revealed:
nearby = game.nearby_mines((i, j))
if nearby:
neighbors = smallFont.render(
str(nearby),
True, NUM_COLOR[nearby - 1]
)
neighborsTextRect = neighbors.get_rect()
neighborsTextRect.center = rect.center
screen.blit(neighbors, neighborsTextRect)
elif (i, j) in safes:
nearby = game.nearby_mines((i, j))
if nearby:
neighbors = smallFont.render(
str(nearby),
True, GRAY
)
pygame.draw.rect(screen, YELLOW, rect)
neighborsTextRect = neighbors.get_rect()
neighborsTextRect.center = rect.center
screen.blit(neighbors, neighborsTextRect)
else:
pygame.draw.rect(screen, YELLOW, rect)
elif (lost or stuck) and (i, j) not in safes:
nearby = game.nearby_mines((i, j))
if nearby:
neighbors = smallFont.render(
str(nearby),
True, BLACK
)
pygame.draw.rect(screen, GRAY, rect)
neighborsTextRect = neighbors.get_rect()
neighborsTextRect.center = rect.center
screen.blit(neighbors, neighborsTextRect)
else:
pygame.draw.rect(screen, GRAY, rect)
row.append(rect)
cells.append(row)
if revealed_count >= round(math.sqrt(h*w)) and init_flag:
init_flag = False
# AI Move button
aiButton = pygame.Rect(
(2 / 3) * width + BOARD_PADDING, (1 / 3) * height - 50,
(width / 3) - BOARD_PADDING * 2, 50
)
buttonText = mediumFont.render("AI Move", True, BLACK)
buttonRect = buttonText.get_rect()
buttonRect.center = aiButton.center
pygame.draw.rect(screen, WHITE, aiButton)
screen.blit(buttonText, buttonRect)
# Reset button
resetButton = pygame.Rect(
(2 / 3) * width + BOARD_PADDING, (1 / 3) * height + 20,
(width / 3) - BOARD_PADDING * 2, 50
)
buttonText = mediumFont.render("Reset", True, BLACK)
buttonRect = buttonText.get_rect()
buttonRect.center = resetButton.center
pygame.draw.rect(screen, WHITE, resetButton)
screen.blit(buttonText, buttonRect)
# Display text
if lost:
text = "Lost"
elif stuck:
text = "Stuck"
elif win:
text = "Win"
else:
text = ""
text = mediumFont.render(text, True, WHITE)
textRect = text.get_rect()
textRect.center = ((5 / 6) * width, (2 / 3) * height)
screen.blit(text, textRect)
move = None
left, _, right = pygame.mouse.get_pressed()
if init_flag:
move = ai.init_knowledge()
revealed_count += 1
# Check for a right-click to toggle flagging
elif right == 1 and not lost:
mouse = pygame.mouse.get_pos()
for i in range(HEIGHT):
for j in range(WIDTH):
if cells[i][j].collidepoint(mouse) and (i, j) not in revealed:
if (i, j) in flags:
flags.remove((i, j))
else:
flags.add((i, j))
time.sleep(0.2)
elif left == 1:
mouse = pygame.mouse.get_pos()
# TODO: AI move part
if aiButton.collidepoint(mouse) and not lost:
time.sleep(0.2)
ai.add_knowledge()
print("KB len:", len(ai.knowledge))
print("KB0 len:", len(ai.knowledge0))
print("Known mines:", len(ai.mines))
print("Unknown mines:", len(game.mines) - len(ai.mines))
print("Safe", len(ai.safes))
# check the board
if not ai.mark_board(game.board, "Total check"):
print("ERROR!")
time.sleep(1)
safe_list = []
for s in ai.safes:
safe_list.append(s[:2])
safes = set(safe_list)
if len(ai.mines) == len(game.mines):
find_mine = ai.mines.copy()
mine_list = []
for m in find_mine:
mine_list.append(m[:2])
flags = set(mine_list)
win = True
print("WIN!!")
else:
find_mine = ai.mines.copy()
mine_list = []
for m in find_mine:
mine_list.append(m[:2])
flags = set(mine_list)
stuck = True
print("Stuck")
# Reset
elif resetButton.collidepoint(mouse):
game = Minesweeper(height=HEIGHT, width=WIDTH, mines=MINES)
ai = MinesweeperAI(height=HEIGHT, width=WIDTH, game=game)
revealed = set()
flags = set()
safes = set()
lost = False
stuck = False
win = False
init_flag = True
revealed_count = 0
ans_board = game.ans_board
print("initiate board:")
for b in game.ans_board:
print(b)
continue
# User-made move
elif not lost:
for i in range(HEIGHT):
for j in range(WIDTH):
if (cells[i][j].collidepoint(mouse)
and (i, j) not in flags
and (i, j) not in revealed):
move = (i, j)
ai.init_knowledge(move)
def make_move(move):
if game.is_mine(move):
return True
else:
# get the number of nearby mines
nearby = game.nearby_mines(move)
revealed.add(move)
if not nearby:
# Loop over all cells within one row and column
for i in range(move[0] - 1, move[0] + 2):
for j in range(move[1] - 1, move[1] + 2):
# Ignore the cell itself
if (i, j) == move:
continue
# Add to the cell collection if the cell is not yet explored
# and is not the mine already none
if 0 <= i < HEIGHT and 0 <= j < WIDTH and (i, j) not in revealed:
make_move((i, j))
if move:
if make_move(move):
lost = True
pygame.display.flip()