Python Logic in Minesweeper

好久不见. 提交于 2021-02-08 10:12:55

问题


I am currently writing a Minesweeper game. It was going fine until I encountered the problem with exposing multiple tiles in one click. Whenever I click something, it never shows mines (like it's supposed to) but only ever works its way down, to the left, and to the right. I made it so it prints its direction, and it thinks down is right, etc. I have never encountered a left. Here is my code:

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()

The logic is in the clicked function:

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:


You seem to be inconsistent. In some clauses, you check self.minearray, while in others, you check self.tiles. Judging from the initialization code, it should indeed self.minearray, as self.tiles[anything][anything] will never be == 0.



来源:https://stackoverflow.com/questions/34958582/python-logic-in-minesweeper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!