Maze generation with recursive backtracking, solved with breadth-first search, animated in Swing. No third-party dependencies.
Needs JDK 21 or newer and Maven.
mvn compile
java -cp target/classes io.github.abdrahmanayach.maze.MainOr open the folder in IntelliJ and run Main.
Recursive backtracking builds the maze. Starting from a grid with every wall intact, it carves a random depth-first path, backtracking whenever the current cell has no unvisited neighbours. The result is a perfect maze: no loops, no unreachable cells, exactly one route between any two of them.
Breadth-first search solves it. Each cell records the one it was reached from, so the shortest path falls out of walking those links back from the exit. The solver also returns the order cells were dequeued, which is what the panel animates.
| File | What it does |
|---|---|
Maze.java |
The grid and its walls, plus the generator |
Solver.java |
Breadth-first search, returns the search order and the path |
MazePanel.java |
Draws it, and a Timer reveals the search one cell at a time |
Main.java |
Window and the two buttons |
