尝试编写一个递归程序来显示所有相邻的空白图块

Trying to write a recursive program that reveals all adjacent tiles that are blank

我正在尝试制作扫雷游戏的克隆版。在那个游戏中,有一个特点,每当你点击一个空方块时,所有相邻的空方块都会被显示出来,并且与这些空方块相邻的空方块也会被显示出来。

现在,当我尝试实现此功能时,它只显示我单击的图块的 8 个相邻图块,而不显示显示的空图块旁边的任何其他空图块

这是我现在 运行 的代码(它有 2 个参数行和列):

local rowCords = {row-16, row, row+16}
local colCords = {col-16, col, col+16}

--Check surroundings 
for r = 1, 3, 1 do
    for c = 1, 3, 1 do
        curRow = rowCords[r]
        curCol = colCords[c]        

        if (curRow >= 16 and curRow <= 400 and curCol >= 16 and curCol <= 176) then
            if boardCords[Board:getBox(curRow, curCol)].value == 1 then
                boardCords[Board:getBox(curRow, curCol)].revealed = true
            end
        end
    end
end   

在您的算法中,您只检查 9 个图块,但您必须对已检查的图块递归执行此操作。所以你的算法必须是这样的:

function revealTile(row, col)
  if (row >= 16 and row <= 400 and col >= 16 and col <= 176) then
    local tile = boardCords[Board:getBox(row, col)]
    -- Try to reveal tile
    if not tile.revealed and tile.value == 1 then
      tile.revealed = true
      -- Try to reveal surroundings
      for r = row-16,row+16,16 do
        for c = col-16,col+16,16 do
          revealTile(r, c)
        end
      end
    end
  end
end