計算開關數量
Round Up the Switches
這個關卡要我們先收集在第一個平台上的寶石,然後再到第二個平台上去打開相同數量的開關。
// 變數初始化
var gems = 0
var switches = 0
// 策略:如果
// 1. 還沒收到寶石,或者是
// 2. 有收到寶石但開關數量比寶石少,
// 就繼續走
while gems == 0 || (gems > 0 && switches < gems) {
walkAlongLeft {
if isOnGem {
collectGem()
gems += 1
}
if isOnClosedSwitch {
toggleSwitch()
switches += 1
}
}
}
// ⭐️ 貼著左邊走的策略,
// 將來可以寫入 Actor 的 extension 中。
func walkAlongLeft(handle:()->Void) {
if !isBlockedLeft {
turnLeft()
moveForward()
handle()
} else if !isBlocked {
moveForward()
handle()
} else {
turnRight()
}
}
Last updated