Breadth-First Search for Game AI: Try an Interactive Maze
Edit a maze, watch a bot explore it and learn why breadth-first search finds a shortest path. Includes a small JavaScript example and a free interactive demo.
By Duangduean Studios

An enemy that takes the right turn around a wall can look clever. It does not necessarily need a language model, training data or a paid AI service. Sometimes it needs a grid, a queue and a rule for remembering where it has already been.
Breadth-first search, usually shortened to BFS, is a good place to start. On a grid where every allowed move has the same cost, it can find a route with the fewest moves. Our free interactive AI Lab makes that process visible.
Try the maze before reading the code
Open the pathfinding experiment. The board has eight rows and eight columns. S marks the start and G marks the goal.
- Press Run search with the default walls. Blue cells show the explored area; the orange route appears when the search finishes.
- Click an open square to add a wall, then run the search again.
- Put a wall at both exits from the start to test a maze with no solution.
- Use Clear walls to compare the result with an empty board.
- Use Reset maze to restore the original layout.
The empty board takes 14 moves from the top-left to the bottom-right: seven across and seven down. Diagonal movement is disabled. Moving walls can create a longer route or make the goal unreachable.
The queue is the important part
BFS starts by putting the start cell in a queue. It removes the oldest queued cell, examines its allowed neighbours and adds any unvisited neighbours to the back of the queue.
That order makes exploration spread in layers: cells one move away, then two moves away, then three. When the goal is reached, no undiscovered route can use fewer equal-cost moves.
Two rules keep the search manageable:
- Mark a cell as discovered when it is added to the queue. Otherwise several neighbours may add the same cell repeatedly.
- Store the previous cell for each discovered cell. That lets you reconstruct the final route instead of only knowing that the goal was reached.
Here is a compact, engine-independent JavaScript example:
function shortestPath(start, goal, neighbours) {
const queue = [start];
const previous = new Map([[start, null]]);
for (let head = 0; head < queue.length; head++) {
const cell = queue[head];
if (cell === goal) {
const path = [];
for (let at = goal; at !== null; at = previous.get(at)) {
path.unshift(at);
}
return path;
}
for (const next of neighbours(cell)) {
if (!previous.has(next)) {
previous.set(next, cell);
queue.push(next);
}
}
}
return []; // The goal is unreachable.
}
The neighbours function supplies your game's rules. In our lab it returns only in-bounds cells immediately above, below, left or right, excluding walls. A platform game would need different rules for jumps, drops and one-way platforms.
Explored cells are not the final path
A bot can inspect a cell and still choose another route. The blue area in the demo is therefore not wasted movement by an invisible character: it is the search space the algorithm considered.
The orange path is reconstructed by following the stored previous cells backwards from the goal. If several shortest routes exist, neighbour order determines which one is found first. That does not make the other routes longer or incorrect.
When BFS is the wrong tool
The fewest moves and the lowest travel cost are only the same when each move costs equally. If mud takes three times longer than a road, ordinary BFS does not account for that difference. A weighted search is the next concept to investigate.
For a much larger map, a goal-directed search may also inspect fewer cells. In Godot, the 2D navigation overview explains grid/graph options such as AStar2D and AStarGrid2D alongside navigation-mesh systems. Pick the representation that fits your movement, rather than assuming every game needs a tile grid.
Finding a route is only half the enemy brain
Pathfinding answers where an enemy can go. It does not decide whether the enemy should chase, attack, search or retreat.
The second AI Lab experiment lets you change health, distance and visibility. It then shows the resulting rule-based decision. Try health below 25% with a nearby visible player, then hide the player and compare the result.
Both experiments run locally in your browser. They are demonstrations of game AI algorithms and decision rules, not examples of a neural model learning while you play.
Want to build a tiny game around these ideas? Start with the free Godot pickup project guide, then add one enemy with a simple movement goal. You can also play the studio's games and watch which decisions make an opponent feel readable rather than random.
Cover illustration: an original grid diagram showing explored cells and a route from start to goal.
Frequently asked questions
What is breadth-first search (BFS)?
A search that puts the start cell in a queue, repeatedly takes the oldest cell, and adds its unvisited neighbours to the back. Exploration spreads in layers, so on a grid where every move costs the same, BFS finds a route with the fewest moves.
Do game enemies need machine learning to find a path?
Not necessarily. A grid, a queue and a record of visited cells are often enough for an enemy to take the right turn around a wall.


