This is a maze generator and solver coded in Java with Graphics too. A single class is responsible for both the generation of the maze and the solving. Both the generator and the solver are based on Depth-first search algorithms, particularly Backtracking algorithms. My implementation is iterative and uses a stack to backtrack efficiently. Images of different sizes of mazes below.
Jump into the code:
- MazeViewer.java: A very short class that just takes the maze and draws it. It also handles keyboard input.
- Maze.java: This class contains the maze and it's properties. The maze is generated and solved when you in instantiate it.
Instructions:
- Press the 'r' key to generate a new solved maze
- If you want to change the dimensions of the maze itself, you can also tweak the 3rd parameter in the constructor call of the Maze class in MazeViewer.java, which is the size of a single cell in pixels.
maze = new Maze(WIDTH, HEIGHT, 10); // size of 10 pixels for a cell
- To change the dimensions just go into MazeViewer.java and change the following lines of code to anything you want. They are dimensions in pixels, the code handles the size of the maze automatically.
private static final int WIDTH = 1010; // width in pixels private static final int HEIGHT = 1010; // height in pixels
A few examples:
50x50
100x60
190x100