Fetch First
In these mazes, the dog's owner waits at the goal, and the bone lies further in, behind the flag. Run the program from the last exercise. The dog walks onto the flag, and the program ends: Repeat until at the goal checks before every pass whether the dog is at the goal, and the dog gets to the goal before it gets to the bone. The bone stays in the maze.
The dog has to walk past the flag, fetch the bone and bring it back. That takes two loops, one under the other:
Repeat whilenotbone here, with one step of the right-hand rule inside, takes the dog past the flag to the bone. Under the loop,Pick up bonepicks it up.Repeat until at the goal, again with one step of the rule inside, takes it on from the bone. The dog keeps following the wall, and the wall leads it back to the flag.
Both loops need the same six blocks of the rule. You could build them twice, but the program would then have 17 blocks, and this exercise allows 14. A function lets you build them just once: put the rule under Define step, and both loops call it with the step block. If the rule had a bug, you'd also fix it in one place, and the program reads like its plan: step until the bone, pick it up, step until the goal.
To make the function, drag Define trick into the editor, click its name and type step. Then drag Turn right ↻ out of the loop and under Define step: the blocks under it come along. The column of blocks now has a step block, which calls the function.
The If that checked every tile for a bone isn't needed anymore: each map has just one bone, and the Pick up bone under the first loop picks it up.
Instructions
- 1
Make a function
stepwith one step of the right-hand rule. Call it inRepeat whilenotbone hereand putPick up boneunder the loop, so that the dog picks up the bone on every map. - 2
Under that, add
Repeat until at the goalwithstepinside, so that the dog walks to the goal. - 3
Use at most 14 blocks.