Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Bones All the Way

There's a bone on every tile between the dog and the goal, as in A Bone on Every Tile. But the corridors have different lengths, so this time you need Repeat until at the goal.

The program that comes to mind first is the one from A Bone on Every Tile, in the new loop:

  • Repeat until at the goal
    • Move forward
    • Pick up bone

Go through its last pass in your head. The dog stands on the last bone, which it has just picked up. It isn't at the goal yet, so the loop runs one more pass: the dog steps onto the goal and tries to pick up a bone there. But there's no bone on the goal, so the program stops with an error.

The loop checks whether the dog is at the goal only before each pass. So in every pass, the step onto the next tile has to come last. When that step takes the dog to the goal, the loop ends before the dog tries to pick anything up. Each pass then does this: the dog picks up the bone it's standing on and steps forward.

That fixes the last pass, but it breaks the first one: at the start, the dog isn't standing on a bone. What does it have to do before the loop? The first and the last pass of a loop are often different from the others. That's why programmers always go through them in their heads.

Instructions

  1. 1

    Pick up all the bones on all three maps.

  2. 2

    Walk to the goal.

Back

6/8

  1. 1. Repeat Until at the Goal
  2. 2. Stairs of Any Height
  3. 3. While There's a Path Ahead
  4. 4. Corner After Corner
  5. 5. Quiz: Only What's Ahead
  6. 6. Bones All the Way
  7. 7. An Endless Loop
  8. 8. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.