Lesson 4: Map Bug Detectives — Tracing and Fixing Broken Routes
Learning objective
Students will step through a broken map program one instruction at a time, circle the exact arrow (bug) where the path hits an obstacle or goes the wrong way, and write a working repair.
Materials
- Printed 4×4 Bug Detective worksheets (diagram below) and colored pencils
Vocabulary
- Bug: An error in a program that causes it to crash into an obstacle or finish on the wrong square.
- Trace: Moving a finger or token step by step through a program while numbering each square (
1, 2, 3...) to see what the program actually does.
- Debug: Finding the exact wrong arrow and fixing it.
Lesson flow
1. Opening: Why Guessing Doesn't Fix Bugs (7 min)
- Put this 4×4 Swamp Map on the board (
S = Start Camp at Col 1, Row 1; T = Treehouse at Col 4, Row 4; # = Mud Swamp):
Row 4 (Top): [ . ] [ . ] [ . ] [ T ]
Row 3: [ . ] [ # ] [ # ] [ . ]
Row 2: [ . ] [ . ] [ . ] [ . ]
Row 1 (Bottom): [ S ] [ . ] [ # ] [ . ]
Col 1 Col 2 Col 3 Col 4
- A hiker wrote this 6-arrow program to reach the Treehouse
T:
Broken Program:
Step 1: ↑ | Step 2: → | Step 3: → | Step 4: ↑ | Step 5: → | Step 6: ↑
- Ask: "If we just look at the arrows from across the room, it has three
↑s and three →s, so it looks like it should reach (Col 4, Row 4)! Why did the hiker get stuck in the Mud Swamp?"
- Trace and Number Each Step Together on the Map:
- Start at
S (Col 1, Row 1).
Step 1 (↑): Lands on (Col 1, Row 2) — write 1 in that square (safe!).
Step 2 (→): Lands on (Col 2, Row 2) — write 2 in that square (safe!).
Step 3 (→): Lands on (Col 3, Row 2) — write 3 in that square (safe!).
Step 4 (↑): Moves from (Col 3, Row 2) up to (Col 3, Row 3) — SPLAT! (Col 3, Row 3) is a Mud Swamp (#)!
- Circle
Step 4 and Step 5: If we swap Step 4 and Step 5 so the hiker goes → first (to Col 4, Row 2) and then ↑ ↑ up Column 4, the hiker walks right around the swamp and reaches T!
2. Main activity: Three Bug-Hunter Cases on the Swamp Map (18 min)
Student pairs trace three broken programs on the Swamp Map above (using a pencil, pointing step by step, sliding a large paper token, or dictating to their partner), identify the step number where the bug happens, and record or state the repaired program:
- Case 1 — The Wrong-Direction Bug:
- Broken Program:
↑ ↑ ↑ → → ←
- Trace: Steps 1–3 (
↑ ↑ ↑) climb Column 1 to (Col 1, Row 4). Steps 4–5 (→ →) walk across Row 4 to (Col 3, Row 4). Then Step 6 (←) steps backward to (Col 2, Row 4) instead of stepping onto T!
- Bug Location: Step 6 (
←).
- Fix: Change Step 6 from
← to → (↑ ↑ ↑ → → →).
- Case 2 — The Loop Count Bug (Off-by-One):
- Broken Program:
REPEAT 2 TIMES: [ ↑ ] then REPEAT 3 TIMES: [ → ]
- Trace:
REPEAT 2 TIMES: [ ↑ ] only climbs 2 squares from (Col 1, Row 1) to (Col 1, Row 3). Then the first → of the second loop crashes straight into the swamp at (Col 2, Row 3)!
- Bug Location: The first loop said
REPEAT 2 TIMES instead of REPEAT 3 TIMES.
- Fix: Change the first loop to
REPEAT 3 TIMES: [ ↑ ] (so the hiker climbs all the way to Row 4 before walking 3 steps right to T).
- Case 3 — Partner Bug Swap (6 min):
- Each student writes a working 6-step program on a clean 4×4 map, intentionally changes 1 arrow to create a bug, and trades with their partner to trace, circle, and fix.
3. Discussion questions (5 min)
- Question 1: "Why did numbering the squares
1, 2, 3, 4... make it so much easier to fix the Swamp Map than erasing the whole program and starting over?"
- Example good answer: Because Steps
1, 2, 3 were already working! Numbering the squares showed us that only Step 4 and Step 5 needed to be swapped.
- Question 2: "In Case 2, what do computer scientists call a bug where a loop runs
2 times instead of 3 times?"
- Example good answer: An off-by-one bug (counting one too few or one too many).
4. Closing check (5 min)
On the Swamp Map (S at Col 1, Row 1; Swamp # at Col 3, Row 1; T at Col 4, Row 4):
- A student writes the program:
→ → ↑ → ↑ ↑. On which step number (1, 2, 3, 4, 5, or 6) does the hiker step into a Mud Swamp (#), and which square is it?
- What is wrong with the program
↑ ↑ ↑ → → on the Swamp Map? (Where does it stop?)
- How do you fix
↑ ↑ ↑ → → so it reaches T at (Col 4, Row 4)?
Worked answers for closing check
- Step 2 (
→)! Step 1 (→) moves safely to (Col 2, Row 1), and Step 2 (→) steps right into the Mud Swamp # at (Col 3, Row 1).
- It stops 1 square short at
(Col 3, Row 4) because it only has two → arrows at the end instead of three.
- Add one more
→ at the end (↑ ↑ ↑ → → →, or REPEAT 3 TIMES: [ ↑ ] then REPEAT 3 TIMES: [ → ]).