07 October 2020

Learn to Code 1 - เฉลยแบบฝึกหัด Logical Labyrinth

เฉลยแบบฝึกหัด Learn to Code 1

บทที่ 5 Logical Operators

แบบฝึกหัด Logical Labyrinth


โจทย์ Use logical operators and conditional code to move through the puzzle world.

คำแปล จงใช้ตัวดำเนินการทางตรรกะ (เครื่องหมาย AND OR NOT) และโค้ดบอกเงื่อนไข เพื่อเคลื่อนที่ไปบทโลกปริศนา





เฉลย 

func clearRight() {

    toggleSwitch()

    collectGem()

    turnRight()

    moveForward()

    moveForward()

    collectGem()

    turnRight()

    turnRight()

    moveForward()

    moveForward()

    turnRight()

}



for i in 1 ... 8 {

    moveForward()

    if isOnGem && !isOnOpenSwitch {

        clearRight()

    } else if isBlocked || isOnClosedSwitch {

        toggleSwitch()

        turnLeft()

    }

} 




ขอบคุณภาพจาก Swift Playgrounds

-- ดีบี --