Skip to content

Robot steps

Logic and puzzles · level 1, lesson 5 of 6

A question to start

Here is a robot on a floor of squares. It is the orange arrow, and it is facing up. It wants to get to the star.

Ana wrote these steps for the robot:

forward 3, turn right, forward 2

Ben wrote different steps:

turn right, forward 2, turn left, forward 3

Ana and Ben are arguing. Each one says the other's steps are wrong. Who is right? Put your finger on the robot and try both. (The answer might surprise them.)

The idea

A robot does exactly what you say

A robot is a machine that follows instructions. It is very good at following them, and very bad at guessing what you mean.

If you tell a person "go to the door", they will look around, find the door, and walk there. If you tell a robot "go to the door", it does not know what a door is, or where it is. It just stands there!

So we talk to a robot in tiny, exact steps that it understands. Our robot knows only three steps.

  • forward and a number, like forward 3: move that many squares the way you are facing.
  • turn right: turn a quarter of the way round to the right, staying on the same square.
  • turn left: turn a quarter of the way round to the left, staying on the same square.

A list of steps, in order, is called a program. People who write programs for computers and robots are called programmers, and what they do is called coding.

Turning does not move you

This is the first tricky thing. When the robot turns, it does not go to a new square. It just faces a new way, like you do when you turn on the spot.

turn right turn right turn right turn right

Turn right once: now the robot faces right. Turn right again: it faces down. Again: it faces left. Again: it faces up, where it started. Four quarter turns bring you all the way round.

The robot's right, not your right

This is the second tricky thing. "Right" means the robot's right, not yours.

When the robot faces up the page, its right is the same as your right. But when the robot faces down, towards you, its right is on your left! Try it: stand up, face the same way as a friend, and you both point right. Now turn to face your friend. Point to your right. You are pointing in opposite directions.

robot's right robot's left the robot faces down, towards you

A good trick: pretend you are the robot. Turn your own body to face the same way, and then ask "which way is my right?"

Following a program: tracing

To check a program, you trace it. Put your finger on the robot. Do one step at a time, and only that step. After each step, say where the robot is and which way it faces.

Let us trace Ana's program from the start.

Step Where is the robot? Facing
start bottom left corner up
forward 3 3 squares up up
turn right same square right
forward 2 2 squares to the right right

It lands on the star! Now Ben's program.

Step Where is the robot? Facing
start bottom left corner up
turn right same square right
forward 2 2 squares to the right, on the bottom row right
turn left same square up
forward 3 3 squares up up

It lands on the star too! Both programs work. They take different paths, but they end in the same place.

Ana: up, then right Ben: right, then up

So the answer to the question at the start is: Ana and Ben are both right. There is often more than one good program.

The order of the steps matters

What if we swap two steps of Ana's program?

  • Ana's program: forward 3, turn right, forward 2.
  • Swapped: turn right, forward 3, forward 2.

The swapped one turns first, then goes 5 squares to the right, along the bottom. That ends somewhere else entirely. Same steps, different order, different place. In a program, order matters.

Finding the mistake

When a program does not work, it has a mistake in it. Programmers call a mistake a bug, and finding and fixing it is called debugging. The way to debug is to trace, step by step, and find the first step where the robot goes somewhere you did not want.

Worked examples

Example 1. Trace it. The robot starts in the bottom left corner, facing up. Where does this program take it?

forward 2, turn right, forward 3, turn left, forward 1

  1. forward 2: up 2 squares. Facing up.
  2. turn right: same square. Now facing right.
  3. forward 3: 3 squares to the right. Facing right.
  4. turn left: same square. Now facing up.
  5. forward 1: up 1 square.

The robot ends 3 squares to the right and 3 squares up from where it started (2 up, then 1 more up), facing up. Notice the trick: the two "up" moves were split by the move to the right, so you have to add them: 2 and 1 make 3.

Example 2. Go round the rock. The robot cannot drive through a rock. Write a program to get to the star.

  1. The star is 3 squares up and 3 squares to the right.
  2. Going up first will not work: the rock is 2 squares up, right in the way.
  3. So go right first: turn right, forward 3. Now the robot is under the star.
  4. Then go up: turn left, forward 3.

Program: turn right, forward 3, turn left, forward 3. Trace it to check: right 3 squares along the bottom, then up 3 squares, and the rock is never touched.

Example 3. Find the bug. The robot starts at the bottom, in the middle, facing up. Zoe wants it to reach the star. Her program:

forward 3, turn left, forward 2

Trace it.

  1. forward 3: up 3 squares. Good so far: the star is on this row.
  2. turn left: now the robot faces left. But the star is to the right! This is the bug.
  3. forward 2: the robot goes 2 squares left, away from the star.

Fix: change "turn left" to "turn right". New program: forward 3, turn right, forward 2. Trace it again: up 3, face right, 2 squares right. On the star.

Example 4. Facing down. The robot starts at the top, facing down. The star is 2 squares down and 2 squares to the right (as you look at the page). Which turn do you need?

  1. forward 2: down 2 squares. Now the robot is level with the star.
  2. The star is to the right of the page. The robot faces down, towards you. Its right is your left! So it must turn left to face the right side of the page.
  3. forward 2.

Program: forward 2, turn left, forward 2. If you said "turn right", stand up and pretend to be the robot facing down. You will see.

Try it

For questions 1 to 5, the robot starts in the bottom left corner of a 5 by 5 floor, facing up (like in the first picture).

  1. Where does "forward 4" take it?
  2. Where does "turn right, forward 3" take it?
  3. The star is 2 squares up and 3 squares to the right. Write a program to reach it.
  4. Max wants to reach a star 2 squares up and 4 squares to the right. His program is "forward 2, turn right, forward 3". Where does the robot end? Find and fix the bug.
  5. Write a program that goes 4 squares to the right, along the bottom, and then faces up at the end.
  6. The robot starts in the top left corner, facing down. Where does "forward 2, turn left, forward 2" take it?
  7. How many "turn right" steps make the robot face the opposite way? How many make it face the same way as at the start?
  8. Play it for real: one person is the robot, one is the programmer. The programmer can only say forward (with a number), turn left and turn right. Can you get the robot to the door?
Answers
  1. Up 4 squares: the top left corner, still facing up.
  2. The turn keeps the robot on its square, facing right. Then 3 squares right: the bottom row, 4th square from the left, facing right.
  3. forward 2, turn right, forward 3. (Or: turn right, forward 3, turn left, forward 2. Both work.)
  4. Trace: up 2, turn right, 3 squares right. It ends one square short of the star. The bug is "forward 3"; it should be forward 4.
  5. turn right, forward 4, turn left. The last turn makes it face up. (Without it, the robot would still face right.)
  6. Down 2 squares. Turn left: facing down, the robot's left is the right side of the page. So it goes 2 squares to the right of the page. It ends 2 down and 2 across from the top left corner, facing right.
  7. 2 turns right make it face the opposite way (a half turn). 4 turns right bring it all the way round to face the same way again.
  8. Any program is fine. Count the squares (or steps) first. Trace it in your head before you say it.

Common mistake

Moving when you turn. "turn right" does not move the robot to the square on the right. It only spins it round on the same square. To move right, you need "turn right" and then "forward".

Using your right instead of the robot's right. When the robot faces you, its right is your left. Turn your body to face the same way as the robot before you decide.

Where it leads