88import platform
99import time
1010
11+ SIZE_X = 10
12+ SIZE_Y = 10
13+
1114STATE_DEFAULT = 0
1215STATE_CLICKED = 1
1316STATE_FLAGGED = 2
@@ -47,8 +50,8 @@ def __init__(self, master):
4750 # create buttons
4851 self .buttons = dict ({})
4952 self .mines = 0
50- for x in range (0 , 10 ):
51- for y in range (0 , 10 ):
53+ for x in range (0 , SIZE_X ):
54+ for y in range (0 , SIZE_Y ):
5255 if y == 0 :
5356 self .buttons [x ] = {}
5457
@@ -72,7 +75,7 @@ def __init__(self, master):
7275 "y" : y
7376 },
7477 "widget" : Button (frame , image = gfx ),
75- "mines" : 0 # calculated after placement in grid
78+ "mines" : 0 # calculated after grid is built
7679 }
7780
7881 self .buttons [x ][y ]["widget" ].bind (BTN_CLICK , self .lclicked_wrapper (x , y ))
@@ -82,19 +85,17 @@ def __init__(self, master):
8285 self .buttons [x ][y ]["widget" ].grid ( row = x , column = y )
8386
8487 # loop again to find nearby mines and display number on tile
85- for x in range (0 , 10 ):
86- for y in range (0 , 10 ):
88+ for x in range (0 , SIZE_X ):
89+ for y in range (0 , SIZE_Y ):
8790 nearby_mines = 0
88- nearby_mines += 1 if self .check_for_mines (x - 1 , y - 1 ) else 0
89- nearby_mines += 1 if self .check_for_mines (x - 1 , y ) else 0
90- nearby_mines += 1 if self .check_for_mines (x - 1 , y + 1 ) else 0
91-
92- nearby_mines += 1 if self .check_for_mines (x , y - 1 ) else 0
93- nearby_mines += 1 if self .check_for_mines (x , y + 1 ) else 0
94-
95- nearby_mines += 1 if self .check_for_mines (x + 1 , y - 1 ) else 0
96- nearby_mines += 1 if self .check_for_mines (x + 1 , y ) else 0
97- nearby_mines += 1 if self .check_for_mines (x + 1 , y + 1 ) else 0
91+ nearby_mines += 1 if self .check_for_mines (x - 1 , y - 1 ) else 0 #top right
92+ nearby_mines += 1 if self .check_for_mines (x - 1 , y ) else 0 #top middle
93+ nearby_mines += 1 if self .check_for_mines (x - 1 , y + 1 ) else 0 #top left
94+ nearby_mines += 1 if self .check_for_mines (x , y - 1 ) else 0 #left
95+ nearby_mines += 1 if self .check_for_mines (x , y + 1 ) else 0 #right
96+ nearby_mines += 1 if self .check_for_mines (x + 1 , y - 1 ) else 0 #bottom right
97+ nearby_mines += 1 if self .check_for_mines (x + 1 , y ) else 0 #bottom middle
98+ nearby_mines += 1 if self .check_for_mines (x + 1 , y + 1 ) else 0 #bottom left
9899
99100 self .buttons [x ][y ]["mines" ] = nearby_mines
100101
@@ -181,18 +182,18 @@ def clear_empty_tiles(self, id):
181182 source_x = int (parts [0 ])
182183 source_y = int (parts [1 ])
183184
184- self .check_tile (source_x - 1 , source_y - 1 , queue ) #top right
185- self .check_tile (source_x - 1 , source_y , queue ) #top middle
186- self .check_tile (source_x - 1 , source_y + 1 , queue ) #top left
187- self .check_tile (source_x , source_y - 1 , queue ) #left
188- self .check_tile (source_x , source_y + 1 , queue ) #right
189- self .check_tile (source_x + 1 , source_y - 1 , queue ) #bottom right
190- self .check_tile (source_x + 1 , source_y , queue ) #bottom middle
191- self .check_tile (source_x + 1 , source_y + 1 , queue ) #bottom left
185+ self .check_tile (source_x - 1 , source_y - 1 , queue ) #top right
186+ self .check_tile (source_x - 1 , source_y , queue ) #top middle
187+ self .check_tile (source_x - 1 , source_y + 1 , queue ) #top left
188+ self .check_tile (source_x , source_y - 1 , queue ) #left
189+ self .check_tile (source_x , source_y + 1 , queue ) #right
190+ self .check_tile (source_x + 1 , source_y - 1 , queue ) #bottom right
191+ self .check_tile (source_x + 1 , source_y , queue ) #bottom middle
192+ self .check_tile (source_x + 1 , source_y + 1 , queue ) #bottom left
192193
193194 def gameover (self ):
194- for x in range (0 , 10 ):
195- for y in range (0 , 10 ):
195+ for x in range (0 , SIZE_X ):
196+ for y in range (0 , SIZE_Y ):
196197 if self .buttons [x ][y ]["isMine" ] == False and self .buttons [x ][y ]["state" ] == STATE_FLAGGED :
197198 self .buttons [x ][y ]["widget" ].config (image = self .tiles ["wrong" ])
198199 if self .buttons [x ][y ]["isMine" ] == True and self .buttons [x ][y ]["state" ] != STATE_FLAGGED :
0 commit comments