Lesson 5: Bug Hunters — Finding and Fixing Broken Algorithms
Learning objective
Students will systematically debug broken grid algorithms (including sequences, REPEAT loops, and IF conditionals) by tracing step by step to find the exact line where actual state diverges from expected state, classifying the bug, and testing a fix.
Materials
- 5×5 tabletop grids, robot tokens, obstacles
X, and goal stars ★
- Printed "Bug Hunter Case File" sheets (3 cases below) and pencils
Vocabulary
- Bug: A mistake in an algorithm that causes it to stop early, crash into an obstacle, or produce the wrong result.
- Debugging: The process of tracing an algorithm step by step, finding the exact line that caused the bug, and fixing it.
- Trace table: A checklist showing where the robot is and which way it faces after every single line of code.
Lesson flow
1. Warm-up: Why Do We Call It a "Bug"? & The Three Classic Bug Types (8 min)
- Share a brief history note: Engineers had already used the word "bug" for mechanical and electrical glitches since the 1800s, and in 1947 operators working on the Harvard Mark II electromechanical computer taped a real moth from a relay into their logbook with the joke "First actual case of bug being found." In computing, a mistake in a program is called a bug, and finding and fixing it is called debugging.
- Introduce the Three Classic Grid Bugs on the board:
- Type 1 — Wrong Turn / Direction Bug: Writing
TURN LEFT when TURN RIGHT was needed.
- Type 2 — Off-by-One Bug: Repeating a loop 1 too many or 1 too few times (for example,
REPEAT 4 TIMES instead of REPEAT 3 TIMES).
- Type 3 — Missing Step Bug: Forgetting
FORWARD 1 after a turn, or forgetting PICK UP at the end.
2. Direct instruction: The Golden Rule of Debugging (10 min)
Tell students: "When an algorithm fails, do NOT erase the whole page right away! Often many of the steps already work as intended, and we only need to repair the specific step where the robot goes off track. Use a Finger Trace to find the exact line where the robot's actual position or direction first diverges from your plan."
Model Case 0 on the board:
- Setup: Start at A1 facing North. Goal star ★ is at B3. Rock X is at A3.
- Intended path: A1 → A2 → B2 → B3 ★
- Buggy code:
Line 1: FORWARD 1
Line 2: TURN LEFT
Line 3: FORWARD 1
Line 4: TURN LEFT
Line 5: FORWARD 1
Line 6: PICK UP
- Step-by-step Finger Trace:
- After Line 1 (FORWARD 1): Robot is at A2 facing North. (Matches plan!)
- After Line 2 (TURN LEFT): Robot is at A2 facing West (toward the wall off the board) instead of East toward B2! (Divergence found on Line 2!)
- Fix: Change Line 2 from TURN LEFT to TURN RIGHT. Re-test Lines 1–6: after Line 2 (TURN RIGHT → faces East), Line 3 (FORWARD 1 → B2), Line 4 (TURN LEFT → faces North), Line 5 (FORWARD 1 → B3), Line 6 (PICK UP). Success!
3. Guided practice: Three Bug Hunter Case Files (17 min)
Pairs set up each case on their 5×5 grid, trace the buggy code to find the exact broken line, classify the bug type, and test their repair:
Case File 1: The Overshot Treasure (Off-by-One Bug)
- Setup: Start at
A1 facing East. Star ★ is on D1.
- Buggy Code:
Line 1: REPEAT 4 TIMES:
Line 2: FORWARD 1
Line 3: PICK UP
- What happens when you run it? The robot walks
A1 → B1 → C1 → D1 → E1 and tries to PICK UP on E1 (1 square past D1!).
- Fix: Change Line 1 from
REPEAT 4 TIMES: to REPEAT 3 TIMES:.
Case File 2: The Spin-In-Place Trap (Missing Step Bug)
- Setup: Start at
B1 facing North. Rock X is on B2. Star ★ is on C2.
- Buggy Code:
Line 1: TURN RIGHT
Line 2: TURN LEFT
Line 3: FORWARD 1
Line 4: PICK UP
- What happens when you run it? Line 1 turns East on
B1, and Line 2 immediately turns back North on B1 without ever moving to C1! Then Line 3 crashes into the Rock at B2.
- Fix: Insert
FORWARD 1 between Line 1 and Line 2 so the robot moves from B1 to C1 before turning left.
Case File 3: The Backwards Sensor (Conditional Bug)
- Setup: Start at
A2 facing East. Square B2 has a Rock X. Goal is to step safely onto A3.
- Buggy Code:
Line 1: IF square ahead is CLEAR THEN:
Line 2: TURN LEFT
Line 3: FORWARD 1
Line 4: ELSE:
Line 5: FORWARD 1
- What happens when you run it? Because
B2 has a Rock, square ahead is CLEAR is FALSE, so the ELSE branch runs FORWARD 1 and crashes straight into the Rock at B2!
- Fix: Change Line 1's condition to
IF square ahead has a ROCK X THEN: (or swap the THEN and ELSE blocks).
4. Wrap-up & Unit Exit Check (10 min)
A robot starts at A1 facing North. The Star ★ is at C2. There are no rocks. A student wrote this 5-line program:
Line 1: FORWARD 1
Line 2: TURN RIGHT
Line 3: REPEAT 3 TIMES:
Line 4: FORWARD 1
Line 5: PICK UP
Answer these three questions:
- Trace the buggy program as written: Which square (
C2, D2, or E2) does the robot actually stand on when it reaches Line 5?
- Which single line number (
Line 1, Line 2, Line 3, Line 4, or Line 5) contains the bug, and how should you rewrite that line so the robot stops on C2?
- Is this bug a Wrong Turn Bug, an Off-by-One Bug, or a Missing Step Bug?
Worked answers for exit check
- After Line 1 (
FORWARD 1), the robot is on A2 facing North. After Line 2 (TURN RIGHT), it is on A2 facing East. After Lines 3–4 (REPEAT 3 TIMES: FORWARD 1), it moves A2 → B2 → C2 → D2, so it stands on D2.
- Line 3 has the bug. Rewrite Line 3 as
REPEAT 2 TIMES: (so it moves A2 → B2 → C2 and stops on C2).
- It is an Off-by-One Bug (the loop repeated 3 times instead of 2 times).