Sponsored

Collapse

Announcement

Collapse
No announcement yet.

Reach for the Stairs - Swift Playgrounds

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

  • Reach for the Stairs - Swift Playgrounds

    The goal of this lesson is to add stairs to solve the puzzle.

    Your next world-building element is a set of stairs. Unlike simple blocks, stairs need to face the right direction, from low side to high side. Remember, you can choose a direction by passing .north, .south, .east, or .west into the facing parameter.

    Placing stairs:
    Here's how you would place north-facing stairs at coordinate (2,3):
    let newStair = Stair()
    world.place(newStair, facing: .north, atColumn: 2, row: 3)

    However, you can use a shortcut to place the stairs more quickly! Instead of initializing your stairs instance separately, you can initialize and place the instance at the same time.

    Shortcut:
    This line of code initializes and places an unnamed instance of type Stair:
    world.place(Stair(), facing: .north, atColumn: 1, row: 1)

    1. Use the code above as a guide to place stairs in the puzzle world. Try using the shortcut to place the stairs more efficiently.
    2. Solve the puzzle using the rest of the coding skills you've learned so far.

    Click image for larger version

Name:	Screen Shot 2017-01-05 at 4.43.32 PM.png
Views:	585
Size:	263.9 KB
ID:	1792

    Hint: You can use the following code to create an instance of type Stair and place it at the same time: world.place(Stair(), facing: east, atColumn: 3, row: 3). Place stairs in the puzzle world so that you can guide your character to toggle all the switches open. You can take any of several different approaches.

    The best answer solution for this "Reach for the Stairs" is shown below:

    Code:
    world.place(Stair(), facing: south, atColumn: 3, row: 1)
    world.place(Stair(), facing: south, atColumn: 3, row: 3)
    world.place(Stair(), facing: west, atColumn: 1, row: 4)
    world.place(Stair(), facing: west, atColumn: 1, row: 6)
    world.place(Stair(), facing: east, atColumn: 5, row: 6)
    world.place(Stair(), facing: north, atColumn: 2, row: 7)
    world.place(Stair(), facing: north, atColumn: 4, row: 7)
    
    func toggleSide() {
     toggleSwitch()
     while !isBlocked {
      moveForward()
      toggleSwitch()
     }
    }
    
    func turnCorner() {
     turnRight()
     move(distance: 2)
     turnLeft()
     move(distance: 2)
     turnRight()
    }
    
    move(distance: 4)
    turnLeft()
    move(distance: 3)
    turnRight()
    for i in 1...2 {
     toggleSide()
     turnCorner()
    }
    toggleSide()
    You're getting quite good at this! Now that you've experimented with placing blocks, portals, switches, and stairs, you know the fundamentals for building worlds. Next, it's time to give some of those skills a try in solving some advanced world-building puzzles.

    Next lesson is: Floating Islands.

  • #2
     

    Comment


    • #3
      Code:
      func threeSwitches() {
      
          for i in 1 ... 10 {
      
              moveForward()
      
              if isOnClosedSwitch {
      
                  toggleSwitch()
      
              }
      
              switch i {
      
              case 3, 7:
      
                  turnLeft()
      
              case 4, 6:
      
                  turnLeft();turnLeft()
      
              default:
      
                  break 
      
              }
      
          }
      
      }
      
      
      
      
      world.place(Stair(), facing: south, atColumn: 3, row: 1)
      
      world.place(Stair(), facing: south, atColumn: 3, row: 3)
      
      world.place(Stair(), facing: north, atColumn: 3, row: 7)
      
      world.place(Stair(), facing: west, atColumn: 1, row: 5)
      
      world.place(Stair(), facing: east, atColumn: 5, row: 5)
      
      move(distance: 5)
      
      for i in 1 ... 3 {
      
          threeSwitches()
      
          if i == 1 {
      
              turnLeft()
      
          }
      
      }

      Comment

      Apple Swift Programming Language

      About this Group

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

      Owner

      Sponsored Box

      Collapse

      Latest Group Topics

      Collapse

      There are no results that meet this criteria.

      Working...
      X