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

Corner After Corner

In the While There's a Path Ahead exercise, the path had one corner. This one has several, and they all turn right. Each map has a different number of corners, and the straight parts between them have different lengths.

Look for the pattern. Along every straight part, the dog does the same thing: it walks to the corner and turns right there. You already know how to walk to a corner, wherever it is:

  • Repeat while path ahead
    • Move forward
  • Turn right ↻

That's what repeats, once for every straight part. How many times? You can't know, since each map has a different number. So put it in a Repeat until at the goal loop. You'll get a loop inside a loop, as in A Loop in a Loop, but this time each of them has a condition:

  • the inner loop decides when a straight part ends: when there's no path ahead,
  • the outer loop decides when the whole walk ends: when the dog is at the goal.

Go through the last pass in your head. The dog walks along the last straight part and stops at the goal, because the path ends there. Then it turns right, even though it doesn't need to, and only then does the outer loop check whether the dog is at the goal. It is, so the loop ends. This works because the goal is at the end of a straight part. If it were in the middle of one, the inner loop would walk the dog right past it: that loop only looks for the path ahead, not for the goal.

Instructions

  1. 1

    Get the dog to the goal on all three maps.

  2. 2

    Use at most 5 blocks. A condition such as path ahead counts as a block too.

Back

4/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.