JLION.COM
JLION.COM - A Java Applet for randomly generating mazes.
2/14/03 Games, JAVA

I've always had an interest in mazes, and in drawing them. Many years ago, I'd come across a book called "More Basic Computer Game", edited by David H. Ahl. Among the book's program listings was a little program that generated a random maze, with a single entrance and exit, and one way through. I thought the program was nifty, though riddled with GOTOs as it was, I found it incomprehensible.


Show a:

I'd always wondered how it worked, though. When I decided that I wanted to try creating a java applet that allowed one to scroll around using scrollbars or a mouse, rather than just displaying a static image or text, I decided to create a scrolling maze applet.

Displaying a moveable window into a larger hidden bitmap was easy. I did have some trouble figuring out how to use java's GridBagConstraints object to lay out the scroll bars and buttons, but after working with it for a while it makes sense-it's just different than windows as locations are relative to each other.

I found that I spent a lot more time getting the maze app to work. I found quite a lot of information on the web about drawing mazes.

The typical way to draw a maze, and the way I used, is to create a stack and an array the same size as the maze. I pick a random starting point in the top row of the maze. I then move randomly through the maze, pushing each location that I visit into the stack, and then marking it as visited. If I visit a cell that's been previously visited, I pop the last location from the stack and start over from there. I do this until all cells in the maze have been visited.


Once I've created the maze, I then relocate to the random starting point at the top of the maze. This time I'm looking for a path to the bottom of the maze, so I use a different stack. I pick a random direction and go until I find either a dead-end or a junction. If I find a junction, I push it onto the stack and then pick a new random direction. If I find a dead-end, I pop the last junction from the stack and start over. I continue until I work my way to the bottom of the maze. Once there, I mark the cell that I've arrived at as the exit point.

Once I got the maze algorithm to work, I couldn't resist doing a bit of tweaking, so I added a routine to show the solution path through the maze and code to allow users to work their own way through the maze by clicking on cells with the mouse.

I also added code to show a bitmap behind the maze.

To pick your way through the maze, look for the red dot at the top of the maze. This is the starting location. Use the mouse to left-click on any cell adjacent to the starting point to begin finding your way through the maze. You can scroll the maze either by using the scroll bars or by holding down the right mouse-button and dragging the mouse.

To see the solution path, click on the "solve" button. To keep the current maze but start over, click on the "reset" button, and to generate a brand new maze, click on the "new" button.