Swift Playgrounds 學(xué)習(xí)編程一 條件代碼 7.決策樹(shù)

挑戰(zhàn):測(cè)試關(guān)卡世界的狀態(tài)來(lái)更改路線。

在這最后一項(xiàng)挑戰(zhàn)中,你需要沿著中央的平臺(tái)收集寶石和切換開(kāi)關(guān),但中途出現(xiàn)了幾條岔路。

你可以使用條件代碼來(lái)檢測(cè)你的角色是否位于寶石或關(guān)閉的開(kāi)關(guān)處,并且,如果你的角色所處的位置類(lèi)型不同,則采取不同的行動(dòng)。

示例

for i in 1...5{

? ? moveForward()

? ? if isOnGem{

????????solveRightSide()

? ? } else if isOnClosedSwitch {

????????solveLeftSide()

? ? }

}


func threeForward(){

? ? moveForward()

? ? moveForward()

? ? moveForward()

}

func solveRightSide(){

? ? collectGem()

? ? turnRight()

? ? threeForward()

? ? turnLeft()

? ? moveForward()

? ? collectGem()

? ? turnLeft()

? ? turnLeft()

? ? moveForward()

? ? turnRight()

? ? threeForward()

? ? turnRight()

}

func solveLeftSide(){

? ? toggleswitch()

? ? turnLeft()

? ? moveForward()

? ? collectGem()

? ? turnLeft()

? ? turnLeft()

? ? moveForward()

? ? turnLeft()

}

for i in 1..5{

? ? moveForward()

? ? if isOnGem{

? ? ? ? solveRightSide()

? ? }else if isOnClosedSwitch {

? ? ? ? solveLeftSide()

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容