扫雷中的Python逻辑

时间:2016-01-23 01:28:38

标签: python python-3.x logic minesweeper

我目前正在编写扫雷游戏。它一直很好,直到我遇到一次点击暴露多个瓷砖的问题。每当我点击某些东西时,它就不会显示地雷(就像它应该的那样),但只会向下,向左,向右移动。我做了它所以它打印它的方向,它认为是正确的,等等我从来没有遇到过左。这是我的代码:

from tkinter import *
from random import *

root = Tk()
root.resizable(0, 0)
root.title("Minesweeper")
frame = Frame(root)

Grid.rowconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 0, weight=1)
frame.grid(row=0, column=0)

class Tiles:
    def __init__(self, frame, size, minecount):
        self.size = size
        self.frame = frame
        self.tiles = []
        self.minearray = []
        self.curmines = 0
        self.minecount = minecount

        for x in range(self.size):
            self.tiles.append([])
            self.minearray.append([])
            for y in range(self.size):
                self.minearray[x].append(0)
                self.tiles[x].append(Button())
                self.tiles[x][y] = Button(self.frame, text=' ', width=2, bd = 3, bg="#CDCDCD", command=lambda row=x, col=y: self.clicked(row, col))
                self.tiles[x][y].grid(row=x, column=y)

        self.setMines()

    def setMines(self):
        for i in range(self.minecount):
            self.minearray[randint(0, self.size - 1)][randint(0, self.size - 1)] = 1
            self.curmines += 1
        print(self.minearray)

    def clicked(self, x, y):
        if self.minearray[x][y] == 1:
            self.tiles[x][y]["text"] = '@'
        self.tiles[x][y]["relief"] = SUNKEN
        self.minearray[x][y] = 2
        if x < self.size - 1 and self.minearray[x+1][y] == 0:
            self.clicked(x+1, y)
            print('r')
            if y > 0 and self.minearray[x+1][y-1] == 0:
                self.clicked(x+1, y-1)
                print('rd')
            if y < self.size - 1 and self.minearray[x+1][y+1] == 0:
                self.clicked(x+1, y+1)
                print('ru')

        if x > 0 and self.tiles[x-1][y] == 0:
            self.clicked(x-1, y)
            print('l')
            if y > 0 and self.minearray[x-1][y-1] == 0:
                self.clicked(x-1, y-1)
                print('ld')
            if y < self.size - 1 and self.minearray[x-1][y+1] == 0:
                self.clicked(x-1, y+1)
                print('lu')

        if y < self.size - 1 and self.tiles[x][y + 1] == 0:
            self.clicked(x, y + 1)
            print('u')

        if y > 0 and self.tiles[x][y - 1] == 0:
            self.clicked(x, y - 1)
            print('d')

tiles = Tiles(frame, 10, 20)

root.mainloop()

逻辑在点击的​​功能中:

def clicked(self, x, y):
    if self.minearray[x][u] == 1:
        self.tiles[x][y]["text"] = '@'
    self.tiles[x][y]["relief"] = SUNKEN
    self.minearray[x][y] = 2
    if x < self.size - 1 and self.minearray[x+1][y] == 0:
        self.clicked(x+1, y)
        print('r')
        if y > 0 and self.minearray[x+1][y-1] == 0:
            self.clicked(x+1, y-1)
            print('rd')
        if y < self.size - 1 and self.minearray[x+1][y+1] == 0:
            self.clicked(x+1, y+1)
            print('ru')

    if x > 0 and self.tiles[x-1][y] == 0:
        self.clicked(x-1, y)
        print('l')
        if y > 0 and self.minearray[x-1][y-1] == 0:
            self.clicked(x-1, y-1)
            print('ld')
        if y < self.size - 1 and self.minearray[x-1][y+1] == 0:
            self.clicked(x-1, y+1)
            print('lu')

    if y < self.size - 1 and self.tiles[x][y + 1] == 0:
        self.clicked(x, y + 1)
        print('u')

    if y > 0 and self.tiles[x][y - 1] == 0:
        self.clicked(x, y - 1)
        print('d')

1 个答案:

答案 0 :(得分:1)

你似乎不一致。在某些条款中,您选中self.minearray,而在其他条款中,则选中self.tiles。从初始化代码来看,它确实应该self.minearray,因为self.tiles[anything][anything]永远不会是== 0