-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper_ui.py
executable file
·319 lines (255 loc) · 7.63 KB
/
minesweeper_ui.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
#!/usr/bin/python3
from minesweeper import *
import minesweeper
from tkinter import *
#from enum import Enum
#import random
import threading
import queue
#class GameThread(threading.Thread, Player):
class GameThread(Player):
def __init__(self):
#threading.Thread.__init__(self, args=a, kwargs=k)
super().__init__()
#self.game = game
self.app = None
self.queue = queue.Queue()
self.lock = threading.Lock()
#self.lock1 = threading.Lock()
#self.daemon = False # i dont know what im doing
#def lose(self, e):
# self.app.lose()
#def win(self, e):
# self.app.win()
def _move(self, grid):
#with self.lock:
self.app.update_grid(grid)
val = self.queue.get()
with self.lock:
return val
#def run(self):
def send_move(self, move):
with self.lock:
self.queue.put(move)
#return self.grid
#size = 8
bd = 2
# #RGB
color = "#BFBFBF"
# irrelavent
#fg = "#BFBFBF"
#bg = "#BFBFBF" # BFBFBF # color of button
#afg = "#BFBFBF"
#abg = "#BFBFBF" # color when clicked on
#hbg = "#BFBFBF"
#hclr = "#BFBFBF"
# end irrelavent
#pad = 0
#ipad = 1
t = Tk()
#default_image = PhotoImage()
class Images(object):
blank = PhotoImage()
i_1 = PhotoImage(file="icons/1.png")
i_2 = PhotoImage(file="icons/2.png")
i_3 = PhotoImage(file="icons/3.png")
#i_4 = PhotoImage(file="icons/4.png")
#i_5 = PhotoImage(file="icons/5.png")
#i_6 = PhotoImage(file="icons/6.png")
#i_7 = PhotoImage(file="icons/7.png")
#i_8 = PhotoImage(file="icons/8.png")
flag = PhotoImage(file="icons/flag.png")
mine = PhotoImage(file="icons/mine.png")
#image_1 = PhotoImage(file="1.png")
#image_1 = Images.i_1
#image_1 = PhotoImage(file="icons/1.png")
#image_1 = PhotoImage(Images.i_1)
'''
def l_press(x, y):
print("Left: (%s, %s)" % (str(x), str(y)))
#self.buttons[x][y].config(state=DISABLED, image=image_1)
def r_press(x, y):
print("Right: (%s, %s)" % (str(x), str(y)))
'''
class Tile(Button):
#IMAGE_1 = PhotoImage(file="1.png")
size = 6 # Find size, bd that makes reasonable final_size
bd = 3 # for every +1 bd, do -1 size for same width, height
final_size = 16 # 16 is closest to xp minesweeper
color = "#BFBFBF" # color of button
grid_color = "#888" # color of 1 px grid line
def __init__(self, master, x, y, app=None):
#'''
self.frame = Frame(master, bd=0, relief=FLAT, \
bg=self.grid_color, \
width=self.final_size, height=self.final_size)
# Button size in px is 2*self.size
# Make Frame same size as Button
# bg = color of 1 px grid line
# bd, relief so Frame is invisible
self.frame.grid(row=x, column=y, padx=0, pady=0, ipadx=0, \
ipady=0, sticky=N+S+E+W)
# Sticky so fill all space, pad so invisible
self.frame.grid_propagate(False) # Keep width, height size'''
#self.frame = master
super().__init__(self.frame, image=Images.blank, \
compound=CENTER, width=self.size, \
height=self.size, bd=self.bd, relief=RAISED, \
bg=self.color, activebackground=self.color, \
activeforeground=self.color, takefocus=False)
# image, compound so small size
# Images.blank is blank
self.grid(row=x, column=y, padx=0, pady=0, ipadx=1, ipady=1, \
sticky=E+S)
self.x, self.y = x, y
if app is None:
app = master
self.app = app
self.state = False
self.open = False
self.click = False
self.register_events()
def generate_callback(self):
def callback(event):
#print('(' + str(x) + ', ' + str(y) + ') ' + str(type(event.type)))
if self.open:
return 'break'
if event.type == EventType.Leave:
self.config(bd=self.bd, width=self.size, height=self.size)
self.state = False
return
if event.type == EventType.Enter and self.click:
self.config(bd=0, width=self.final_size-1, height=self.final_size-1) # bd=0 so same as relief=FLAT, final_size-1 so 1 px line
self.state = True
return
if (event.type == EventType.ButtonPress) and (event.num == 1):
self.config(bd=0, relief=FLAT, width=self.final_size-1, height=self.final_size-1) # Same as above Enter
self.click = True
self.state = True
return
if event.type == EventType.ButtonRelease and event.num == 1 and self.state:
self.config(bd=self.bd, relief=FLAT, width=self.size-1, height=self.size-1)
self.state = False
self.click = False
self.open = True
self.app.l_press(self.x, self.y)
return
if event.type == EventType.ButtonRelease and event.num == 1:
self.config(bd=self.bd, width=self.size, height=self.size)
self.state = False
self.click = False
return
if (event.type == EventType.ButtonPress) and (event.num == 3):
self.state = False
self.app.r_press(self.x, self.y)
return
return callback
def register_events(self):
callback = self.generate_callback()
for x in ["<ButtonPress-1>", "<ButtonRelease-1>", "<Leave>", "<ButtonPress-3>", "<Enter>"]:
self.bind(x, callback)
class App(Frame):
def __init__(self, tk, player, grid):
super().__init__(tk, bg=color) # "#888"
self.x_size, self.y_size = 9, 9
self.player = player
self._grid = grid
self.load_images()
self.make_widgets()
self.player.app = self
#def move(self, grid):
# assert grid.x_size == 9
# assert grid.y_size == 9
#
# self.grid = grid
def redraw(self):
class Event(object):
type = EventType.ButtonRelease
num = 1
for x in range(self.x_size):
for y in range(self.y_size):
self.buttons[x][y]["image"] = self.get_image(x, y)
if self._grid.grid(x, y).visible == Visibility.open:
self.buttons[x][y].state = True
self.buttons[x][y].generate_callback()(Event())
def load_images(self):
pass
def make_widgets(self):
self.make_button_grid()
def get_image(self, x, y):
# This is where you get appropriate image from Game, or grid, or whatever
cell = self._grid.grid(x, y)
if cell.visible == Visibility.hidden:
return Images.blank
if cell.visible == Visibility.flag:
return Images.flag
m, n = cell.is_mine, cell.neighbors
if m:
return Images.mine
if n == 0:
return Images.blank
if n == 1:
return Images.i_1
if n == 2:
return Images.i_2
if n == 3:
return Images.i_3
return Images.flag
def update_grid(self, grid):
self._grid = grid
self.redraw()
def l_press(self, x, y):
print("Left: (%s, %s)" % (str(x), str(y)))
#self.redraw()
#image = self.get_image(x, y)
#self.buttons[x][y].config(image=image)
#self.buttons[x][y].grid(ipadx=1, ipady=1)
self.player.send_move([MoveType.open, x, y])
def r_press(self, x, y):
print("Right: (%s, %s)" % (str(x), str(y)))
self.player.send_move([MoveType.flag, x, y])
def make_button_grid(self, x_size=9, y_size=9, x_offset=None, y_offset=None):
x_size, y_size = int(x_size), int(y_size)
if (x_offset is None):
x_offset = 0
x_offset = int(x_offset)
if x_offset < 0:
x_offset = 0
if y_offset is None:
y_offset = x_offset
y_offset = int(y_offset)
if y_offset < 0:
y_offset = 0
buttons = []
for x in range(x_size):
row = []
for y in range(y_size):
b = self.make_button(x + x_offset, y + y_offset)
row.append(b)
buttons.append(row)
self.buttons = buttons
def make_button(self, x, y):
return Tile(self, x, y)
class Game2(threading.Thread):
def __init__(self, game):
self.game = game
super().__init__()
def run(self):
self.game.mainloop()
def main():
#tk = Tk()
#image = PhotoImage()
grid_generator = RandomGridGenerator(9, 9, 10)
print(grid_generator)
grid = grid_generator.next()
print(grid)
grid = minesweeper.Grid(grid)
print(grid)
player = GameThread()
game = Game(grid, player)
app = App(t, player, grid)
app.grid()
Game2(game).start()
t.mainloop()
if __name__ == "__main__":
main()