Sponsored

Collapse

Announcement

Collapse
No announcement yet.

Connect and Solve - Swift Playgrounds

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

  • Connect and Solve - Swift Playgrounds

    The challenge of this lesson is to add blocks to bridge all the gaps.

    For this challenge, practice modifying the puzzle world. You'll need to add multiple blocks to reach the gems. You can create a stack by placing one block on top of another.

    Be sure to use loops, and factor your code into functions so you don't have to write the same lines of code more than once.
    Click image for larger version  Name:	Screen Shot 2017-01-05 at 1.51.12 AM.png Views:	1 Size:	276.2 KB ID:	1696


    Hint: You'll need to make multiple instances of the Block type, and place them in locations that will enable you to solve the puzzle. By stacking one block on top of another, you may be able to cross bridges of higher elevations. Use the same world.place method as you used in the previous exercise. The placement code for one block might look something like this: world.place(block1, atColumn: 2, row: 2).

    The answer solution is shown below:

    Code:
    func solveLine() {
     turnRight()
     for i in 1...4 {
      jump()
     }
    }
    
    for i in 1...3 {
     world.place(Block(), atColumn: i*2, row: 2)
     move(distance: 2)
     solveLine()
     collectGem()
     turnRight()
     solveLine()
    turnRight()
     toggleSwitch()
    }
    Amazing! You've started combining all of your coding skills in new and useful ways. Next, you'll see how to place a different type of item-a portal.

    Next lesson is "Making Your Own Portals".

  • #2
     

    Comment


    • #3
      Code:
      func solveRow() {
      
          toggleSwitch()
      
          turnRight()
      
          move(distance: 4)
      
          collectGem()
      
          turnLeft();turnLeft()
      
          move(distance: 4)
      
          turnRight()
      
      }
      
      
      
      
      [2, 4, 6].map({return world.place(Block(), atColumn: $0, row: 2)})
      
      [2, 6].map({return world.place(Block(), atColumn: $0, row: 2)})
      
      
      
      
      while !isBlocked {
      
          moveForward()
      
          if isOnClosedSwitch {
      
              solveRow()
      
          }
      
      }
      Last edited by cxs2549; 12-26-2018, 01:48 AM.

      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