A Function in a Loop
Remember the Over the Hill exercise? The dog climbed the hill stair by stair: forward, right, forward, left. At the top, it turned right, and the same four blocks took it down the other side. Now there are two hills, a small one and a big one, with a short valley between them.
The four blocks are needed on all four slopes. Make them a function called zigzag:
Define zigzagMove forwardTurn right ↻Move forwardTurn left ↺
A call is a block like any other, so it can go into a loop. On every pass, the loop calls the function, and the function carries out its four blocks. So this loop takes the dog up two stairs:
Repeat 2 timeszigzag
Each slope is a loop like this one, with as many passes as the slope has stairs. Without the function, each of the four loops would have to hold all four blocks.
Between the slopes, make the dog face the right way. At the top of a hill, it turns right to face down the slope. At the bottom of the first hill, it walks two tiles along the valley and turns left to face up the next one.
Instructions
- 1
Get the dog over both hills to the goal.
- 2
Use at most 18 blocks.