Click & WhirrAll lessons

Planning · Lesson 07

Plan the route

This is the written walk-through of the lesson: the idea, stage by stage, plus the deeper asides. The interactive version, where you write real Python in the browser and drive a simulated robot, lives on the lesson page.

New problem: no line, no rays, just a map, a start, and a goal with walls in between. The robot has to plan a whole route before it moves. A* does it by exploring the grid one cell at a time, always expanding the most promising cell so far: the one with the smallest cost to reach it plus estimated cost still to go. That estimate is a heuristic, a hunch you write. With no hunch it floods the entire board (that’s Dijkstra’s algorithm); with a good one it beelines. Same search: the heuristic is the only difference.

A map, a start, a goal

Point your robot at a goal behind walls. There is no line to ride and no rays to read this time, just a map, a start, and a goal with walls in between. Before it moves a single wheel, the robot has to plan the whole route.

The map on the right is a grid of cells. A* plans by exploring it one cell at a time, always expanding the most promising cell so far: the one with the smallest cost to reach it plus estimated cost still to go. That estimate is a heuristic, a hunch you write in code. A good hunch keeps the search pointed at the goal; a bad one, or none at all, sends it wandering.

The starter ships with no hunch. Read the skeleton in the editor: a plan_path that hands the engine your heuristic h(a, b), and an h that returns 0 for every cell. Press Run and watch what a search with no sense of direction does.

Run as-is: the search floods

With h returning 0, every cell looks equally promising, so A* spreads out evenly in all directions and floods the board until it stumbles onto the goal. That is exactly Dijkstra’s algorithm: A* with the heuristic switched off.

It does reach the goal, so the first checkpoint clears. But look at the cell count under the grid: it explored most of the open board to get there. Reaching the goal is easy; reaching it without searching the whole map is the real problem, and that is the next gate.

Add the heuristic: beeline the goal

Now give the search a sense of direction. Replace the return 0 inside h with the Manhattan distance from the cell being scored to the goal: the steps across plus the steps down, since the robot moves one cell at a time along the grid. Each cell carries a .col and a .row, so the guess is the gap in columns added to the gap in rows. Take the size of each gap, never a negative, then add them.

Press Run again. Same search, same path, but now every cell that steps toward the goal scores better than one that steps away, so A* stops flooding and beelines. Watch the cell count collapse. When it drops under the bar, the second checkpoint clears and the lesson is done.

Go deeper: what the heuristic actually changes

The heuristic does not change WHERE A* can go or WHICH path it settles on. Both runs walk the same grid and return the same route. What changes is the order A* considers cells in. With no guess, it works outward from the start like ripples in a pond and reaches the goal last. With the Manhattan guess, a cell nearer the goal jumps the queue, so the search leans toward the goal from the first step.

no heuristic: 183 cellsManhattan: 69 cells

The Manhattan distance is honest here: because the robot moves one cell at a time across and down, the true remaining cost is never LESS than the Manhattan gap, so the guess never overpromises. That is what keeps A* correct while it hurries. A heuristic that overpromised could talk the search into skipping the real shortest route.

Ready to build it? The interactive lesson is where you write the code and watch the robot run.