首页 > 解决方案 > 扫雷,显示空单元格和所有其他空单元格逻辑和 python

问题描述

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

When an empty cell is selected, the surrounding cells have to be revealed, which in some cases, are also empty, so the surrounding cells of THAT cell need to be revealed, and so on. 我的问题是编写一个支持它的函数,我尝试了很多事情,比如创建一个单元格坐标的元组列表,其中单元格 == 0,然后创建一个新列表来保存所有最终可以用于的新元组将所有相应的布尔网格单元格设置为 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()

标签: pythonlistfor-loopwhile-loopminesweeper

解决方案


我已经重构并让它的其余部分工作。谢谢@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)

推荐阅读