1

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.

4

1 に答える 1

0

ボタンを 2D 配列に保存するため、blist[x ,y] は x,y 位置のボタンを表します。x、y 位置がまっすぐであることがわかっている場合は、正しいボタンを取得します。

編集:

最初に 2D 配列を作成します。

blist = []
for i in range(1, height+1):
    tmpList = []
    for j in range(1, width+1):
        btn = tk.Button(playfield, text = '   ')
        tmpList.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)

    blist.append(tmpList)

次に、それを使用してフラッド関数でボタン オブジェクトを取得します。

 if field[y][x] != '9':
        Button_To_Hide = blist[x-1][y-1] 
        Button_To_Hide.grid_forget()

ここで x と y を入れ替える必要があるかもしれません。-1 フィールドの座標に 1 を指定してインデックス作成を開始したためです (私はそう思います)。

于 2013-12-08T16:57:32.413 に答える