This repository was archived by the owner on Jul 1, 2018. It is now read-only.
forked from ripexz/python-tkinter-minesweeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.py
318 lines (274 loc) · 10.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
# Python Version 2.7.3
# File: minesweeper.py
import Tkinter as tk
import tkMessageBox
import random
import com
from Tkinter import N, S, E, W
from view import TileFrame, BoardFrame
from model import Tile
from collections import deque
class Minesweeper:
ROWS = 10
COLS = 10
BOMBS = 10
def __init__(self, master):
# set up frame
frame = tk.Frame(master)
frame.pack(fill=tk.BOTH, expand=True)
self.data = list()
self.game_ended = False
self.clicks = 0
self.board = BoardFrame(frame)
self.board.grid(row=0, column=0, sticky=N+S+E+W)
self.board.set_board((Minesweeper.ROWS, Minesweeper.COLS))
self.board.grid_propagate(True)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_propagate(True)
self.set_board()
self.place_bombs()
for index in range(Minesweeper.ROWS*Minesweeper.COLS):
self.board.get_view_at(index).canvas.bind('<Button-1>', self.lclicked_wrapper(index))
self.board.get_view_at(index).canvas.bind('<Button-3>', self.rclicked_wrapper(index))
self.update()
def set_board(self):
# Initialize board with empty tiles
for row in range(Minesweeper.ROWS):
row_list = list()
for column in range(Minesweeper.COLS):
row_list.append(Tile(row*Minesweeper.COLS+column))
self.data.append(row_list)
def get_covered_tiles(self):
data = reduce(lambda x,y: x+y, self.data)
covered_tiles = filter(lambda tile: tile.state == 0, data)
return covered_tiles
def place_bombs(self):
mines = 0
# Looping while we haven't placed anough bombs.
while mines < Minesweeper.BOMBS:
while True:
# Chosing a random place to place a bomb at.
bomb_index = random.randint(0, Minesweeper.COLS * Minesweeper.ROWS - 1)
tile = self.tile_at_index(bomb_index)
# If the chosen random place is not already a bomb, place a bomb there
if not tile.is_mine:
tile.is_mine = True
mines += 1
# Setting bomb count for tiles around the placed bomb
pos = (bomb_index/Minesweeper.COLS, bomb_index%Minesweeper.COLS)
for row in range(max(pos[0]-1, 0), pos[0]+2):
for col in range(max(pos[1]-1, 0), pos[1]+2):
try:
tile = self.data[row][col]
tile.nearby_mines += 1
except IndexError:
continue
break
def amount_open_around(self, index):
state = self.get_state(index, 1)
state = reduce(lambda x,y: x+y, state)
open_tiles_around = filter(lambda tile_value: tile_value >= 0 or tile_value == -2, state)
return len(open_tiles_around)
def tile_at_index(self, index):
return self.data[index/Minesweeper.COLS][index%Minesweeper.COLS]
def check_for_mines(self, key):
try:
if self.tile_at_index(key).is_mine:
return True
except KeyError:
pass
def update(self):
for row in range(Minesweeper.ROWS):
for col in range(Minesweeper.COLS):
tile_view = self.board.get_view_at((row, col))
tile_view.set_tile(self.data[row][col])
global root
root.update()
root.update_idletasks()
def lclicked_wrapper(self, index):
return lambda x: self.lclicked(index)
def rclicked_wrapper(self, index):
return lambda x: self.rclicked(index)
def lclicked(self, x):
self.clicks += 1
tile = self.tile_at_index(x)
tile_view = self.board.get_view_at(x)
if tile.is_mine: #if a mine
self.print_state(x, False)
tile.exploded = True
# end game
self.gameover(False)
return
else:
self.print_state(x, True)
#change image
if tile.nearby_mines == 0:
pos = (x/Minesweeper.COLS, x%Minesweeper.COLS)
self.clear_empty_tiles(pos)
# if not already set as clicked, change state and count
if tile.state != 1:
tile.state = 1
self.update()
def rclicked(self, x):
self.clicks += 1
tile = self.tile_at_index(x)
tile_view = self.board.get_view_at(x)
# if not clicked
if tile.state == 0:
if tile.is_mine:
#print "Correct flag"
self.print_state(x, False)
else:
# No mine, should left click there
self.print_state(x, True)
tile.state = 2
tile_view.unbind('<Button-1>')
# if flagged, unflag
elif tile.state == 2:
tile.state = 0
tile_view.bind('<Button-1>', self.lclicked_wrapper(x))
self.update()
def get_state(self, x, radius=2):
tile = self.tile_at_index(x)
# This function #prints the state of the board surrounding a given cell index
row = x/10
col = x%10
row_range = range(row-radius, row+radius+1)
col_range = range(col-radius,col+radius+1)
# If cell is uncovered
return [[self.get_tile_number(row, col) for col in col_range] for row in row_range]
def print_state(self, x, good_answer):
tile = self.tile_at_index(x)
if tile.state == 0:
data = self.get_state(x)
data_file = open('input_data.txt', 'a+')
data_file.write(str([data, int(good_answer)])+"\n")
data_file.close()
# for row in data:
# #print row
# #print "Should click there?", good_answer
def is_win(self):
for row in range(Minesweeper.ROWS):
for col in range(Minesweeper.COLS):
if self.data[row][col].state == 2 and not self.data[row][col].is_mine:
return False
return True
def get_tile_number(self, row, column):
try:
if row < 0 or column < 0:
raise IndexError
tile = self.data[row][column]
# tile is flagged, ignore mistakes
if tile.state == 2 and tile.is_mine:
return -2
elif tile.state == 0:
return -1
else:
return tile.nearby_mines
except IndexError:
return -3
def check_tile(self, pos, queue):
try:
tile = self.data[pos[0]][pos[1]]
tile_view = self.board.get_view_at(pos)
if tile.state == 0:
if tile.nearby_mines == 0:
queue.append(pos)
tile.state = 1
except IndexError:
return
def clear_empty_tiles(self, pos):
queue = deque([pos])
while len(queue) != 0:
pos = queue.popleft()
for row in range(max(pos[0]-1, 0), pos[0]+2):
for col in range(max(pos[1]-1, 0), pos[1]+2):
if row == pos[0] and col == pos[1]:
continue
self.check_tile((row, col), queue)
def ignore_interaction(self):
for row in range(Minesweeper.ROWS):
for col in range(Minesweeper.COLS):
self.board.get_view_at((row, col)).canvas.unbind('<Button-3>')
self.board.get_view_at((row, col)).canvas.unbind('<Button-1>')
def gameover(self, iswin):
global root
# print self.clicks
self.ignore_interaction()
if not iswin:
for row in range(Minesweeper.ROWS):
for col in range(Minesweeper.COLS):
tile = self.data[row][col]
if tile.state == 0:
# Uncover bomb
tile.state = 1
self.update()
if iswin:
tkMessageBox.showinfo("Game ended.", "You Won!")
self.game_ended = True
root.destroy()
class Solver:
def __init__(self, minesweeper, addr = "127.0.0.1", port = 8080):
self.minesweeper = minesweeper
self.open_tiles = list()
self.communicator = com.Communicator(addr, port)
def is_flag_percent(self, tile):
# Getting data for chosen index.
data = self.minesweeper.get_state(tile.index)
data = reduce(lambda x,y: x+y, data)
# Getting the result, should we click there or no?
return self.communicator.get_result(data)
def solve(self):
global root
if self.minesweeper.game_ended:
return
self.open_tiles = self.minesweeper.get_covered_tiles()
# If stuck, start a new game
if len(self.open_tiles) == 0:
if self.minesweeper.is_win():
self.minesweeper.gameover(True)
else:
self.minesweeper.gameover(False)
return
chances = map(self.is_flag_percent, self.open_tiles)
max_index = chances.index(max(chances))
(max_tile, max_percent) = self.open_tiles[max_index], chances[max_index]
min_index = chances.index(min(chances))
(min_tile, min_percent) = self.open_tiles[min_index], chances[min_index]
played = False
if min_percent < 0.08 or Minesweeper.COLS * Minesweeper.ROWS - len(self.open_tiles) < 10:
self.minesweeper.lclicked(min_tile.index)
played = True
if max_percent > 0.97:
self.minesweeper.rclicked(max_tile.index)
played = True
if not played:
if abs(0.5-max_percent) > abs(0.5-min_percent):
self.minesweeper.rclicked(max_tile.index)
else:
self.minesweeper.lclicked(min_tile.index)
root.after(1, self.solve)
### END OF CLASSES ###
def main():
while True:
global root
try:
# create Tk widget
root = tk.Tk()
# set program title
root.title("Minesweeper")
# create game instance
minesweeper = Minesweeper(root)
root.geometry('500x500+200+200')
Tile.MINE_IMAGE = tk.PhotoImage(file = "images/mine.gif")
Tile.FLAG_IMAGE = tk.PhotoImage(file = "images/flag.gif")
solver = Solver(minesweeper, "127.0.0.1")
root.after(0, solver.solve)
# run event loop
root.mainloop()
except KeyboardInterrupt:
print "Shutting down."
break
if __name__ == "__main__":
main()