1. 当…时运行代码
while isOnClosedSwitch {
toggleSwitch ( )
moveForward ( )
}
2. 创建更巧妙的While循环
while ! isBlocked{
if isOnClosedSwitch{
toggleSwitch ( )
}
moveForward ( )
}
3. 正确选取工具
func turnAndCollectGem ( ) {
moveForward ( )
turnLeft ( )
moveForward ( )
collectGem ( )
turnRight ( )
}
while ! isBlocked{
turnAndCollectGem ( )
}
4. 四乘以四
while ! isBlocked{
moveForward ( )
moveForward ( )
moveForward ( )
turnRight ( )
if isOnClosedSwitch{
toggleSwitch ( )
}
}
5. 转身
func oneNext ( ) {
moveForward ( )
turnLeft ( )
collectGem ( )
}
for i in 1 . . . 4 {
oneNext ( )
oneNext ( )
moveForward ( )
turnRight ( )
}
6. 富饶之地
func oneNext ( ) {
if isOnClosedSwitch{
toggleSwitch ( )
}
}
func twoNext ( ) {
oneNext ( )
moveForward ( )
if isOnGem{
collectGem ( )
}
moveForward ( )
oneNext ( )
}
while ! isBlocked{
moveForward ( )
turnRight ( )
twoNext ( )
turnLeft ( )
moveForward ( )
turnLeft ( )
twoNext ( )
turnRight ( )
}
7. 嵌套循环
while ! isBlocked {
while ! isOnGem {
moveForward ( )
}
collectGem ( )
turnLeft ( )
}
8. 随机矩形
while ! isOnOpenSwitch{
while isBlocked{
turnRight ( )
if isOnClosedSwitch{
toggleSwitch ( )
}
}
moveForward ( )
}
9. 始终右转
func oneNext ( ) {
if isOnClosedSwitch{
toggleSwitch ( )
}
}
while ! isOnGem{
moveForward ( )
oneNext ( )
while isBlocked{
turnRight ( )
}
}
collectGem ( )