Sponsored

Collapse

Announcement

Collapse
No announcement yet.

Rivers to Cross - Swift Playgrounds

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

  • Rivers to Cross - Swift Playgrounds

    The challenge of this lesson is to place your expert in the puzzle world, and solve the puzzle.

    You can now place the expert in a specific location, but what if you want the expert to face a specific direction, as well? You can call a different version of the place method that takes a direction as an additional argument.

    Specifying a direction for your expert:
    world.place(expert, facing: .west, atColumn: 6, row: 3)

    But wait, what does.west mean? Think of it as a shorter form of dot notation that gives you a group of options to choose from. In this case, you can choose .west, .east, .north, or .south, but nothing else!

    Choices like this work because each choice is of the same type, an enumeration, that defines that group of related values. You could write each choice like Direction.north, for example, but you can also leave out Direction to make it simpler.

    Start by initializing your expert. Then find world in the shortcut bar and add it to your code. Use dot notation to call the place method that includes the facing parameter, and pass in your arguments. Then solve the puzzle.

    Click image for larger version

Name:	Screen Shot 2017-01-04 at 5.14.56 PM.png
Views:	1
Size:	297.1 KB
ID:	1636

    Hint: Before you can solve this puzzle, you'll need to place your expert into the puzzle world. Initialize an instance of type Expert, and then specify where you want to place that instance. To place your expert using the place method, pas expert into the item parameter. Then pass .north, .south, .east, or .west into the facing parameter. Finally pass in two Int values for atColumn and row. Here's an example of how to use the place method: world.place(expert, facing: .north, atColumn: 3, row: 4). Remember, the puzzle world is on a grid, with the (0,0) coordinates at the bottom left.

    The complete answer solution is shown below:

    Code:
    let expert = Expert()
    world.place(expert, facing: .south, column: 1, row: 8)
    
    func moveCollect(times: Int) {
     for i in 1...times {
      expert.moveForward()
      if expert.isOnGem {
       expert.collectGem()
      }
     }
    }
    
    expert.collectGem()
    moveCollect(times: 3)
    expert.turnLockDown()
    expert.turnLeft()
    expert.move(distance: 2)
    moveCollect(times:2)
    expert.turnLockUp()
    expert.turnRight()
    moveCollect(times:4)
    Well done! You now know how to access the puzzle world itself through the world instance. This skill will become extremely valuable, enabling you to modify elements of the puzzle world.

    Next lesson we will be looking at Placing Two Characters.

  • #2
     

    Comment


    • #3
      Code:
      let expert = Expert()
      
      world.place(expert, facing: south, atColumn: 1, row: 8)
      
      
      func moveCollect() {
      
          while !expert.isBlocked {
      
              expert.moveForward()
      
              if expert.isOnGem {
      
                  expert.collectGem()
      
              }
      
          }
      
      }
      
      expert.collectGem()
      
      moveCollect()
      
      expert.turnLockDown()
      
      expert.turnLeft()
      
      moveCollect()
      
      expert.turnLockUp()
      
      expert.turnRight()
      
      moveCollect()

      Comment

      Working...
      X