這關(guān)我們?yōu)榱耸盏綄毷尳巧约簞?dòng)了曙强。
代碼如下:
‘’‘swift
let totalGems = randomNumberOfGems
var gemNum = 0
func findGem() {
? ? if isOnGem {
? ? ? ? collectGem()
? ? ? ? gemNum += 1
? ? }else if isBlocked? {
? ? ? ? turnLeft()
? ? ? ? turnLeft()
? ? ? ? Portalchico()
? ? }else{
? ? ? ? moveForward()
? ? }
}
func Portalchico() {
? ? if bluePortal.isActive == false {
? ? ? ? Portaltrue()
? ? }else{
? ? ? ? Portalfalse()
? ? }
}
func Portaltrue() {
? ? pinkPortal.isActive = true
? ? bluePortal.isActive = true
}
func Portalfalse() {
? ? pinkPortal.isActive = false
? ? bluePortal.isActive = false
}
while gemNum != totalGems {
? ? findGem()
}
‘’‘
執(zhí)行后: