開啟與關閉傳送門
Portal On and Off
// 紀錄數量
var gems = 0 // 收集的寶石數
var turns = 0 // 轉反方向的次數 (每次迴轉都會 +2)
let portal = purplePortal
// 關閉傳送門
portal.close()
// 如果收集的寶石不到 7 個,就繼續走。
while gems < 7 {
walk(along: .left, onMove: {
// 往前走時,如果遇到寶石就收集、遇到開關就打開
if isOnGem {
collectGem()
gems += 1
} else if isOnClosedSwitch {
toggleSwitch()
}
}, onTurnOtherSide: { // 往反方向轉時,紀錄轉向的次數
turns += 1
if turns == 2 { portal.open() } // 第一次迴轉時,打開傳送門
if turns == 4 { portal.close() } // 第二次迴轉時,關閉傳送門
})
}extension Portal {
func close() { isActive = false }
func open () { isActive = true }
}// 貼著哪邊走的選項
enum Side {
case left, right
}
// 將「左右邊被擋住」的狀況放到同一個函數中處理。
func isBlockedOn(_ side: Side) -> Bool {
side == .left ? isBlockedLeft : isBlockedRight
}
// 將「向左右轉」放到同一個函數中處理。
func turn(_ side: Side) {
side == .left ? turnLeft() : turnRight()
}
// 轉反方向
func turnOtherSide(from side: Side) {
side == .left ? turnRight() : turnLeft()
}Last updated
Was this helpful?