Home

Awesome

CoffeeScript Mazes

There are a lot of different maze algorithms out there, each with different properties, strengths, weaknesses, and interesting points. The aim of this project is to develop a library of these algorithms in a format that allows the inner structure and behavior of them to be studied and observed visually, by animating them and allowing students to step through them.

Installation

You'll need CoffeeScript installed. Once you've got that, you can run:

cake build

This will convert the CoffeeScript sources in the "src" directory, to Javascript files in the "lib" directory.

At this point you should be able to open the demo in examples/maze.html. (A possibly-out-of-date version of the demo can be seen here, if you want to get an idea of what csMazes can do.)

If you want to do a piecemeal installation of your own, you'll need at least these files, included in this order:

Further, the "widget.js" includes a script for easily embedding maze animations on your page; you just need to add the CSS definitions. (See examples/maze.html for the CSS definitions.)

Once you've included those files, you can include any of the algorithm-specific files you want.

Also, these files may be safely combined and minified, if you want to reduce everything to a single file.

Usage

Using the included widget, embedding a maze is as simple as this:

<script type="text/javascript">
  Maze.createWidget("Prim", 10, 10)
</script>

This would embed a 10x10 grid that will animate Prim's algorithm. You can also pass an optional object (hash) with properties to customize how the algorithm runs, or how the grid is displayed. These properties are supported:

Advanced Usage

If you're determined to do things the hard way, you can always instantiate the mazes yourself, setting up the callbacks and rendering things manually. To instantiate a maze:

var maze = new Maze(10, 10, Maze.Algorithms.Prim)

This would create a blank 10x10 grid that will generate a maze using Prim's algorithm. Mazes are generated either step-wise:

maze.step() // returns false when the maze is completed

Or they can be generated all at once:

maze.generate() // calls step() repeatedly until done

As with the widget helper, the maze constructor accepts an optional final parameter, an object, whose properties can be used to customize how the maze is built. The following properties are understood (and have the same meaning as their counterparts in the widget helper):

To indicate interest in the progress of the maze, you can use the onUpdate and onEvent methods to register callbacks that will be invoked. The onUpdate callback is triggered every time a cell is changed. The onEvent callback is triggered whenever an algorithm-dependent "event" occurs (e.g. the recursive backtracker hits a dead-end and has to backtrack). Both callbacks accept three parameters: the maze object that caused the callback, and the x and y coordinates that are relevant.

maze.onUpdate(function(m, x, y) {
  // update the display, etc.
});

maze.onEvent(function(m, x, y) {
  // pause the animation, etc.
});

License

csMazes is written by Jamis Buck (jamis@jamisbuck.org) and is made available in the public domain. Do with it what you will.

But please prefer good over evil.