-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy.py
312 lines (285 loc) · 9.92 KB
/
easy.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
from Tkinter import *
import tkMessageBox
import random
#This is the main class
class 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=0,columnspan=3)
self.Restart_button=Button(self.frame,text="restart",command=self.Res1)
self.Restart_button.grid(row=0,column=3,columnspan=3)
self.Quit_button=Button(self.frame,text="Quit >",command=self.Quit1) #Displays Quit button.
self.Quit_button.grid(row=0,column=6,columnspan=3)
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(81):
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.lclick1(x))
self.buttons[x][0].bind("<Button-3>",self.rclick1(x))
y_cord=y_cord+1;
if(y_cord==9):
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 #Different for boundary tiles as no.of tiles surrounding it are different.
if(keys==0):
if (self.check_mines(keys+1)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys+9)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys+10)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(keys==8):
if (self.check_mines(keys-1)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys+9)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys+8)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(keys==72):
if (self.check_mines(keys-9)):
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-8)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(keys==80):
if (self.check_mines(keys-1)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys-9)):
self.near_mines=self.near_mines+1
if (self.check_mines(keys-8)):
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+10)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+9)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+8)):
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-9)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-8)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+9)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+10)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(x_crd==9):
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-10)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-9)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-8)):
self.near_mines=self.near_mines+1
self.buttons[keys][5]=self.near_mines
elif(y_crd==8):
if(self.check_mines(keys-1)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+9)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+8)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-9)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-10)):
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+8)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+9)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys+10)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-10)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-9)):
self.near_mines=self.near_mines+1
if(self.check_mines(keys-8)):
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=10,column=0,columnspan=5)
self.label3=Label(self.frame,text="Flags: "+str(self.flags))
self.label3.grid(row=10,column=5,columnspan=5)
def Res1(self):
self.Restart1()
#after completion of one game, we use this function to forget the previous game and show the new game.
def Quit1(self):
self.Quit()
def Back(self):
self.frame.grid_forget()
self.__init__(self)
#after completion of one game, we use this function to forget the previous game and show the new game.
def forgetframe(self):
self.frame.grid_forget()
def check_mines(self,keys):
try:
if (self.buttons[keys][1]==1):
return True
else:
return False
except KeyError:
pass
def lclick1(self,keys):
return lambda Button: self.l_click1(self.buttons[keys])
def rclick1(self,keys):
return lambda Button: self.r_click1(self.buttons[keys])
#open the tile if we left click on it.
def l_click1(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(1)
else:
if (button[5]==0):
button[0].configure(image=self.tile_clicked)
self.cln_emp_tile1(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==81-self.mines):
self.win(1)
#plants flag if once right clicked and removes it if clicked once again.
def r_click1(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.lclick1(button[3]))
self.flags=self.flags-1
self.update_flag()
#This function opens all the empty tiles near a tile.
def cln_emp_tile1(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+9,button_list)
self.check(keys+10,button_list)
elif(keys==8):
self.check(keys-1,button_list)
self.check(keys+9,button_list)
self.check(keys+8,button_list)
elif(keys==72):
self.check(keys-9,button_list)
self.check(keys-8,button_list)
self.check(keys+1,button_list)
elif(keys==80):
self.check(keys-10,button_list)
self.check(keys-9,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+10,button_list)
self.check(keys+9,button_list)
self.check(keys+8,button_list)
elif(y_crd==0):
self.check(keys-9,button_list)
self.check(keys-8,button_list)
self.check(keys+1,button_list)
self.check(keys+9,button_list)
self.check(keys+10,button_list)
elif(x_crd==9):
self.check(keys-10,button_list)
self.check(keys-9,button_list)
self.check(keys-8,button_list)
self.check(keys-1,button_list)
self.check(keys+1,button_list)
elif(y_crd==8):
self.check(keys-10,button_list)
self.check(keys-9,button_list)
self.check(keys-1,button_list)
self.check(keys+8,button_list)
self.check(keys+9,button_list)
else:
self.check(keys-8,button_list)
self.check(keys-9,button_list)
self.check(keys-10,button_list)
self.check(keys-1,button_list)
self.check(keys+1,button_list)
self.check(keys+10,button_list)
self.check(keys+9,button_list)
self.check(keys+8,button_list)
def check(self,keys,button_list):
try:
if(self.buttons[keys][2]==0):
if(self.buttons[keys][5]==0):
self.buttons[keys][0].configure(image=self.tile_clicked)
button_list.append(keys)
else:
self.buttons[keys][0].configure(image=self.tile_no[self.buttons[keys][5]-1])
self.buttons[keys][2]=1
self.clicked=self.clicked+1
except KeyError:
pass