I'm quite a newbie when it comes to programming, and in our programming excercise in school I'm making a minesweeper game in python using TKinter as GUI. The game works, except my "floodfill" algorithm, to clear any adjacent blank spaces. I have made the board, printed a previously created list, according to choices(height, width and amount of mines) chosen by the user, with labels, and hidden these labels behind buttons.
I can bind events to hide these buttons when clicked on, but what I also want is to be able to hide the buttons nearby, with the help of my floodfill algorithm. I feel like all I need anymore is the line of code that will hide the button according to x and y coordinates, and not just the one that's clicked upon. I think you've got the idea,
def initGame(field, height, width, mn):
play = tk.Toplevel()
play.grid()
title = ttk.Label(play, text= "MineSweeper")
title.grid(row=0)
playfield = tk.LabelFrame(play, text = None)
playfield.grid(row = 1, rowspan = height+2, columnspan = width+2)
mine = tk.PhotoImage(file='mine.gif')
for i in range(1, height+1):
for j in range(1, width+1):
if field[i][j] == '9':
val = tk.Label(playfield, image = mine)
val.image=mine
else:
val = tk.Label(playfield, text= "%s" %(field[i][j]))
val.grid(row=i-1, column=j-1)
blist = []
for i in range(1, height+1):
for j in range(1, width+1):
btn = tk.Button(playfield, text = ' ')
blist.append(btn)
def handler(event, i=i, j=j):
return floodfill(event, field, blist, j, i)
btn.bind('<ButtonRelease-1>', handler)
btn.bind('<Button-3>', iconToggle)
btn.grid(row=i-1, column=j-1)
def floodfill(event, field, blist, x, y):
edge = []
edge.append((y,x))
while len(edge) > 0:
(y,x) = edge.pop()
if field[y][x] != '9':
#####################
else:
continue
for i in [-1, 1]:
for j in [-1, 1]:
if y + i >= 1 and y + i < len(field)-1:
edge.append((y + i, x))
if x + j >= 1 and x + j < len(field[0])-1:
edge.append((y, x + j))
the long line of # is where I believe is all I need anymore to get this system to work, in something like "button.position(x, y). I attempted to save the button in the blist, and maybe I could get the correct button that needs to be hidden from it, with the help of x and y coordinates?
of course, if you have a better idea how to solve this problem, I'd love to hear it.