apple Swift playground 算法之‘调整算法’代码
·

本章内容主要是要打破固有思维,注意观察已给出的线索与条件。
线索如下:

条件如下:只能通过修改“navigateAroundWall()”自定义函数来优化算法,不能碰其他while函数里的代码。
根据线索我们需要准备三种代码来运行算法,完整代码如下:
func navigateAroundWall() {
if isBlocked && isBlockedRight{
turnLeft()
moveForward()
}else if isBlockedRight {
moveForward()
}else {
turnRight()
moveForward()
}
}
while !isOnClosedSwitch {
navigateAroundWall()
if isOnGem {
collectGem()
turnLeft()
turnLeft()
}
}
toggleSwitch()
线索一:当右边与前方受阻时,左转并前进一步
if isBlocked && isBlockedRight{
turnLeft()
moveForward()
线索二:当右边受阻时,前进一步
else if isBlockedRight {
moveForward()
线索三:当右边没有受阻时,右转并前进一步
else {
turnRight()
moveForward()
}
教程结束,感谢观看。
更多推荐

所有评论(0)