5嵌套模式
目标:从其他函数中调用函数。
到目前为止,你定义过的函数仅调用了现有的命令,例如moveForward()和
collectGem()。但并不是非要如此!
函数 turnAround()(转身)可引导你的角色转身并面向相反方向。你可以在另
一个函数solveStair()(解决一侧阶梯)内部调用这个函数,并在你的代码中调用solveStair()来解决关卡中更大的区域。
这个将较大的问题分解成较小的部分的过程称为分解。
1 定义solveStair()函数,在其中调用 turnAround()。
2 调用solveStair()以及你所需要的其他函数。
3 收集全部四颗宝石完成通关。
func turnAround(){
turnLeft()
turnLeft()
}
func solvestair(){
moveForward()
collectGem()
turnAround()
moveForward()
turnRight()
}
for i in 1...4{
solveStair()
}