Sponsored

Collapse

Announcement

Collapse
No announcement yet.

Two Experts - Swift Playgrounds

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Two Experts - Swift Playgrounds

    The challenge of this level is to solve this puzzle using your knowledge of initialization, parameters, and other skills in your coding tool belt.

    To solve this challenge, you'll need two experts working together. Each needs to pick different locks to raise and lower different platforms so you can collect the gem and activate the switch.

    Remember how types are just blueprints for instances? You can initialize two instances of the same type. They're basically identical, but you control them separately.

    Click image for larger version

Name:	Screen Shot 2017-01-04 at 7.24.19 PM.png
Views:	697
Size:	291.3 KB
ID:	1665

    Hint: You can initialize two instances of the Expert type; just be sure to give them different names. Place both experts into the puzzle and using the world.place method. You'll need your experts to work together to solve this puzzle.

    The full complete solution for this level is shown below:

    Code:
    let expert = Expert()
    let expert2 = Expert()
    world.place(expert, facing: .north, atColumn: 0, row: 4)
    world.place(expert2, facing: .east, atColumn: 0, row: 0)
    
    func checkGem(char: Bool, times: Int) {
     for i in 1...times {
      if char == true {
       if expert.isOnGem {
        expert.collectGem()
       }
       expert.moveForward()
      } else {
       if expert2.isOnGem {
        expert2.collectGem()
       }
       expert2.moveForward()
      }
     }
    }
    
    checkGem(char: false, times: 3)
    expert2.turnLeft()
    expert2.turnLockUp()
    expert2.turnLockUp()
    expert2.turnRight()
    expert.turnLockDown()
    expert.turnRight()
    checkGem(char: false, times: 4)
    expert2.turnLockDown()
    expert2.turnLockDown()
    checkGem(char: true, times: 8)
    Monumental! When you have a type like Expert, you can create as many instances of that type as you want. Because they all came from the same blueprint, you can call the same methods to access the same properties on any instance of that type.

    Next is Twin Peaks.

  • #2
     

    Comment


    • #3
      Code:
      let exp1 = Expert()
      
      let exp2 = Expert()
      
      
      
      
      world.place(exp1, facing: north, atColumn: 0, row: 4)
      
      world.place(exp2, facing: east, atColumn: 0, row: 0)
      
      
      
      
      exp2.collectGem()
      
      exp2.move(distance: 3)
      
      exp2.turnLeft()
      
      exp2.turnLock(up: true, numberOfTimes: 2)
      
      exp2.turnRight()
      
      
      
      
      exp1.collectGem()
      
      exp1.turnLock(up: false, numberOfTimes: 1)
      
      exp1.turnRight()
      
      
      
      
      exp2.move(distance: 3)
      
      exp2.turnLock(up: false, numberOfTimes: 2)
      
      
      
      
      while !exp1.isBlocked {
      
          exp1.moveForward()
      
          if exp1.isOnGem {
      
              exp1.collectGem()
      
          }
      
      }

      Comment

      Apple Swift Programming Language

      About this Group

      Discussion group for Swift programming language by apple for Xcode
      Type: Public
      Topics: 112
      Comments: 82

      Owner

      Sponsored Box

      Collapse

      Latest Group Topics

      Collapse

      Working...
      X