扫雷,显示空单元格和所有其他空单元格逻辑和 python

minesweeper, reveal empty cell and all other empty cell logic and python

我目前正在创建扫雷器,并且已经到了我必须创建一个函数的部分,一旦玩家点击一个空的单元格,它就会显示该单元格和所有其他空单元格,包括编号单元格包围空单元格的集群(来吧伙计们,你知道扫雷 :) 我编写这个游戏的方法是有一个包含值的列表列表,0 是空的,1-8 是接触那个单元格的炸弹数量, 50是炸弹。我称之为网格。然后我使用这个网格来绘制整个游戏。然后使用另一个列表列表隐藏该网格,这些列表都包含布尔值。真隐藏,假显明。我称之为 boolgrid。例如,当玩家点击 cell[2][10] 时,boolgrid[2][10] 变为 False,显示该 cell.

选择空单元格时,必须显示周围的单元格,在某些情况下,周围的单元格也是空的,因此需要显示该单元格的周围单元格,依此类推。我的问题是编写一个支持它的函数,我尝试了很多东西,比如创建一个单元格坐标元组列表,其中单元格 == 0,然后创建一个新列表来保存所有最终可用于的新元组将所有相应的 boolgrid 单元格设置为 False。它不能很好地工作,很乱,不符合 Python 标准,占用大量内存。

我将非常感谢任何能帮助我实现此功能的人。

下面是一些精简代码,其中包含基本元素。该代码在网格中包含全 0,因此每个布尔值都应变为 False

# used to hold values to denote what the cell contains,
grid = [[0 for x in range(30)] for x in range(15)]


# used to check what cell is hidden, true is hidden, false is revealed,

booleangrid = [[True for x in range(30)] for x in range(15)]

list_to_hold_tuples = []


def find_connected_0_value_cells(cell):

        i = 5 # used as an example in the real program these are whatever cell the player selects
        j = 10  # as above


    # this function should be given an argument of a cell that the player selected.
    # reveal the surrounding cells, AND THEN take each surrounding cell, and go through
    # the same process again, until every surrounding cell containing 0, and its surrounding
    # cells containing zero have been revealed.
    # i know i need some kind of loop, but cannot seem to build it.
    # currently this function just reveals the cell and its immediate surrounding cells

        if cell[i][j] == 0:


            s = i, j + 1
            t = i, j - 1
            u = i - 1, j - 1
            v = i - 1, j
            w = i - 1, j + 1
            x = i + 1, j - 1
            y = i + 1, j
            z = i + 1, j + 1

            tempholding = [s, t, u, v, w, x, y, z]

            for i in tempholding:
                list_to_hold_tuples.append(i)


def reveal_empty_cells():

    for a,b in list_to_hold_tuples:
        booleangrid[a][b] = False

    print(booleangrid)



find_connected_0_value_cells(grid)
reveal_empty_cells()

好的,所以我对您的代码进行了一些工作,并且能够编写出有效的代码。我确实调整了网格大小只是为了更容易理解,但它应该适用于您的原始大小。只要确保您没有像发布的代码中那样使用全 0 的网格进行测试,因为它会 return 太多递归的错误。这是代码。 编辑:我更改了代码,所以现在它也显示了周围的数字

# used to hold values to denote what the cell contains,
grid = [[0,1,2,5,4,6,0,0,0,0],
        [0,1,2,5,4,6,0,0,0,0],
        [0,1,2,5,4,6,0,0,5,0],
        [0,1,2,5,4,6,0,0,0,0],
        [0,1,2,5,4,6,0,0,0,0],
        [0,1,2,5,4,6,6,0,0,0]]
# used to check what cell is hidden, true is hidden, false is revealed,

booleangrid = [[True for x in range(10)] for x in range(6)]

list_to_hold_tuples = []


def find_connected_0_value_cells(i,j):
        list_to_hold_tuples.append((i,j))

        if grid[i][j] == 0:

            s = (i, j + 1)
            t = (i, j - 1)
            u = (i - 1, j - 1)
            v = (i - 1, j)
            w = (i - 1, j + 1)
            x = (i + 1, j - 1)
            y = (i + 1, j)
            z = (i + 1, j + 1)

            tempholding = [s, t, u, v, w, x, y, z]

            for a in tempholding:
                if a[0]>=0 and a[1]>=0 and a[0]<=len(grid)-1 and a[1]<=len(grid[i])-1 and grid[a[0]][a[1]]==0:
                    if (a[0]!=i and a[1]!=j) and (a not in list_to_hold_tuples):
                        find_connected_0_value_cells(a[0],a[1])
                    list_to_hold_tuples.append(a)
                elif a[0]>=0 and a[1]>=0 and a[0]<=len(grid)-1 and a[1]<=len(grid[i])-1:
                    list_to_hold_tuples.append(a) #this part adds surrounding non-zero numbers


def reveal_empty_cells():
    global booleangrid
    for a,b in list_to_hold_tuples:
        if a>=0 and a<len(booleangrid) and b>=0 and b<len(booleangrid[a]):
            booleangrid[a][b] = False

    for i in booleangrid:
        print(i)



find_connected_0_value_cells(5,8)#Just using coordinates of 5,8 for testing, this would be click spot
reveal_empty_cells()


我已经重构并让其余部分正常工作。 谢谢@zettatekdev

grid = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 1, 2, 1, 0, 0, 0, 0, 0, 0],
        [1, 1, 2, 2, 0, 0, 0, 0, 0, 0],
        [1, 1, 2, 3, 0, 0, 0, 0, 0, 0],
        [1, 1, 2, 5, 0, 0, 0, 0, 0, 0],
        [1, 1, 2, 5, 0, 0, 0, 0, 0, 0]]





list_to_hold_tuples = []
list_change_boolgrid =[]

row = 6
cell = 10

booleangrid = [[True for x in range(cell)] for x in range(row)]

def find_connected_0_value_cells(a, b):
    list_to_hold_tuples.append((a, b))

    if grid[a][b] == 0:

        coord_list = get_surrounding_coords(a, b)

        for a,b in coord_list:
            if check_coord_values(a, b):
                if grid[a][b] != 0:
                    c = a,b
                    if c not in list_to_hold_tuples:
                        list_to_hold_tuples.append(c)
                else:
                    c = a,b
                    if c not in list_to_hold_tuples:
                        find_connected_0_value_cells(a,b)



def add_surrounding_cells():

    extra_coord = True

    for a,b in list_to_hold_tuples:

        if grid[a][b] == 0:
            coord_list = get_surrounding_coords(a,b, extra_coord)

            for i in coord_list:
                if i not in list_change_boolgrid:
                    list_change_boolgrid.append(i)

        else:
            c = a,b
            if c not in list_change_boolgrid:
                list_change_boolgrid.append(c)



def reveal_empty_cells():
    global booleangrid
    for a, b in list_change_boolgrid:
        if check_coord_values(a,b):
            booleangrid[a][b] = False




def check_coord_values(a,b):

    if a == -1 or a >= row:
        return False
    if b == -1 or b >= cell:
        return False
    else:
        return True


def get_surrounding_coords(a, b, *extra_coord):
    c = (a, b + 1)
    d = (a, b - 1)
    e = (a - 1, b - 1)
    f = (a - 1, b)
    g = (a - 1, b + 1)
    h = (a + 1, b - 1)
    i = (a + 1, b)
    j = (a + 1, b + 1)
    if extra_coord:
        k = (a, b)
        return [c, d, e, f, g, h, i, j, k]

    return [c, d, e, f, g, h, i, j]




find_connected_0_value_cells(3,5)
add_surrounding_cells()
reveal_empty_cells()

print(booleangrid)