IT

day 7 - type 본문

Learn to Code

day 7 - type

손님번호 2022. 5. 15. 22:42

if you want to build a house, you use a blueprint.
A blueprint shows the features of your house, such as the kitchen, bathrooms, and bedrooms.
When you use a blueprint to build multiple houses, you know that the houses will look similar.

In programming, a type is like a blueprint, and an instance is like a house that you built from the blueprint.


In a type, features are called properties, and behaviors are called methods.

 

 

 

Goal : Modify the state of each portal to gather the gems.

 


func turnAround() {
turnLeft()
turnLeft()

}

bluePortal.isActive = false
pinkPortal.isActive = false
while !isBlocked {
    moveForward()
    if isOnGem {
      collectGem()
      }
}
turnAround()
pinkPortal.isActive = true
moveForward()
turnAround()
moveForward()
collectGem()
turnAround()
moveForward()
bluePortal.isActive = true
moveForward()
moveForward()
collectGem()
turnAround()
bluePortal.isActive = false
while !isBlocked {
   moveForward()
   if isOnGem {
      collectGem()
      }
}
  
Comment) 점 앞에 붙은 것을 인스턴스라고 한다.




Goal : Modify the state of two portals to solve the puzzle

turnRight()
orangePortal.isActive = false
moveForward()
for i in 1 ... 2 {
    moveForward()
    turnRight()
    moveForward()
    toggleSwitch()
    turnLeft()
    turnLeft()


}
orangePortal.isActive = true
moveForward()
orangePortal.isActive = false

for i in 1 ... 3 {
    moveForward()
    collectGem()
    turnLeft()
    turnLeft()
    moveForward()
    turnLeft()
}
moveForward()
collectGem()
moveForward()
greenPortal.isActive = false
moveForward()


for i in 1 ... 2 {
    moveForward()
    collectGem()
    turnLeft()
    turnLeft()
    moveForward()
    turnLeft()
}
moveForward()
greenPortal.isActive = true

turnRight()
turnRight()
moveForward()
greenPortal.isActive = false
for i in 1 ... 4 {
    moveForward()
    toggleSwitch()
    turnLeft()
    turnLeft()
    moveForward()
    turnLeft()
}

comment ) while 를 쓰지 않았다.  정해진 횟수를 움직이는 데는 'for ' statement is better. and I learned what 'instance' is. 

when I write here 'Grammarly' helped me to correct the sentence. it is very helpful. It's a good experience.

 

 

 

 

Random Gems Everywhere

 

 

let totalGems = randomNumberOfGems

var collectedGems = 0

 

func turnAround() {

    turnLeft()

    turnLeft()

}

func flipStateOfBothPortals() {

    pinkPortal.isActive = !pinkPortal.isActive

    bluePortal.isActive = !bluePortal.isActive

}

 

func moveForwardCollectingGems() {

    if isOnGem {

        collectGem()

        collectedGems += 1

    }

    if isBlocked {

        turnAround()

        flipStateOfBothPortals()

    } else {

        moveForward()

    }

}

 

if isBlocked {

    turnAround()

    pinkPortal.isActive = false

}

 

while !(collectedGems == totalGems) {

    moveForwardCollectingGems()

    

}

 

 

 

comments) func 에 많은 점들을 포함시켰다. I can use 'func' more efficiently. I think the matter is how can I find this code in the google 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments