Study CommonsRead · reason · practise

Home / Reasoning / Code a Robot Path: Turns, Loops and Finding the Bug

Reasoning & Coding · Coding

Code a Robot Path: Turns, Loops and Finding the Bug

Give a grid robot exact instructions: move forward, turn left or right, repeat steps with a loop, and find the one wrong step in a broken program.

Learning objectiveWrite and trace a sequence of movement commands on a grid, keep track of the robot's direction after each turn, shorten repeated steps with a loop, and debug a program by testing it one step at a time.

The idea

A robot does exactly what you tell it, one step at a time, and nothing more. That is what coding is: writing instructions that are clear enough for a machine to follow. The Ontario math curriculum includes coding from Grade 1, and grids like these are a common way to start.

Our robot knows three commands. F means move forward one square. L means turn left. R means turn right. A turn changes the direction the robot faces, but it does not move the robot to a new square. The robot always turns from its own point of view, as if you were standing in its shoes.

When the same steps repeat, you can use a loop. "Repeat 3 [F]" means do F three times: F F F. Loops make programs shorter and easier to check.

When a program goes wrong, do not start over. Trace it: move a finger along the grid one command at a time and say the direction out loud. The first step where the robot goes somewhere unexpected is the bug.

In every grid below, S is the start, G is the goal and dark squares are walls the robot cannot enter. Unless a question says otherwise, the robot starts on S facing right (east).

Worked examples

Example 1 · Plan a path around a wall

Write a program that takes the robot from S to G. It starts facing right.

A 3 by 5 grid. S is top left. Two wall squares fill the fourth column in the top two rows. G is bottom right.SG
A 3 by 5 grid. S is top left. Two wall squares fill the fourth column in the top two rows. G is bottom right.
  1. F F moves two squares right. The next square is a wall, so the robot must go down.
  2. R turns it to face down (south). F F moves it to the bottom row.
  3. L turns it from facing down to facing right. F F reaches G.

F F R F F L F F (8 commands)

Example 2 · Use a loop

The robot must move from S to G along one row. Write the program with a loop.

A single row of six squares with S on the left end and G on the right end.SG
A single row of six squares with S on the left end and G on the right end.
  1. Count the moves, not the squares: from S to G is 5 moves, because the robot is already standing on S.
  2. Five forward moves is F F F F F.
  3. With a loop: Repeat 5 [F].

Repeat 5 [F]

Example 3 · Find the bug

This program should take the robot from S to G: F R F F R F. It goes wrong. Find the bug.

A 3 by 3 grid. S top left. Walls on the left and right of the middle row and at the bottom left. G is bottom right.SG
A 3 by 3 grid. S top left. Walls on the left and right of the middle row and at the bottom left. G is bottom right.
  1. Step 1, F: the robot moves right to the top middle square.
  2. Step 2, R: it now faces down. Steps 3 and 4, F F: it moves down to the bottom middle square.
  3. Step 5, R: facing down, a right turn makes it face left, toward the wall. Step 6, F, would crash into the wall.
  4. To reach G on the right, step 5 must be L, not R.

The bug is step 5. Correct program: F R F F L F

Common mistakes

Turning from your own point of view instead of the robot's.

Why this fails: When the robot faces down the page (toward you), its left is your right. Imagine standing on the grid facing the same way.

Thinking a turn moves the robot.

Why this fails: L and R only change direction. The robot stays on the same square until the next F.

Counting the starting square as a move.

Why this fails: If S and G are five squares apart in a row of six, the robot needs 5 moves, not 6.

Forgetting which way the robot faces after several turns.

Why this fails: Say the direction out loud after every turn: right, down, left, up.

Rewriting the whole program when only one step is wrong.

Why this fails: Trace one step at a time. Usually you only need to change the first step that goes wrong.

Foundation

Foundation practice

Keep track of direction. Turns change direction; only F moves.

Direction

1. The robot faces right (east). After R, which way does it face?

  1. Up (north)
  2. Left (west)
  3. Down (south)
  4. Right (east)
Direction

2. The robot faces up (north). After L, which way does it face?

  1. Left (west)
  2. Right (east)
  3. Down (south)
  4. Up (north)
Direction

3. The robot faces right (east). After R R, which way does it face?

  1. Down (south)
  2. Up (north)
  3. Right (east)
  4. Left (west)
Direction

4. The robot faces down (south). After L, which way does it face?

  1. Left (west)
  2. Right (east)
  3. Up (north)
  4. Down (south)
Concept

5. The robot is on square B3 and runs the command R. Which square is it on now?

Practice

Practice practice

Trace the program on the grid. The robot starts on S facing right.

Trace

6. Run F F R F. Which lettered square does the robot end on?

  1. A
  2. B
  3. C
  4. It hits a wall
A 3 by 4 grid. S top left, A top right, B in the middle row third column, C bottom left.SABC
A 3 by 4 grid. S top left, A top right, B in the middle row third column, C bottom left.
Trace

7. On the same grid, run F F F. Where does the robot end?

  1. B
  2. C
  3. A
  4. It hits a wall
A 3 by 4 grid. S top left, A top right, B in the middle row third column, C bottom left.SABC
A 3 by 4 grid. S top left, A top right, B in the middle row third column, C bottom left.
Trace

8. On the same grid, run R F F. Where does the robot end?

  1. A
  2. B
  3. It hits a wall
  4. C
A 3 by 4 grid. S top left, A top right, B in the middle row third column, C bottom left.SABC
A 3 by 4 grid. S top left, A top right, B in the middle row third column, C bottom left.
Write a program

9. Write a program that takes the robot from S to G without entering the wall.

A 2 by 4 grid. S top left. A wall in the top row third column. G top right. The bottom row is open.SG
A 2 by 4 grid. S top left. A wall in the top row third column. G top right. The bottom row is open.
Loops

10. Which program does the same thing as Repeat 4 [F]?

  1. F 4
  2. F R F R
  3. F F F F
  4. F F
Loops

11. Shorten this program with a loop: F F F R F F F R F F F R

Think

Think practice

Debug and reason about programs.

Debug

12. This program should reach G: F F R F F L F F. The robot goes off the grid. Which step is the bug, and what should it be?

A 3 by 3 grid. S top left. Walls in the first two squares of the middle row. G bottom left.SG
A 3 by 3 grid. S top left. Walls in the first two squares of the middle row. G bottom left.
Reason

13. Ana's program reaches G in 8 commands. Leo's program for the same grid reaches G in 12 commands and never hits a wall. Is Leo's program wrong?

Reason

14. The robot runs R R R. Which single command leaves it facing the same direction?

  1. R
  2. F
  3. L
  4. R R
Trace

15. On an open grid, the robot faces up (north) and runs R F F L F. Which way is it facing at the end?

Predict

16. On an open grid, the robot runs Repeat 4 [F R]. Where does it end, and which way does it face?

Challenge

Challenge practice

Plan efficient programs.

Write a program

17. The robot starts on S facing right, but there is a wall in front of it. Write the shortest program to reach G.

A 3 by 4 grid. S top left with a wall to its right. A wall continues below it in the second column of the middle row and another wall at the end of the middle row. G bottom right.SG
A 3 by 4 grid. S top left with a wall to its right. A wall continues below it in the second column of the middle row and another wall at the end of the middle row. G bottom right.
Write a program

18. The robot must collect the star (*) and then reach G. Write a program.

A 3 by 3 grid. S top left, a star at top right, G bottom right.S*G
A 3 by 3 grid. S top left, a star at top right, G bottom right.
Loops

19. Rewrite your answer to the previous wall question (R F F L F F F) using loops.

Answer key

  1. 1. Down (south)
    From facing east, a right turn points it south, down the page.
  2. 2. Left (west)
    Facing north, your left hand points west.
  3. 3. Left (west)
    Two right turns make a half turn, so it faces the opposite way.
  4. 4. Right (east)
    Facing down the page, the robot's left is your right. This is the most common trap.
  5. 5. Still B3.
    A turn changes direction only. The robot does not move until it runs F.
  6. 6. B
    F F reaches the third square of the top row. R faces down. F moves down one square to B.
  7. 7. A
    Three moves right along the top row reach A.
  8. 8. C
    R turns it down. F F moves two squares down the left column to C.
  9. 9. Example: F R F L F F L F
    F, then R to face down, F to the bottom row, L to face right, F F along the bottom, L to face up, F to G. Other correct programs exist, such as R F L F F F L F.
  10. 10. F F F F
    The loop runs F four times.
  11. 11. Repeat 3 [F F F R]
    The block F F F R appears three times in a row.
  12. 12. Step 6 should be R, not L.
    After F F R F F the robot is at the bottom right facing down. To head left toward G it must turn right (from facing down, right points left). L would point it right, off the grid.
  13. 13. No. Both work; Ana's is shorter.
    A program is correct if it reaches the goal safely. Shorter programs are more efficient, but a longer one is not wrong.
  14. 14. L
    Three right quarter turns equal one left quarter turn.
  15. 15. Up (north).
    R faces east, F F moves, L turns back to north, F moves north. The forward moves do not change direction.
  16. 16. Back on its starting square, facing its starting direction.
    Each F R moves one square and turns a quarter. Four of them trace a small square and bring the robot home.
  17. 17. R F F L F F F (7 commands)
    Turn to face down, move to the bottom row, turn to face right, then move three squares to G.
  18. 18. F F R F F
    F F reaches the star in the top right. R faces down, and F F reaches G.
  19. 19. R, Repeat 2 [F], L, Repeat 3 [F]
    Loops are most useful when a step repeats several times in a row.

Related practice