@@ -46,68 +46,56 @@ def __init__(self, master):
4646 # create buttons
4747 self .buttons = dict ({})
4848 self .mines = 0
49- x_coord = 1
50- y_coord = 0
51- for x in range (0 , 100 ):
52- isMine = 0
53-
54- # tile image changeable for debug reasons:
55- gfx = self .tiles ["plain" ]
56-
57- # currently random amount of mines
58- if random .uniform (0.0 , 1.0 ) < 0.1 :
59- isMine = 1
60- self .mines += 1
61-
62- self .buttons [x ] = {
63- "id" : x ,
64- "isMine" : isMine ,
65- "state" : STATE_DEFAULT ,
66- "coords" : {
67- "x" : x_coord ,
68- "y" : y_coord
69- },
70- "widget" : Button (frame , image = gfx ),
71- "mines" : 0 # calculated after placement in grid
72- }
73-
74- self .buttons [x ]["widget" ].bind (BTN_CLICK , self .lclicked_wrapper (x ))
75- self .buttons [x ]["widget" ].bind (BTN_FLAG , self .rclicked_wrapper (x ))
76-
77- # calculate coords for next loop:
78- y_coord += 1
79- if y_coord == 10 :
80- y_coord = 0
81- x_coord += 1
82-
83- # lay buttons in grid
84- for key in self .buttons :
85- self .buttons [key ]["widget" ].grid ( row = self .buttons [key ]["coords" ]["x" ], column = self .buttons [key ]["coords" ]["y" ] )
86-
87- # find nearby mines and display number on tile
88- for key in self .buttons :
89- nearby_mines = 0
90- if self .check_for_mines (key - 9 ):
91- nearby_mines += 1
92- if self .check_for_mines (key - 10 ):
93- nearby_mines += 1
94- if self .check_for_mines (key - 11 ):
95- nearby_mines += 1
96- if self .check_for_mines (key - 1 ):
97- nearby_mines += 1
98- if self .check_for_mines (key + 1 ):
99- nearby_mines += 1
100- if self .check_for_mines (key + 9 ):
101- nearby_mines += 1
102- if self .check_for_mines (key + 10 ):
103- nearby_mines += 1
104- if self .check_for_mines (key + 11 ):
105- nearby_mines += 1
106- # store mine count in button data list
107- self .buttons [key ]["mines" ] = nearby_mines
108- #if self.buttons[key]["isMine"] != 1:
109- # if nearby_mines != 0:
110- # self.buttons[key]["widget"].config(image = self.tiles["numbers"][nearby_mines-1])
49+ for x in range (0 , 10 ):
50+ for y in range (0 , 10 ):
51+ if y == 0 :
52+ self .buttons [x ] = {}
53+
54+ id = str (x ) + "_" + str (y )
55+ isMine = False
56+
57+ # tile image changeable for debug reasons:
58+ gfx = self .tiles ["plain" ]
59+
60+ # currently random amount of mines
61+ if random .uniform (0.0 , 1.0 ) < 0.1 :
62+ isMine = True
63+ self .mines += 1
64+
65+ self .buttons [x ][y ] = {
66+ "id" : id ,
67+ "isMine" : isMine ,
68+ "state" : STATE_DEFAULT ,
69+ "coords" : {
70+ "x" : x ,
71+ "y" : y
72+ },
73+ "widget" : Button (frame , image = gfx ),
74+ "mines" : 0 # calculated after placement in grid
75+ }
76+
77+ self .buttons [x ][y ]["widget" ].bind (BTN_CLICK , self .lclicked_wrapper (x , y ))
78+ self .buttons [x ][y ]["widget" ].bind (BTN_FLAG , self .rclicked_wrapper (x , y ))
79+
80+ # lay buttons in grid
81+ self .buttons [x ][y ]["widget" ].grid ( row = x , column = y )
82+
83+ # loop again to find nearby mines and display number on tile
84+ for x in range (0 , 10 ):
85+ for y in range (0 , 10 ):
86+ nearby_mines = 0
87+ nearby_mines += 1 if self .check_for_mines (x - 1 , y - 1 ) else 0
88+ nearby_mines += 1 if self .check_for_mines (x - 1 , y ) else 0
89+ nearby_mines += 1 if self .check_for_mines (x - 1 , y + 1 ) else 0
90+
91+ nearby_mines += 1 if self .check_for_mines (x , y - 1 ) else 0
92+ nearby_mines += 1 if self .check_for_mines (x , y + 1 ) else 0
93+
94+ nearby_mines += 1 if self .check_for_mines (x + 1 , y - 1 ) else 0
95+ nearby_mines += 1 if self .check_for_mines (x + 1 , y ) else 0
96+ nearby_mines += 1 if self .check_for_mines (x + 1 , y + 1 ) else 0
97+
98+ self .buttons [x ][y ]["mines" ] = nearby_mines
11199
112100 #add mine and count at the end
113101 self .label2 = Label (frame , text = "Mines: " + str (self .mines ))
@@ -118,27 +106,27 @@ def __init__(self, master):
118106
119107 ## End of __init__
120108
121- def check_for_mines (self , key ):
109+ def check_for_mines (self , x , y ):
122110 try :
123- if self .buttons [key ]["isMine" ] == 1 :
124- return True
111+ return self .buttons [x ][y ]["isMine" ]
125112 except KeyError :
126113 pass
127114
128- def lclicked_wrapper (self , x ):
129- return lambda Button : self .lclicked (self .buttons [x ])
115+ def lclicked_wrapper (self , x , y ):
116+ return lambda Button : self .lclicked (self .buttons [x ][ y ] )
130117
131- def rclicked_wrapper (self , x ):
132- return lambda Button : self .rclicked (self .buttons [x ])
118+ def rclicked_wrapper (self , x , y ):
119+ return lambda Button : self .rclicked (self .buttons [x ][ y ] )
133120
134121 def lclicked (self , button_data ):
135- if button_data ["isMine" ] == 1 :
122+ if button_data ["isMine" ] == True :
136123 # show all mines and check for flags
137- for key in self .buttons :
138- if self .buttons [key ]["isMine" ] == 0 and self .buttons [key ]["state" ] == STATE_FLAGGED :
139- self .buttons [key ]["widget" ].config (image = self .tiles ["wrong" ])
140- if self .buttons [key ]["isMine" ] == 1 and self .buttons [key ]["state" ] != STATE_FLAGGED :
141- self .buttons [key ]["widget" ].config (image = self .tiles ["mine" ])
124+ for x in range (0 , 10 ):
125+ for y in range (0 , 10 ):
126+ if self .buttons [x ][y ]["isMine" ] == False and self .buttons [x ][y ]["state" ] == STATE_FLAGGED :
127+ self .buttons [x ][y ]["widget" ].config (image = self .tiles ["wrong" ])
128+ if self .buttons [x ][y ]["isMine" ] == True and self .buttons [x ][y ]["state" ] != STATE_FLAGGED :
129+ self .buttons [x ][y ]["widget" ].config (image = self .tiles ["mine" ])
142130 # end game
143131 self .gameover ()
144132 else :
@@ -162,47 +150,51 @@ def rclicked(self, button_data):
162150 button_data ["state" ] = STATE_FLAGGED
163151 button_data ["widget" ].unbind (BTN_CLICK )
164152 # if a mine
165- if button_data ["isMine" ] == 1 :
153+ if button_data ["isMine" ] == True :
166154 self .correct_flags += 1
167155 self .flags += 1
168156 self .update_flags ()
169157 # if flagged, unflag
170158 elif button_data ["state" ] == 2 :
171159 button_data ["widget" ].config (image = self .tiles ["plain" ])
172160 button_data ["state" ] = 0
173- button_data ["widget" ].bind (BTN_CLICK , self .lclicked_wrapper (button_data ["id" ]))
161+ button_data ["widget" ].bind (BTN_CLICK , self .lclicked_wrapper (button_data ["coords" ][ x ], button_data [ "coords" ][ y ]))
174162 # if a mine
175- if button_data ["isMine" ] == 1 :
163+ if button_data ["isMine" ] == True :
176164 self .correct_flags -= 1
177165 self .flags -= 1
178166 self .update_flags ()
179167
180- def check_tile (self , key , queue ):
168+ def check_tile (self , x , y , queue ):
181169 try :
182- if self .buttons [key ]["state" ] == STATE_DEFAULT :
183- if self .buttons [key ]["mines" ] == 0 :
184- self .buttons [key ]["widget" ].config (image = self .tiles ["clicked" ])
185- queue .append (key )
170+ if self .buttons [x ][ y ]["state" ] == STATE_DEFAULT :
171+ if self .buttons [x ][ y ]["mines" ] == 0 :
172+ self .buttons [x ][ y ]["widget" ].config (image = self .tiles ["clicked" ])
173+ queue .append (self . buttons [ x ][ y ][ "id" ] )
186174 else :
187- self .buttons [key ][ "widget" ].config (image = self .tiles ["numbers" ][self .buttons [key ]["mines" ]- 1 ])
188- self .buttons [key ]["state" ] = STATE_CLICKED
175+ self .buttons [x ][ y ][ "widget" ].config (image = self .tiles ["numbers" ][self .buttons [x ][ y ]["mines" ]- 1 ])
176+ self .buttons [x ][ y ]["state" ] = STATE_CLICKED
189177 self .clicked += 1
190178 except KeyError :
191179 pass
192180
193- def clear_empty_tiles (self , main_key ):
194- queue = deque ([main_key ])
181+ def clear_empty_tiles (self , id ):
182+ queue = deque ([id ])
195183
196184 while len (queue ) != 0 :
197185 key = queue .popleft ()
198- self .check_tile (key - 9 , queue ) #top right
199- self .check_tile (key - 10 , queue ) #top middle
200- self .check_tile (key - 11 , queue ) #top left
201- self .check_tile (key - 1 , queue ) #left
202- self .check_tile (key + 1 , queue ) #right
203- self .check_tile (key + 9 , queue ) #bottom right
204- self .check_tile (key + 10 , queue ) #bottom middle
205- self .check_tile (key + 11 , queue ) #bottom left
186+ parts = key .split ("_" )
187+ source_x = int (parts [0 ])
188+ source_y = int (parts [1 ])
189+
190+ self .check_tile (source_x - 1 , source_y - 1 , queue ) #top right
191+ self .check_tile (source_x - 1 , source_y , queue ) #top middle
192+ self .check_tile (source_x - 1 , source_y + 1 , queue ) #top left
193+ self .check_tile (source_x , source_y - 1 , queue ) #left
194+ self .check_tile (source_x , source_y + 1 , queue ) #right
195+ self .check_tile (source_x + 1 , source_y - 1 , queue ) #bottom right
196+ self .check_tile (source_x + 1 , source_y , queue ) #bottom middle
197+ self .check_tile (source_x + 1 , source_y + 1 , queue ) #bottom left
206198
207199 def gameover (self ):
208200 tkMessageBox .showinfo ("Game Over" , "You Lose!" )
0 commit comments