Sponsored

Collapse

Announcement

Collapse
No announcement yet.

Twin Peaks - Swift Playgrounds

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

  • Twin Peaks - Swift Playgrounds

    The challenge of this level is to use all of your coding skills to collect a randomly determine number of gems, represented by totalGems.

    Test your knowledge of parameters, initialization, methods, variables, and more in this challenge! As you go through the puzzle world collecting gems, more will randomly appear. Your goal is to stop collecting gems when you've gathered a number equal to the number you specified in totalGems.

    Click image for larger version

Name:	Screen Shot 2017-01-05 at 12.50.36 AM.png
Views:	880
Size:	235.2 KB
ID:	1690

    Hint: Try using pseudocode to map out how you want to solve this puzzle. Think through which skills you can use, and find a starting point. When you've figured out a strategy, start writing and testing your code. You'll find it can be very helpful to test smaller sections of code before you try to write an entire solution. If you're stuck, try taking a break and coming later. You'll clear your mind, which can help you see new solutions that you might have missed before.

    The answer solution is shown below:

    Code:
    let totalGems = randomNumberOfGems
    var gemCounter = 0
    var moveCounter = 0
    let character = Character()
    world.place(character, facing: .north, atColumn: 3, row: 0)
    
    func checkGem() {
     if character.isOnGem {
      character.collectGem()
      gemCounter += 1
     }
    }
    
    checkGem()
    while gemCounter < totalGems {
     if moveCounter == 6 {
      character.turnRight()
      character.jump()
      character.turnRight()
      checkGem()
     } else if moveCounter == 12 {
      character.turnLeft()
      character.jump()
      character.turnLeft()
      checkGem()
     } else if moveCounter == 18 {
      character.turnLeft()
      character.turnLeft()
      moveCounter = 0
     }
     if !character.isBlocked {
      character.moveForward()
     } else {
      character.jump()
     }
     moveCounter += 1
     checkGem()
    }
    
    
    
    func solveRow(jump: Bool, times: Int) {
     for i in 1...Int {
      if jump == true {
       character.jump()
      } else {
       character.moveForward()
      }
      checkGem()
     }
    }
    Amazing! Jump up and down with joy, you just completed parameters! You now have enough coding knowledge to change the puzzle world itself. Doesn't it feel great? It's time to move on to World Building.

    Next we will be going to "Introduction to World Building" lesson.

  • #2
     

    Comment


    • #3
      Code:
      var gems = 0
      
      let exp = Expert()
      
      let char = Character()
      
      world.place(exp, facing: north, atColumn: 0, row: 4)
      
      world.place(char, facing: south, atColumn: 4, row: 6)
      
      exp.turnLockUp()
      
      repeat {
      
          for i in 1 ... 3 {
      
              for i in 1 ... 6 {
      
                  if char.isOnGem {
      
                      char.collectGem()
      
                      gems += 1
      
                  }
      
                  char.jump()
      
              }
      
              switch i {
              case 1:
                  char.turnRight()
      
                  char.moveForward()
      
                  char.turnRight()
      
              case 2:
                  char.turnLeft()
      
                  char.moveForward()
      
                  char.turnLeft()
      
              case 3:
                  char.turnLeft()
      
                  char.turnLeft()
      
              default:
                  break 
              }
      
          }
      
      } while gems != totalGems

      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