Teach the Dog a Trick
Three hedges stand in the dog's way. It can't get through them, but it can go around each one: step aside, walk past the hedge and step back onto the path. These are the eight blocks for one hedge:
Turn left ↺Move forwardTurn right ↻Move forwardMove forwardTurn right ↻Move forwardTurn left ↺
Going around is a pattern, but a loop won't help here. The hedges aren't evenly spaced, so the walks between them have different lengths. You could build the eight blocks three times, but the program would then have about 30 blocks.
Instead, teach the dog a trick. Drag the Define trick block from the column of blocks to an empty spot in the editor. It's shaped like Start: blocks snap under it. Click its name, trick, and type a better one: goAround. A name is one word, so if you type go around, the editor makes it goAround.
The blocks under Define goAround don't run by themselves. But as soon as the Define block is in the editor, the column of blocks offers a new block: goAround. Put it into your program wherever the dog stands in front of a hedge. When the dog gets to it, it carries out all the blocks under Define goAround and then goes on with the block under goAround.
A block of your own like this is called a function. You define it once, and then you call it by its name as often as you need. You'll find functions in every programming language. They make a program shorter, their names say what each part of it does, and a mistake in a function is fixed in one place for all its calls.
The column of blocks has no conditions this time. The dog walks along a path you know, and you can see where every hedge stands.
Instructions
- 1
Define the trick: drag
Define trickinto the editor, rename itgoAroundand put the eight blocks for going around a hedge under it. - 2
Under
Start, build the way to the goal out ofMove forwardandgoAround. - 3
Use at most 19 blocks. The
Define goAroundblock counts as one block, and so does eachgoAround.