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 whilepath aheadMove 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
Get the dog to the goal on all three maps.
- 2
Use at most 5 blocks. A condition such as
path aheadcounts as a block too.