Click & Whirr needs a wider screen
Open it on a laptop or desktop (1280px or wider recommended). The Learn · Code · Watch workspace needs the room to breathe.
Lesson 07
Plan the route
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.
Checkpoints
- Find the goal (any heuristic gets there) (waiting for Run)
- Find it efficiently: explore under 110 cells (waiting for Run)
Press Run ▸ to start checking your code.
Press Run as-is: return 0 explores the whole map to find the goal. Now replace the 0 with the Manhattan distance and Run again: same path, a fraction of the work.
▸API reference
- plan_path()
- Runs ONCE when you press Run. Define your heuristic inside it and hand it to world.search.
- h(a, b)
- Your estimate of the cost from cell a to the goal cell b (each has .col and .row). If it throws or returns a non-number, that cell counts as 0: the search never crashes, it just floods.
- world.search(h)
- The engine's A* over this lesson's grid. Returns a summary: try print(plan_path().nodes_explored).
space pause · r reset
Simulator
press Run to search