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.
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.
- F F moves two squares right. The next square is a wall, so the robot must go down.
- R turns it to face down (south). F F moves it to the bottom row.
- 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.
- Count the moves, not the squares: from S to G is 5 moves, because the robot is already standing on S.
- Five forward moves is F F F F F.
- 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.
- Step 1, F: the robot moves right to the top middle square.
- Step 2, R: it now faces down. Steps 3 and 4, F F: it moves down to the bottom middle square.
- Step 5, R: facing down, a right turn makes it face left, toward the wall. Step 6, F, would crash into the wall.
- 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
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.
Why this fails: L and R only change direction. The robot stays on the same square until the next F.
Why this fails: If S and G are five squares apart in a row of six, the robot needs 5 moves, not 6.
Why this fails: Say the direction out loud after every turn: right, down, left, up.
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.
1. The robot faces right (east). After R, which way does it face?
- Up (north)
- Left (west)
- Down (south)
- Right (east)
2. The robot faces up (north). After L, which way does it face?
- Left (west)
- Right (east)
- Down (south)
- Up (north)
3. The robot faces right (east). After R R, which way does it face?
- Down (south)
- Up (north)
- Right (east)
- Left (west)
4. The robot faces down (south). After L, which way does it face?
- Left (west)
- Right (east)
- Up (north)
- Down (south)
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.
6. Run F F R F. Which lettered square does the robot end on?
- A
- B
- C
- It hits a wall
7. On the same grid, run F F F. Where does the robot end?
- B
- C
- A
- It hits a wall
8. On the same grid, run R F F. Where does the robot end?
- A
- B
- It hits a wall
- C
9. Write a program that takes the robot from S to G without entering the wall.
10. Which program does the same thing as Repeat 4 [F]?
- F 4
- F R F R
- F F F F
- F F
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.
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?
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?
14. The robot runs R R R. Which single command leaves it facing the same direction?
- R
- F
- L
- R R
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?
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.
17. The robot starts on S facing right, but there is a wall in front of it. Write the shortest program to reach G.
18. The robot must collect the star (*) and then reach G. Write a program.
19. Rewrite your answer to the previous wall question (R F F L F F F) using loops.
Answer key
- 1. Down (south)
From facing east, a right turn points it south, down the page. - 2. Left (west)
Facing north, your left hand points west. - 3. Left (west)
Two right turns make a half turn, so it faces the opposite way. - 4. Right (east)
Facing down the page, the robot's left is your right. This is the most common trap. - 5. Still B3.
A turn changes direction only. The robot does not move until it runs F. - 6. B
F F reaches the third square of the top row. R faces down. F moves down one square to B. - 7. A
Three moves right along the top row reach A. - 8. C
R turns it down. F F moves two squares down the left column to C. - 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. F F F F
The loop runs F four times. - 11. Repeat 3 [F F F R]
The block F F F R appears three times in a row. - 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. 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. L
Three right quarter turns equal one left quarter turn. - 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. 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. 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. F F R F F
F F reaches the star in the top right. R faces down, and F F reaches G. - 19. R, Repeat 2 [F], L, Repeat 3 [F]
Loops are most useful when a step repeats several times in a row.