Instructions a robot can't misunderstand
'Walk forward until you reach the door.' Simple for you. Why could it send a robot through a wall?
About 25 min · 3 goals · Checked by automated tests; not yet reviewed by an educator
1.Why should I care?
Most bugs in real machines aren't the machine's fault: they're instructions that meant one thing to the person who wrote them and something else to the machine. Learning to write exact instructions is a superpower for any engineer.
- ◇ Rewrite vague instructions as exact ones a machine can follow.
- ◇ Use position and facing direction to predict a robot's path.
- ◇ Break a route into short instruction sequences.
2.Predict
The robot starts at (0, 0) facing north and runs 'Forward 1' four times, then 'Turn right' and 'Forward 1' four times. Walls are at (2, 4) among others. What happens?
Guess first. Being wrong here is how the surprise works.
3.Discover
Try the four-forwards-then-turn program from the prediction and watch it get stuck. Then find a program that reaches the flag. Count your instructions.
Instruction robot
Write a program of exact instructions to move the robot from the bottom-left square to the flag without touching a wall. The robot follows the list in order and never guesses what you meant.
The program is empty. Add instructions, then press Step or Run all. The robot starts bottom-left, facing north.
Can't use the interactive? Read a description instead
The grid robot can be replaced by this description: on a 6-by-6 grid with walls at (1, 1), (2, 1), (3, 1), (3, 3) and (2, 4), the program 'Forward ×4, Turn right, Forward ×4' stops at (1, 4) because (2, 4) is a wall. Going up two, then east four, then north two reaches the flag at (4, 4) in 10 instructions.
4.Understand
Vague words to avoid
Words like a bit, until it looks right, nearby or carefully mean nothing to a machine. Replace them with things it can count or measure: Forward 3, until the distance sensor reads less than 10 cm, turn 90° right.
Track two things
To predict a grid robot, keep track of where it is (x, y) and which way it faces. Forward changes the position in the facing direction. Turns change only the direction. Writing both down after every step is the most reliable way to follow a program.
Chunk the route
Long programs are easier to write in chunks: 'get to the corridor', 'go along the corridor', 'turn into the room'. Each chunk is a short sequence. This is decomposition, and it makes mistakes much easier to find.
Explain it another way
Imagine guiding a blindfolded friend through an obstacle course using only 'step', 'turn left' and 'turn right'. You'd watch every step and count carefully, because they'll do exactly what you say.
The robot's state is (x, y, heading). Forward maps (x, y) to (x + dx, y + dy) where (dx, dy) depends on heading: north (0, 1), east (1, 0), south (0, −1), west (−1, 0). Turns rotate the heading by 90°.
Factory robots are programmed with exact coordinates and angles. A pick-and-place robot told to move 'roughly above the box' would be useless; it is told to move to (412.5 mm, 128.0 mm) at a precise speed.
Worked example 1
Make this exact: 'Go forward a few squares, then turn towards the flag.' The flag is 3 squares north and 2 east of the robot, which faces north.
Forward 1, Forward 1, Forward 1, Turn right, Forward 1, Forward 1. 'A few' became 3, 'towards the flag' became 'Turn right' (north to east), and the last part is 2 forwards.
Common mix-ups
- The robot will steer round obstacles it can see.
✓ A robot following a fixed sequence doesn't look. It runs each instruction exactly, even into a wall. - Turning left then right moves you sideways.
✓ Left then right just puts you back to facing the same way, in the same square.
5.Practice
Practice, not a test. Wrong answers get a hint and another try.
Practice 1
Which instruction is exact enough for a robot?
Practice 2
A robot at (3, 2) facing east does 'Turn left'. Where is it now, and which way does it face?
Practice 3 · a fresh version every time
A robot starts at (0, 0) facing north on an open grid (east is +x, north is +y). It runs: forward 1, forward 1, forward 1, forward 1, forward 1, forward 1. What is its final y (north–south) coordinate? Use a negative number for south.
Practice 4 · a fresh version every time
A robot faces north (0°). Compass angles go clockwise: east 90°, south 180°, west 270°. It makes these 90° turns: right, right, right, left, right. Which compass angle does it face at the end (0 to 270)?
6.Apply it
Write exact instructions to guide the grid robot from (0, 0) to the flag at (4, 4) in as few instructions as you can, avoiding all the walls. Can you beat 10?
7.Reflect
Concepts: Algorithms, searching and complexity · Functions and decomposition