-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.py
404 lines (394 loc) · 16.6 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import sys, platform, random, time, tkinter as tk
def add_mines_numbers(board, mines):
# Add mines
for _ in range(mines):
not_ready = True
while not_ready == True:
space1 = random.randint(0,len(board) - 1)
space2 = random.randint(0,len(board) - 1)
if board[space1][space2] == " ":
board[space1][space2] = "✶"
not_ready = False
# Add numbers for mines
i_row = -1
for row in board:
i_row += 1
i_item = -1
for item in row:
mines_count = 0
i_item += 1
if item == " ":# This item - board[i_row][i_item]
if i_row - 1 >= 0:
if i_item - 1 >= 0:
if board[i_row - 1][i_item - 1] == "✶":# Up left
mines_count += 1
if board[i_row - 1][i_item] == "✶":# Up
mines_count += 1
try:
if board[i_row - 1][i_item + 1] == "✶":# Up right
mines_count += 1
except:pass
if i_item - 1 >= 0:
if board[i_row][i_item - 1] == "✶":# Left
mines_count += 1
try:
if board[i_row][i_item + 1] == "✶":# Right
mines_count += 1
except:pass
try:
if i_item - 1 >= 0:
if board[i_row + 1][i_item - 1] == "✶":# Down left
mines_count += 1
if board[i_row + 1][i_item] == "✶":# Down
mines_count += 1
if board[i_row + 1][i_item + 1] == "✶":# Down right
mines_count += 1
except:pass
# Add number
if mines_count > 0:
board[i_row][i_item] = str(mines_count)
return board
def scan_empty_around_marks(board_2):
# If around marks found open empty spaces - open this mark
i_row = -1
for row in board_2:
i_row += 1
i_item = -1
for item in row:
i_item += 1
if item == "►":
empty_around_marks = 0
if i_row - 1 >= 0:
if i_item - 1 >= 0:
if board_2[i_row - 1][i_item - 1] == " ":# Up left
empty_around_marks += 1
if board_2[i_row - 1][i_item] == " ":# Up
empty_around_marks += 1
try:
if board_2[i_row - 1][i_item + 1] == " ":# Up right
empty_around_marks += 1
except:pass
if i_item - 1 >= 0:
if board_2[i_row][i_item - 1] == " ":# left
empty_around_marks += 1
try:
if board_2[i_row][i_item + 1] == " ":# right
empty_around_marks += 1
except:pass
if i_row + 1 <= len(board_2) - 1:
if i_item - 1 >= 0:
if board_2[i_row + 1][i_item - 1] == " ":# Down left
empty_around_marks += 1
if board_2[i_row + 1][i_item] == " ":# Down
empty_around_marks += 1
try:
if board_2[i_row + 1][i_item + 1] == " ":# Down right
empty_around_marks += 1
except:pass
# Open empty space
if empty_around_marks >= 1:
board_2[i_row][i_item] = " "
return board_2
def scan_space(board, board_2, item_y, item_x):
directions = []
# Open empty spaces
if board[item_y][item_x] == " ":
board_2[item_y][item_x] = " "
# Scan empty spaces on directions
if item_y - 1 >= 0:
if board[item_y - 1][item_x] == " " and (board_2[item_y - 1][item_x] == "-" or board_2[item_y - 1][item_x] == "►"):# Up
directions.append([item_y - 1, item_x])
if item_x - 1 >= 0:
if board[item_y][item_x - 1] == " " and (board_2[item_y][item_x - 1] == "-" or board_2[item_y][item_x - 1] == "►"):# Left
directions.append([item_y, item_x - 1])
try:
if board[item_y][item_x + 1] == " " and (board_2[item_y][item_x + 1] == "-" or board_2[item_y][item_x + 1] == "►"):# Right
directions.append([item_y, item_x + 1])
except:pass
if item_y + 1 <= len(board) - 1:
if board[item_y + 1][item_x] == " " and (board_2[item_y + 1][item_x] == "-" or board_2[item_y + 1][item_x] == "►"):# Down
directions.append([item_y + 1, item_x])
# Looks into next empty spaces
ready = False
counter1 = 0
while ready == False:
try:
board_2 = scan_space(board, board_2, directions[counter1][0], directions[counter1][1])
counter1 += 1
except:
ready = True
# Opening numbers near by empty"s
if item_y - 1 >= 0:
if board[item_y - 1][item_x] != " ":
board_2[item_y - 1][item_x] = board[item_y - 1][item_x]# Up
if item_x - 1 >= 0:
if board[item_y - 1][item_x - 1] != " ":
board_2[item_y - 1][item_x - 1] = board[item_y - 1][item_x - 1]# Up left
try:
if board[item_y - 1][item_x + 1] != " ":
board_2[item_y - 1][item_x + 1] = board[item_y - 1][item_x + 1]# Up right
except:pass
if item_x - 1 >= 0:
if board[item_y][item_x - 1] != " ":
board_2[item_y][item_x - 1] = board[item_y][item_x - 1]# Left
try:
if board[item_y][item_x + 1] != " ":
board_2[item_y][item_x + 1] = board[item_y][item_x + 1]# Right
except:pass
if item_y + 1 <= len(board) - 1:
if board[item_y + 1][item_x] != " ":
board_2[item_y + 1][item_x] = board[item_y + 1][item_x]# Down
if item_x - 1 >= 0:
if board[item_y + 1][item_x - 1] != " ":
board_2[item_y + 1][item_x - 1] = board[item_y + 1][item_x - 1]# Down left
try:
if board[item_y + 1][item_x + 1] != " ":
board_2[item_y + 1][item_x + 1] = board[item_y + 1][item_x + 1]# Down right
except:pass
# Open number
elif board[item_y][item_x] != " " and board[item_y][item_x] != "✶":
board_2[item_y][item_x] = board[item_y][item_x]
# Lose
elif board[item_y][item_x] == "✶":
# Count and show mines
iter = -1
for row in board:
iter += 1
index = -1
for item in row:
index += 1
if item == "✶":
if board_2[iter][index] == "►":
board_2[iter][index] = "▷"
else:
board_2[iter][index] = "✶"
board_2[item_y][item_x] = "X"
return board_2
return board_2
def mark_mine(board_2, item_y, item_x):
if board_2[item_y][item_x] == "-":
board_2[item_y][item_x] = "►"
elif board_2[item_y][item_x] == "►":
board_2[item_y][item_x] = "-"
return board_2
def check_win(board_2):
mine = False
hidden_spaces = 0
for row in board_2:
for item in row:
if item == "X":
mine = True
if item == "-":
hidden_spaces += 1
if mine == True:
return "lose"
if hidden_spaces == 0:
return "win"
else:
return None
def print_board(board):
"""Print board on the command line"""
y = 0
print("")
# Upper row
list_x = []
for i in range(len(board)):
if len(str(i + 1)) == 2:
list_x.append(str(i + 1))
else:
list_x.append(str(i + 1) + " ")
print(" " + " " + " ".join(list_x))
print("--" + "---" + "---".join(["-" for i in range(len(board))]) + "--")
for i in board:
# Left column
y += 1
y1 = f"{y} "
if len(str(y)) == 2:
y1 = f"{y}"
# Board row
print(y1 + " | " + " | ".join(i) + " |")
print("")
def player_input(board, board_2):
"""Player input on the command line"""
while True:
player_input = input("Write 'Y X' or 'Y X m' for mark mine (eg: '2 1', '2 1 m'): ")
input_item = player_input.split() # ["2", "1"]
if len(input_item) == 2 or len(input_item) == 3:
try:
input_item[0] = int(input_item[0]) - 1
input_item[1] = int(input_item[1]) - 1
if board[input_item[0]][input_item[1]]:
if board_2[input_item[0]][input_item[1]] == "-" or board_2[input_item[0]][input_item[1]] == "►":
return input_item
else:
print("Space already open")
except IndexError:
print("Incorrect space")
except ValueError:
print("Incorrect value")
else:
print("Incorrect value")
def play_command_line(board_size=10, mines=13):
"""Play Minesweeper on the command line
Board size: width and height of the board (rec ≈ 20)
Mines: number of mines"""
board = [[" " for _ in range(board_size)] for _ in range(board_size)]
board = add_mines_numbers(board, mines)
board_2 = [["-" for _ in range(board_size)] for _ in range(board_size)]
result = None
start = time.time()
while result == None:
board_2 = scan_empty_around_marks(board_2)
print_board(board_2)
input_item = player_input(board, board_2)
if len(input_item) == 3:
board_2 = mark_mine(board_2, input_item[0], input_item[1])
elif len(input_item) == 2 and board_2[input_item[0]][input_item[1]] != "►":
board_2 = scan_space(board, board_2, input_item[0], input_item[1])
result = check_win(board_2)
if result == "lose":
end = time.time()
print_board(board_2)
print(f"BOOM !!! Game over in {int(end - start)} seconds\n")
if result == "win":
end = time.time()
print_board(board_2)
print(f"You won in {int(end - start)} seconds\n")
def play_interface(board_size=10, mines=13):
"""Play Minesweeper with the interface
Board size: width and height of the board (rec ≈ 20)
Mines: number of mines"""
def game(board_size, mines):
def new_game():
result[0] = "lose"
button_state[0] = "disabled"
label_count["text"] = counter
label_mines["text"] = mines
button_new_game.destroy()
for b in buttons_for_destroying: # Destroy old buttons
b.destroy()
buttons_for_destroying.clear()
game(board_size, mines)
def game_counter():
if result[0] == None:
counter[0] += 1
label_count["text"] = str(counter[0])
label_count.after(1000, game_counter)
def reload_board(board_2):
# Count mark mines and show it on label
mines_mark = 0
for row in board_2:
for item in row:
if item == "►":
mines_mark += 1
label_mines["text"] = mines - mines_mark
# Create buttons
item_y = -1
for row in board_2:
item_y += 1
item_x = -1
for item in row:
item_x += 1
b = tk.Button(frame_down, text=item, width=3, height=1, font=("Helvetica", 14), state=button_state[0], relief="ridge", disabledforeground='black')
b.grid(row=item_y,column=item_x)
b["command"] = lambda y=item_y, x=item_x: click_on_board(board_2, y, x)
if item != "-":
b["bg"] = "white"
b["state"] = "disabled"
if item == "X":
b["bg"] = "red"
b["text"] = "✶"
elif item == "►":
b["state"] = "disabled"
b["disabledforeground"] = "red"
elif item == "▷":
b["text"] = "►"
b["state"] = "disabled"
elif item == "-":
b["text"] = " "
elif item == "1":
b["disabledforeground"] = "blue"
elif item == "2":
b["disabledforeground"] = "green"
elif item == "3":
b["disabledforeground"] = "red"
elif item == "4":
b["disabledforeground"] = "blue4"
elif item == "5":
b["disabledforeground"] = "brown"
elif item == "6":
b["disabledforeground"] = "brown4"
elif item == "7":
b["disabledforeground"] = "orange"
b.bind('<Button-3>', lambda event, y=item_y, x=item_x:right_click_on_board(board_2, y, x))
buttons_for_destroying.append(b)
return board_2
def click_on_board(board_2, y, x):
if board_2[y][x] != "►":
board_2 = scan_space(board, board_2, y, x)
result[0] = check_win(board_2)
if result[0] == "lose":
button_state[0] = "disabled"
label_count["text"] = counter
label_mines["text"] = mines
button_new_game["text"] = "☠"
if result[0] == "win":
button_state[0] = "disabled"
label_count["text"] = counter
label_mines["text"] = mines
button_new_game["text"] = "👍"
board_2 = scan_empty_around_marks(board_2)
for b in buttons_for_destroying: # Destroy old buttons
b.destroy()
buttons_for_destroying.clear()
board_2 = reload_board(board_2)
def right_click_on_board(board_2, y, x):
if result[0] == None:
board_2 = mark_mine(board_2, y, x)
for b in buttons_for_destroying: # Destroy old buttons
b.destroy()
buttons_for_destroying.clear()
board_2 = reload_board(board_2)
result[0] = check_win(board_2)
if result[0] == "win":
button_state[0] = "disabled"
label_count["text"] = counter
label_mines["text"] = mines
button_new_game["text"] = "👍"
# Start new game
button_new_game = tk.Button(frame_up, width=6, height=1, font=("Helvetica", 14), relief="ridge", foreground="black")
button_new_game.pack(side="left")
button_new_game["command"] = lambda: new_game()
button_new_game["text"] = "😁"
result = [None]
counter = [0]
game_counter()
board = [[" " for _ in range(board_size)] for _ in range(board_size)]
board = add_mines_numbers(board, mines)
board_2 = [["-" for _ in range(board_size)] for _ in range(board_size)]
button_state = ["normal"]
board_2 = scan_empty_around_marks(board_2)
buttons_for_destroying = []
board_2 = reload_board(board_2)
# Fix graphic on Win 10
if sys.platform == "win32" and platform.release() == "10":
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
# Windows settings
window = tk.Tk()
window.resizable(False, False)
window.title("Minesweeper")
window.iconphoto(False, tk.PhotoImage(file="data/minesweeper.png"))
frame_up = tk.Frame(window, border=3)
frame_up.pack()
frame_down = tk.Frame(window, border=3)
frame_down.pack()
label_mines = tk.Button(frame_up, text=mines, width=6, height=1, font=("Helvetica", 14), relief="ridge", state="disabled", disabledforeground='red', bg="black")
label_mines.pack(side="left", padx=40)
label_count = tk.Button(frame_up, width=6, height=1, font=("Helvetica", 14), relief="ridge", state="disabled", disabledforeground='red', bg="black")
label_count.pack(side="right", padx=40)
game(board_size, mines)
window.mainloop()
if __name__ == "__main__":
# Game options: play_command_line(board_size, mines), play_interface(board_size, mines)
play_interface()