gpt4 book ai didi

algorithm - 扫雷板 "opening up"

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:11:59 24 4
gpt4 key购买 nike

我正在设计一个扫雷游戏,但我找不到关于如何登上“打开”的算法。如果您曾经玩过扫雷,第一次点击左右会显示很多方 block 。它背后的算法是什么?

最佳答案

你可以使用 flood-fill算法,但仅适用于没有任何相邻炸弹的单元格(将显示“0”或无值)及其周围的单元格。

一些伪代码:

floodFill(cell)
if not cell.isOpen
cell.open()
if not cell.hasNeighbouringBombs
for each neighbour n of cell
floodFill(n)

请注意这与“正常”的洪水填充算法有何不同:

floodFill(cell)
if not cell.hasNeighbouringBombs and not cell.isOpen
cell.open()
for each neighbour n of cell
floodFill(n)

差异的原因可以看这个例子:

1 1 2
2 0 3
1 2 3

正常的flood-fill算法只会填充0,但是我们要填充上面的所有内容,因此我们应该在打开当前后检查hasNeighbouringBombs单元格。

请注意,如果您开始单击具有非零值的单元格,则不会打开其他单元格(至少在大多数游戏版本中)- 这将通过上述算法处理。

关于algorithm - 扫雷板 "opening up",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19633718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com