iOS/Swift

[Swift Playground] 코딩 배우기2 - 임의의 장소에 보석 놓기

태애니 2025. 3. 17. 21:01
728x90

 

 

 

최대한 간결하게, 모듈화하도록 노력해보았다.

 

let totalGems = randomNumberOfGems

var myGem = 0

bluePortal.isActive = false

func move() {
    if isBlocked {
        turnAround()
        portalProgress()
    }
    moveForward()
}

func collectMyGem(){
    if isOnGem {
        collectGem()
        myGem += 1
    }
}

func turnAround(){
    turnRight()
    turnRight()
}

func portalProgress(){
    pinkPortal.isActive.toggle()
    bluePortal.isActive.toggle()
}

while totalGems != myGem {
    move()
    collectMyGem()
}

 

 

 

귀여운 녀석ㅎ_ㅎ

728x90