Maze Solver Robot Simulator for Flood-Fill Planning
Run a maze solver robot in your browser: watch it explore every corridor and dead end, then flood-fill its map to find and speed-run the one shortest path.
- Category
- ESP32
- Time
- 2–4 hours
- Platform
- Browser · ESP32
01 / Start here
Introduction
Maze-solving robots explore first, then optimise. This lab shows both phases: the robot walks the maze depth-first — down every corridor, into dead ends, backtracking out — until the whole maze is mapped, then flood-fills from the goal to find the single shortest path and speed-runs it. Watch it try all the wrong paths, then take the right one — the same explore-then-optimise idea a real ESP32 Micromouse uses.
Live lab / Maze exploration & flood-fill shortest path
Maze solver simulator
Watch a robot explore a real maze — down every corridor, into dead ends, backtracking out — then flood-fill the map and speed-run the one shortest path to the centre.
Bold lines are maze walls. The shaded corridors are everywhere the robot explored (including dead ends it backtracked out of); the bold line is the shortest path found by flood fill; the triangle is the robot and its heading.
- Wall
- Explored (wrong paths)
- Shortest path
- Goal
- Phase
- Ready
- Explored
- 1 / 144
- Exploration
- — moves
- Shortest path
- — moves
- Saved
- —
- Maze
- —
Keyboard: focus the maze, then use Space to run/pause, N to step, R to reset, M for a new maze, and F for full screen.
Controls
Pick a mode — Explore, then speed-run or Speed-run only — and press Start to watch it solve, or Step one move at a time. New maze builds a fresh random maze; Reset replays the same one. Three speeds, keyboard shortcuts, and Full screen keep the run controllable. Bold lines are the maze walls, the shaded corridors are everywhere the robot explored (dead ends included), and the bold line is the shortest path flood fill found.
Theory
A grid maze is cells joined by open edges, with walls between the rest. To find the shortest route a robot must first know the maze — so this lab runs two phases.
Explore. The robot walks the maze depth-first with a left-hand rule: at each junction it turns left if it can, otherwise straight, then right. When it hits a dead end it backtracks to the last junction and takes the next branch. Repeating that until every cell is visited maps the whole maze — including all the wrong paths it had to rule out.
Optimise. With the map complete, a flood fill assigns the goal a distance of zero and propagates increasing values outward through every open edge. Every cell now holds its distance to the goal, so the shortest path is simply this: from the start, always step to a neighbour one number lower. The robot speed-runs that path — far shorter than the wandering exploration.
Algorithm
- Explore the maze depth-first with a left-hand rule, backtracking out of every dead end, until all cells are visited.
- Record each open edge on both sides so the finished map knows every wall.
- Flood-fill from the goal: label it 0 and propagate increasing distances outward through every open edge.
- Read the shortest path off the field: from the start, repeatedly step to a neighbour whose distance is one lower.
- Speed-run that path to the goal.
- Compare the exploration length with the shortest-path length to see how much wandering the map saved.
Source code
Keep exploration, mapping, planning, and motion as separate functions:
// Phase 1 — map the whole maze, backtracking out of dead ends
exploreDepthFirst(map, robot); // left-hand rule until every cell is visited
// Phase 2 — flood-fill the finished map and run the shortest path
map.floodFill(goal); // goal = 0, distances ripple outward
for (Cell c = start; c != goal; c = map.lowerNeighbor(c))
robot.driveTo(c); // always step one distance lower
That boundary makes the same planner testable in this browser lab, on a desktop, and on the final ESP32 controller.
Assumptions and hardware differences
The lab makes mapping clean by removing the hard part of a real Micromouse: odometry. The simulated robot moves exactly one cell per step with no wheel slip, no gyro bias and no 90° turn error, and its three range sensors report walls perfectly with no false positives. A physical ESP32 build accumulates heading and distance error every move, must re-square against walls, and must filter ToF readings against sunlight and supply sag — which is why the precise turns tutorial and the flood-fill guide spend more time on motion than on planning. Use this simulator to learn why flood fill replans, not to prove your motion layer.
Circuit diagram
Use an external regulator sized for the motor stall current. The ESP32 and sensors need a stable logic rail even when motors reverse.
Hardware checklist
Components
- ESP32 development board
- Three time-of-flight or infrared distance sensors
- Two encoder-equipped geared motors
- Dual motor driver and regulated battery supply
- Compact differential-drive chassis
Explore the graph
Where this simulator is used
The projects, learning paths, and tutorials that build on this lab.
Continue building
Download resources
Use these on-page references while working through the project. Downloadable project bundles will be added only after their source and version are published.
Common questions
Frequently asked questions
Which maze algorithm is best for a first robot?
A depth-first exploration to map the maze, then a flood fill to find the shortest path. Flood fill is a strong teaching choice because every reachable cell receives a distance value and each move can be inspected. This simulator explores first, then flood-fills the completed map, so mapping and shortest-path planning both stay clear.
How does a maze robot find the shortest path?
It can't know the shortest path until it has seen the maze, so it explores first. This simulator, like a real Micromouse, uses two phases: an exploration run that walks every corridor and dead end to map the whole maze, then a fast run along the single shortest path that flood-filling the completed map reveals. The first phase is thorough and slow; the second is short and optimal.
How is flood fill different from BFS or A*?
BFS and A* plan over a graph you already know. Flood fill is designed for a maze you are still discovering: it assigns the goal distance zero and floods increasing values outward through every edge not known to be blocked, then recomputes whenever a newly sensed wall changes the map. The distance field itself encodes the route, so the robot just steps to any neighbor one value lower.
What is the difference between the explore and speed-run phases?
Exploring maps the maze: the robot walks depth-first into every corridor and dead end, backtracking out of the wrong ones, until it knows every wall — that is where you see it try all the wrong paths. Speed-running uses that finished map: a flood fill turns it into a distance field, and the robot follows the single shortest path from start to goal. Explore is slow and thorough; the speed-run is short and optimal.
What sensors does a Micromouse or maze robot need?
At minimum, distance sensors looking left, front, and right to classify the walls of the current cell, plus wheel encoders to move an accurate number of cells and square up turns. Time-of-flight or infrared distance sensors are common; encoders matter because wall sensing alone cannot tell you how far you have travelled.
Why use flood fill instead of wall following?
Wall following (always keep one hand on a wall) is simple but cannot reach a goal that is not attached to the outer wall, such as the centre of a Micromouse maze, and it rarely finds a short route. Flood fill builds a distance map of the whole known maze, so it can reach an interior goal and improve toward the shortest path as it explores.
Further reading
References
Authoritative sources for going deeper than this simulator's bounded educational model.