Sponsored

Collapse

Announcement

Collapse
No announcement yet.

Moving Further Forward - Swift Playgrounds

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

  • Moving Further Forward - Swift Playgrounds

    The goal of this level is to write a function to move forward a certain number of times.

    In this puzzle, a new function lets you move across multiple tiles using a single command, reducing the repetition in your code. Using a parameter, you'll specify an input (distance) for your function. When you call the function, you'll pass in a value, or argument, for distance. For example, in move(distance: 6), 6 is the argument.

    The function declaration for move is provided below with a distance parameter. Use the distance value in the function to specify how many times to run moveForward(). When you call move, pass in the argument for distance to run moveForward() that number of times.

    1. Fill in the function definition, using the distance parameter in a loop that calls moveForward() a given number of times.
    2. If you use a for loop, make distance the number of times the loop runs. Example: for i in 1 ... distance {
    3. Solve the puzzle using the move function.

    Click image for larger version

Name:	Screen Shot 2017-01-02 at 7.05.50 PM.png
Views:	281
Size:	242.0 KB
ID:	1391

    Hint: First, define the move function. In the function, write a for loop that uses the distance value to determine how many times the loop runs. Your for loop could start off like this: for i in 1 ... distance. After you write your move function, call it and pass in an argument to move your character forward by number of tiles defined for distance. For example, move(distance: 5) will move your character forward 5 tiles,

    The complete answer and solution is shown below:

    Code:
    let expert = Expert()
    
    func move(distance: Int) {
     for i in 1...distance {
      expert.moveForward()
     }
    }
    
    move(distance: 6)
    expert.turnRIght()
    expert.move(distance: 2)
    expert.turnRIght()
    move(distance: 5)
    expert.turnLeft()
    move(distance: 5)
    expert.turnLeft()
    expert.turnLockUp()
    expert.turnLeft()
    move(distance: 3)
    expert.turnRight()
    move(distance: 3)
    expert.turnRight()
    move(distance: 4)
    expert.collectGem()
    You're getting really good at this! Defining a function using parameters can be extremely useful. It can make the function more reusable, meaning you can call it in many more situations. See how else you can use parameters!

    Next lesson we will be looking at Generalizing a Function.

  • #2
     

    Comment


    • #3
      Code:
      func move(distance: Int) {
          for i in 1 ... distance {
      
              expert.moveForward()
      
          }
      
          for i in 1 ... 1 {
      
              if expert.isBlocked {
      
              expert.turnRight()
      
              break
      
          } else if expert.isBlockedLeft && !expert.isBlockedRight {
      
              expert.turnRight()
      
          } else if !expert.isBlockedLeft || expert.isBlockedRight {
      
              expert.turnLeft()
      
          }
      
      }
      
      move(distance: 6)
      
      move(distance: 2)
      
      move(distance: 5)
      
      move(distance: 5)
      
      expert.turnLockUp()
      
      expert.turnLeft()
      
      move(distance: 3)
      
      move(distance: 3)
      
      move(distance: 4)
      
      expert.collectGem()
      Last edited by cxs2549; 12-01-2018, 07:08 PM.

      Comment


      • #4
        The swift playgrounds given hint solution for this is wrong. It doesn’t work and you can’t see the hints anymore after completing the puzzle without resetting. There is no way to let apple easily know.

        Comment

        Working...
        X