-
Notifications
You must be signed in to change notification settings - Fork 0
/
hard.py
287 lines (261 loc) · 9.07 KB
/
hard.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
from easy import Easy
from Tkinter import *
import tkMessageBox
import random
class Hard(Easy):
def __init__(self,master):
#Takes image files from the folder
self.tile_plain=PhotoImage(file="tile_plain.gif")
self.tile_clicked=PhotoImage(file="tile_clicked.gif")
self.tile_mine=PhotoImage(file="tile_mine.gif")
self.tile_flag=PhotoImage(file="tile_flag.gif")
self.tile_wrong=PhotoImage(file="tile_wrong.gif")
self.tile_no=[]
for x in range(1,9):
self.tile_no.append(PhotoImage(file="tile_"+str(x)+".gif"))
#creates a frame
self.frame=Frame(master)
self.frame.grid()
self.Back_button=Button(self.frame,text="< Back",command=self.Back)
self.Back_button.grid(row=0,column=2,columnspan=4)
self.Restart_button=Button(self.frame,text="restart",command=self.Res2)
self.Restart_button.grid(row=0,column=6,columnspan=4)
self.Quit_button=Button(self.frame,text="Quit >",command=self.Quit2) #Displays Quit button.
self.Quit_button.grid(row=0,column=10,columnspan=4)
self.flags=0
self.correct_flags=0
self.clicked=0
self.buttons={}
self.mines=0
x_cord=1
y_cord=0
for x in range(256):
mine=0
image_p=self.tile_plain
if (random.uniform(0,1)<0.15): #computer chooses no.of mines randomly
mine=1
self.mines=self.mines+1
#0:button
#1:wether mine present(1:present,0:not present)
#2:state(0:unclicked,1:clicked;2:flagged)
#3:button number
#4:coordinates
#5:nearby mines
self.buttons[x]=[Button(self.frame,image=image_p),mine,0,x,[x_cord,y_cord],0]
self.buttons[x][0].bind("<Button-1>",self.lclick2(x))
self.buttons[x][0].bind("<Button-3>",self.rclick2(x))
y_cord=y_cord+1;
if(y_cord==16):
y_cord=0
x_cord=x_cord+1
for keys in self.buttons:
self.buttons[keys][0].grid(row=self.buttons[keys][4][0],column=self.buttons[keys][4][1])
#checks no.of mines near a tile
for keys in self.buttons:
x_crd=self.buttons[keys][4][0]
y_crd=self.buttons[keys][4][1]
self.near_mines=0
if(keys==0): #Different for boundary tiles as no.of tiles surrounding it are different.
if (self.check_mines(keys+1)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys+16)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys+17)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(keys==15):
if (self.check_mines(keys-1)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys+15)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys+16)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(keys==240):
if (self.check_mines(keys-16)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys+1)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys-15)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(keys==255):
if (self.check_mines(keys-1)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys-16)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys-17)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(x_crd==1):
if(self.check_mines(keys-1)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+1)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+15)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+16)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+17)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(y_crd==0):
if(self.check_mines(keys+1)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-16)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-15)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+16)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+17)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(x_crd==16):
if(self.check_mines(keys+1)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-1)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-15)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-16)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-17)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(y_crd==15):
if(self.check_mines(keys-1)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+16)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+15)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-16)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-17)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
else:
if(self.check_mines(keys-1)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+1)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+15)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+16)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+17)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-15)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-16)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-17)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
#creates two labels which shows the the no.of mines in the game and no.of flags you can still plant.
self.label2=Label(self.frame,text="Mines: "+str(self.mines))
self.label2.grid(row=17,column=0,columnspan=8)
self.label3=Label(self.frame,text="Flags: "+str(self.flags))
self.label3.grid(row=17,column=8,columnspan=8)
def Quit2(self):
self.Quit()
def Back(self):
self.frame.grid_forget()
self.__init__(self)
def Res2(self):
self.Restart2()
def lclick2(self,keys):
return lambda Button: self.l_click2(self.buttons[keys])
def rclick2(self,keys):
return lambda Button: self.r_click2(self.buttons[keys])
#open the tile if we left click on it.
def l_click2(self,button):
if(button[1]==1):
for keys in self.buttons:
if (self.buttons[keys][1]!=1 and self.buttons[keys][2]==2):
self.buttons[keys][0].configure(image=self.tile_wrong)
if (self.buttons[keys][1]==1 and self.buttons[keys][2]!=2):
self.buttons[keys][0].configure(image=self.tile_mine)
self.end(2)
else:
if (button[5]==0):
button[0].configure(image=self.tile_clicked)
self.cln_emp_tile2(button[3])
else:
button[0].configure(image=self.tile_no[button[5]-1])
if (button[2]!=1):
button[2]=1
self.clicked=self.clicked+1
if (self.clicked==256-self.mines):
self.win(2)
#plants flag if once right clicked and removes it if clicked once again.
def r_click2(self,button):
if (button[2]==0):
button[0].configure(image=self.tile_flag)
button[2]=2
button[0].unbind("<Button-1>")
self.flags=self.flags+1
self.update_flag()
elif (button[2]==2):
button[0].configure(image=self.tile_plain)
button[2]=0
button[0].bind("<Button-1>",self.lclick2(button[3]))
self.flags=self.flags-1
self.update_flag()
#This function opens all the empty tiles near a tile.
def cln_emp_tile2(self,button_num):
button_list=[button_num]
while (len(button_list)!=0):
keys=button_list.pop()
x_crd=self.buttons[keys][4][0]
y_crd=self.buttons[keys][4][1]
if (keys==0):
self.check(keys+1,button_list)
self.check(keys+16,button_list)
self.check(keys+17,button_list)
elif(keys==15):
self.check(keys-1,button_list)
self.check(keys+15,button_list)
self.check(keys+16,button_list)
elif(keys==240):
self.check(keys-16,button_list)
self.check(keys-15,button_list)
self.check(keys+1,button_list)
elif(keys==255):
self.check(keys-17,button_list)
self.check(keys-16,button_list)
self.check(keys-1,button_list)
elif(x_crd==1):
self.check(keys-1,button_list)
self.check(keys+1,button_list)
self.check(keys+15,button_list)
self.check(keys+16,button_list)
self.check(keys+17,button_list)
elif(y_crd==0):
self.check(keys-16,button_list)
self.check(keys-15,button_list)
self.check(keys+1,button_list)
self.check(keys+16,button_list)
self.check(keys+17,button_list)
elif(x_crd==16):
self.check(keys-15,button_list)
self.check(keys-16,button_list)
self.check(keys-17,button_list)
self.check(keys-1,button_list)
self.check(keys+1,button_list)
elif(y_crd==15):
self.check(keys-16,button_list)
self.check(keys-17,button_list)
self.check(keys-1,button_list)
self.check(keys+16,button_list)
self.check(keys+15,button_list)
else:
self.check(keys-15,button_list)
self.check(keys-16,button_list)
self.check(keys-17,button_list)
self.check(keys-1,button_list)
self.check(keys+1,button_list)
self.check(keys+15,button_list)
self.check(keys+16,button_list)
self.check(keys+17,button_list)