The Puzzle and Comments

Maze puzzles are easy to create. We provide custom AS3 classes that randomly generate and render the so called perfect mazes; that is, mazes that have exactly one solution. At a click of a button you can switch from simple examples to very complex ones. We use an implementation of the classic maze-generating algorithm called recursive backtracker.

Click the screen shot below or this link to open the puzzle in a new window:

The particular rendition of perfect maze puzzle presented here uses keyboard interface and it is not well-suited for Android phones. A version in the AIR for Android format is coming soon to flashandmath.com.

Download the Complete AS3 Code

The following zip package contains complete code and the custom AS3 classes behind the puzzle:

Custom AS3 Classes

There are three AS3 classes behind the application and a few lines of Timeline code.

MazeDisplay Class

The MazeDisplay class extends the Sprite class. This is the only class that you need to use when building your maze applications. The other two classes, MazeDataGenerator and MazeCell are used internally by MazeDisplay. The class' constructor evoked with the keyword 'new' takes no parameters:

var mazeDisp:MazeDisplay = new MazeDisplay();

The method that randomly creates a maze and renders it onscreen is the 'createMaze' method. Before you evoke the method you should add event listener that listens to the custom event MazeDisplay.MAZE_READY dispatched by the class when creation of a maze is over:

mazeDisp.addEventListener(MazeDisplay.MAZE_READY,readyHandler);

Then you evoke 'createdMaze' method. The method takes three parameters the number of rows, the number of columns, and the size of square cells in the maze. For example:

mazeDisp.createMaze(14,25,30);

These values correspond to the easier version of the puzzle in our application. The harder version has more columns and rows.

function readyHandler(e:Event):void {
 
        .......................controls are made visible........
 
  }

The MazeDisplay class uses MazeDataGenerator and MazeCell to create and store data about a randomly generated maze. The hard work and the implementation of Recursive Backtracker is done within the MazeDataGenerator class.

In a nutshell, the algorithm begins with a grid of (number of rows)*(number of columns) cells. At first all cells have all they walls up. We start with a random cell, mark it as 'visited' and look at its non-visited neighbors. We randomly choose one neighbor and knock down the wall between the two cells . The neighbor is added to the 'visited cells' and becomes our next starting point. We look at its neighbors, etc. If we hit a cell whose all neighbors have been visited, we pop back the last one from the array of visited cells and see if that one has a non-visited neighbor. Step-by-step we create a perfect maze.

You can find precise description of the algorithm and other maze-generating algorithms at, for example: Maze generation algorithm. Or you can look at the code in MazeDataGenerator.as included in the zip package.

Enjoy the puzzle!

Back to Advanced Tutorials              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.