Skip to content

Commit cec439c

Browse files
committed
simplify
1 parent 816ac17 commit cec439c

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

minesweeper.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def setup(self):
8181
isMine = True
8282
self.mines += 1
8383

84-
self.tiles[x][y] = {
84+
tile = {
8585
"id": id,
8686
"isMine": isMine,
8787
"state": STATE_DEFAULT,
@@ -93,11 +93,11 @@ def setup(self):
9393
"mines": 0 # calculated after grid is built
9494
}
9595

96-
self.tiles[x][y]["button"].bind(BTN_CLICK, self.onClickWrapper(x, y))
97-
self.tiles[x][y]["button"].bind(BTN_FLAG, self.onRightClickWrapper(x, y))
96+
tile["button"].bind(BTN_CLICK, self.onClickWrapper(x, y))
97+
tile["button"].bind(BTN_FLAG, self.onRightClickWrapper(x, y))
98+
tile["button"].grid( row = x+1, column = y ) # offset by 1 row for timer
9899

99-
# lay buttons in grid, offset by 1 row for timer
100-
self.tiles[x][y]["button"].grid( row = x+1, column = y )
100+
self.tiles[x][y] = tile
101101

102102
# loop again to find nearby mines and display number on tile
103103
for x in range(0, SIZE_X):
@@ -223,21 +223,21 @@ def clearSurroundingTiles(self, id):
223223
x = int(parts[0])
224224
y = int(parts[1])
225225

226-
for n in self.getNeighbors(x, y):
227-
self.clearTile(n["coords"]["x"], n["coords"]["y"], queue)
228-
229-
def clearTile(self, x, y, queue):
230-
try:
231-
if self.tiles[x][y]["state"] == STATE_DEFAULT:
232-
if self.tiles[x][y]["mines"] == 0:
233-
self.tiles[x][y]["button"].config(image = self.images["clicked"])
234-
queue.append(self.tiles[x][y]["id"])
235-
else:
236-
self.tiles[x][y]["button"].config(image = self.images["numbers"][self.tiles[x][y]["mines"]-1])
237-
self.tiles[x][y]["state"] = STATE_CLICKED
238-
self.clickedCount += 1
239-
except KeyError:
240-
pass
226+
for tile in self.getNeighbors(x, y):
227+
self.clearTile(tile, queue)
228+
229+
def clearTile(self, tile, queue):
230+
if tile["state"] != STATE_DEFAULT:
231+
return
232+
233+
if tile["mines"] == 0:
234+
tile["button"].config(image = self.images["clicked"])
235+
queue.append(tile["id"])
236+
else:
237+
tile["button"].config(image = self.images["numbers"][tile["mines"]-1])
238+
239+
tile["state"] = STATE_CLICKED
240+
self.clickedCount += 1
241241

242242
### END OF CLASSES ###
243243

0 commit comments

Comments
 (0)