Sponsored

Collapse

Announcement

Collapse
No announcement yet.

Placing Two Characters - Swift Playgrounds

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

  • Placing Two Characters - Swift Playgrounds

    The goal of this lesson is to place a character and an expert, then use the jump() ability to solve the puzzle.

    In this puzzle, you'll choose starting locations for both your character and your expert. You'll also need a new ability to solve this puzzle. Just as your expert has the special skill of turning locks, your character has the special skill of jumping.

    New Ability!
    The Character type has the ability to jump up and down when you use the following command:
    jump()

    1. Identify starting locations for your character and your expert.
    2. Initialize both characters and place them at your starting locations.
    3. Use the jump() command to make your character jump when needed to solve the puzzle.

    Click image for larger version

Name:	Screen Shot 2017-01-04 at 5.34.22 PM.png
Views:	1
Size:	286.3 KB
ID:	1639

    Hint: First, initialize and place both your expert and your character into the puzzle world.

    The full answer solution for this "Placing Two Characters" level is shown below:

    Code:
    let expert = Expert()
    let character = Character()
    
    world.place(expert, facing: .north, atColumn 3, row: 0)
    world.place(character, facing: .north, atColumn: 0, row: 0)
    
    func jumpCollect() (
     character.jump()
     character.jump()
     character.collectGem()
    }
    
    character.collectGem()
    jumpCollect()
    jumpCollect()
    character.turnRight()
    jumpCollect()
    jumpCollect()
    character.turnLeft()
    jumpCollect()
    Outstanding! Passing arguments into functions with parameters will soon become second nature to you. When you learn something new in coding, it can often seem a little intimidating until you've seen and used it in many different places. The more you code, the more familiar you'll become with the concepts you've learned.

    Next lesson we will be solving: Two Expert.

  • #2
     

    Comment


    • #3
      Code:
      let character = Character()
      
      let expert = Expert()
      
      
      
      
      func jumpCollect(blocks: Int) {
      
          for i in 1 ... blocks {
      
              character.jump()
      
              if character.isOnGem {
      
                  character.collectGem()
      
              }
      
          }
      
      }
      
      
      
      
      world.place(character, facing: north, atColumn: 0, row: 0)
      
      world.place(expert, facing: north, atColumn: 3, row: 0)
      
      
      
      
      expert.turnLockUp()
      
      expert.toggleSwitch()
      
      character.collectGem()
      
      
      
      
      jumpCollect(blocks: 4)
      
      character.turnRight()
      
      jumpCollect(blocks: 4)
      
      character.turnLeft()
      
      jumpCollect(blocks: 2)

      Comment

      Working...
      X