【发布时间】:2016-11-19 00:11:33
【问题描述】:
我正在使用 Swift 3 和 Xcode。
我正在创建一个基本上是扫雷游戏的 iOS 游戏,但没有正方形,只有六边形,所以每个六边形的周围最多可以有 6 个地雷。
我创建了一个递归算法,这样当玩家触摸一个六边形时,如果它不是炸弹,它就会调用一个名为“reveal”的递归函数: - 如果周围有一个或多个地雷并且被触摸的六边形仍然隐藏(隐藏的意思是我们不知道它是否是地雷),显示六边形并设置周围地雷的标签数量,并停止功能 - 如果周围没有地雷,对于附近每个隐藏的六边形,调用显示函数。
这就是我的代码的样子:
class Hexagon: SKShapeNode
{
var mine: Bool
var hide: Bool
var proximityMines: Int
init(mine: Bool = false, proximityMines: Int = 0, hide: Bool = true)
{
self.mine = mine // if it's a mine
self.proximityMines = proximityMines // number of surrounding mines (that I calculated using a function after I generated the level)
self.hide = hide // if the hexagon is still hidden
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
func reveal(hexagon: Hexagon)
{
if hexagon.proximityMines == 0 && hexagon.hide == true // if there are no mines in the surrounding
{
hexagon.hide = false // we update the value of this hexagon
setNumberLabel(hexagon: hexagon) // we set the .proximityMines number as a label (here 0)
for proxyHexagon in proximityHexagons(hexagon: hexagon) // for each surrounding hexagon ...
{
if proxyHexagon.hide == true // ... that is still hidden
{
reveal(hexagon: proxyHexagon) // we call this function again
}
}
}
else if hexagon.proximityMines != 0 && hexagon.hide == true // else if there are mines in the surrounding
{
hexagon.hide = false // update
setNumberLabel(hexagon: hexagon) // set label
}
}
proximityHexagons(hexagon: Hexagon) 函数返回一个数组,其中包含给定六边形的所有周围六边形。
所以我真的一次又一次地检查了我的算法,我真的认为这是一个很好的算法。
但事实是,当我创建一个 0 或我的数量非常少的关卡并单击一个六边形时,递归函数需要大约 2 秒来更新所有空的六边形。
我的地图包含或多或少的 260 个六边形,我调试了 reveal() 的调用次数,大约是相同的数量。
那么为什么要花这么多时间呢?我不认为 iPhone 6 不能处理这么多的操作!我在我的 iPhone 上试过,不是模拟器。 你有什么想法吗?
【问题讨论】:
-
你在每个周围的六边形上递归地调用它到每个周围的六边形。我不是专家,但我认为这是 O(N * N!) ?也许O(N ^ N)?而且您每次都在检查每个六边形。您需要保留一组被覆盖的六边形,每次发现一个六边形时您都需要移除这些六边形,并且只检查它们。
-
如果对于地图上的每个六边形只调用一次reveal(),那么您的算法似乎没问题。也许在同步呼叫或类似的情况下设置数字标签阻塞会导致一些与 UI 相关的减速?还要检查您的proximityHexagons() 函数需要多长时间。一个想法可能是编写一个 hiddenProximityHexagons() 函数,该函数仅返回 hide == true 的六边形。另外,检查您是从函数返回对象引用还是新创建的对象
-
@twiz_,不,我调用函数的时间与六边形的数量完全相同,所以问题不存在。
-
@samgak,你是对的,问题是proximityHexagons函数需要很多时间,我在回答中解释了,谢谢!
标签: swift algorithm recursion sprite-kit minesweeper