编程语言
首页 > 编程语言> > Playground 学习编程1-函数

Playground 学习编程1-函数

作者:互联网

第二章|函数

1. 组合新行为

moveForward()
moveForward()
moveForward()
turnLeft()
turnLeft()
turnLeft()
moveForward()
moveForward()
moveForward()
collectGem()

2. 创建新函数

func turnRight() {
	turnLeft()
    turnLeft()
    turnLeft()
}
moveForward()
turnLeft()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnRight()
moveForward()
turnLeft()
moveForward()
toggleSwitch()

3. 收集、切换、重复

func nextFunc() {
	moveForward()
    collectGem()
    moveForward()
    toggleSwitch()
}
nextFunc()
moveForward()
turnLeft()
nextFunc()
moveForward()
moveForward()
turnLeft()
nextFunc()
moveForward()
turnLeft()
nextFunc()

4. 尽收囊中

func funcNext() {
	moveForward()
    collectGem()
}
collectGem()
funcNext()
funcNext()
turnRight()
funcNext()
funcNext()
turnRight()
funcNext()
funcNext()
turnRight()
funcNext()
turnRight()
funcNext()

5. 嵌套模式

func turnAround() {
	turnLeft()
    turnLeft()
}
func solveStair() {
	turnAround()
    moveForward()
    moveForward()
    collectGem()
}
moveForward()
collectGem()
solveStair()
turnAround()
moveForward()
turnLeft()
moveForward()
collectGem()
solveStair()

6. 嵌入式阶梯

func collectGemTurnAround() {
	moveForward()
    moveForward()
    collectGem()
    turnLeft()
    turnLeft()
    moveForward()
    moveForward()
}
func solveRow() {
	turnRight()
    moveForward()
    turnLeft()
    collectGemTurnAround()
    collectGemTurnAround()
}
collectGemTurnAround()
collectGemTurnAround()
solveRow()
solveRow()

7. 寻宝

func turnAround(){
    turnLeft()
    turnLeft()
}
func oneToggle(){
    moveForward()
    moveForward()
    toggleSwitch()
}
func oneMove(){
    turnAround()
    moveForward()
    moveForward()
}

func twoToggle(){
    oneToggle()
    oneToggle()
    oneMove()
    moveForward()
    moveForward()
}
oneToggle()
oneMove()
oneToggle()
oneMove()
turnRight()
twoToggle()
twoToggle()

标签:funcNext,函数,turnLeft,编程,turnRight,Playground,func,collectGem,moveForward
来源: https://blog.csdn.net/qq_44445809/article/details/117049336