• Stars
    star
    391
  • Rank 110,003 (Top 3 %)
  • Language
    CoffeeScript
  • Created almost 14 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Maze algorithms implemented in CoffeeScript, with an eye toward demonstrating how the algorithms work by animating them.

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:

  • mersenne.js
  • maze.js

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:

  • id : used to set the id of the created HTML elements. If not specified, the lower-cased algorithm name will be used.
  • class : the HTML class attribute to add to the outermost generated div. This is in addition to any other classes that the widget itself assigns (e.g. "maze").
  • input : data that should be passed to the maze object upon creation. This should be either a string, in which case it is passed directly to the maze constructor, or a function, in which case it is invoked first and the return value used as the value passed to the maze. The actual format of the string is dependent on the algorithm used.
  • interval : the delay (in milliseconds) between steps when the maze is in "run" mode. Defaults to 50ms.
  • wallwise : a boolean value indicating whether the maze is to be displayed as a passage carver (false) or a wall adder (true). The meaning of the wall queries is inverted when wallwise is true. Most mazes need to have wallwise set to false (the default), but the RecursiveDivision algorithm is a wall adder and needs to be rendered with wallwise set to true.
  • seed : an integer value to use as a seed for the random number generated. Using the same seed for different animation runs (where the algorithm and dimensions are otherwise the same) will always result in the same maze being generated.
  • rng : the random number generator object to use to generate numbers. You'll almost never need to use this; but it could be handy if you want to generate a series of mazes with the same original seed. If used, this should be an instance of MersenneTwister (defined in mersenne.coffee), or should at least conform to the same interface.
  • padded : if true, adds space around each cell. The default is false.
  • weave : if true, generates a "weave" maze (where passages move over and under other passages). This is not supported by all algorithms. For best results, use with padded set to true.
  • weaveMode : either, "onePhase" (the default), or "twoPhase". Only Kruskal's algorithm currently supports this setting.
  • weaveDensity : A number between 0 and 100 (default 80), with 100 meaning "maximum" density". Only used when weaveMode is set to "twoPhase".

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):

  • input : a string used as input to the algorithm, which can be used to customize its behavior. Not all algorithms use this parameter.
  • seed
  • rng
  • weave
  • weaveMode
  • weaveDensity

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 ([email protected]) and is made available in the public domain. Do with it what you will.

But please prefer good over evil.

More Repositories

1

bulk_insert

Efficient bulk inserts with ActiveRecord
Ruby
818
star
2

bucketwise

ATTENTION: This project is no longer being updated. If you're still interested, feel free to read on... "A web-based personal finance manager with a focus on non-OCD budgeting and avoiding credit card debt"
Ruby
457
star
3

castaway

System for building screencasts and video presentations
Ruby
306
star
4

fuzzyfinder_textmate

A vim script that extends the fuzzyfinder plugin to support TextMate style file searches (e.g. cmd-T) (Unmaintained now, see http://weblog.jamisbuck.org/2009/1/28/the-future-of-fuzzyfinder-textmate)
Vim Script
216
star
5

theseus

A very flexible random maze generator, solver, and renderer for Ruby
Ruby
178
star
6

query-composer

A library for composing complex SQL queries by defining their subcomponents and the dependencies between them.
Ruby
166
star
7

fuzzy_file_finder

A (slightly enhanced) implementation of TextMate's cmd-T lookup functionality, in Ruby, for embedding in other projects
Ruby
143
star
8

wordsearch

A word-search puzzle generator
Ruby
76
star
9

safe_mass_assignment

ActiveRecord plugin for allowing (careful) mass assignment of protected attributes, separate from values provided via users of your application.
Ruby
55
star
10

net-ssh-multi

SSH connection multiplexing: execute commands simultaneously on multiple hosts via SSH
Ruby
44
star
11

sqlpp

A simplistic SQL parser and pretty-printer
Ruby
42
star
12

impose

A utility and library for imposition -- arranging pages on a sheet of paper for optimal printing
Ruby
37
star
13

dnd-dungeon

A random maze generator in C, with a CGI front-end for generating random dungeons for D&D, 3rd edition
35
star
14

kaleidoscope

Generate uniform tilings (tesselations) of a plane using Wythoff constructions. Not as hard (or scary) as it sounds!
Ruby
32
star
15

net-ssh-shell

NOTE: this repository is no longer actively maintained. Please go to the actively maintained repository, here: https://github.com/mitchellh/net-ssh-shell. Net::SSH::Shell is a net-ssh extension library that provides an API for programmatically interacting with a login shell
Ruby
26
star
16

ifrb

Interactive Fiction for Interactive Ruby
Ruby
23
star
17

mod_reproxy

A module for Apache 2 that implements support for the X-Reproxy-Url response header
C
23
star
18

net-ssh-gateway

THIS REPOSITORY IS NO LONGER MAINTAINED. Please see https://github.com/net-ssh/net-ssh-gateway for the currently maintained version. Thanks! -- A gateway class for tunneling connections via SSH over a forwarded port.
Ruby
23
star
19

amazing-desktops

A simple utility for generating random abstract images (using mazes) for use as desktop wallpaper.
C
21
star
20

MazeMaker

An implementation of grid layouts and maze algorithms, in Swift
Swift
20
star
21

logic-engine

Prolog-inspired logic engine in Ruby, with backtracking
16
star
22

rtc-ocaml

"The Ray Tracer Challenge" (http://www.raytracerchallenge.com) implemented in OCaml
OCaml
15
star
23

code_slide

Generate PDF/PNG slides from source code
Ruby
12
star
24

curves

A library for interpolating various curves (bezier, cubic hermite, etc.)
Ruby
12
star
25

mazoo

An HTML5 game to test your maze navigation skills!
JavaScript
11
star
26

zing

Framework for playful maze generation (from "Twisty Little Passages" presentation at MWRC 2015)
Ruby
11
star
27

process_roulette

A silly little game that could mess up your machine pretty badly (please use a VM!)
Ruby
9
star
28

chaussettes

A thin wrapper around the sox audio manipulation utility
Ruby
9
star
29

dnd-npc

A random NPC generator for D&D 3rd edition, written in C. Includes CGI and console interfaces.
C
8
star
30

truth

A utility for displaying a truth table for an expression
Ruby
8
star
31

celtic_knot

A library for generating Celtic Knotwork designs from graphs
Ruby
8
star
32

lindy

An L-system parser and interpreter
Ruby
7
star
33

dnd-util

An encapsulation (in C) of the core logic and data of D&D, 3rd edition.
6
star
34

KSP-RealSolarSystem-Bundler

Program for bundling all needed dependencies for the "Real Solar System" mod, for Kerbal Space Program.
Ruby
6
star
35

ekawada-web

A rails application for recording, comparing, and researching string figures
Ruby
6
star
36

weekly-challenges

My submissions for the weekly programming challenges (https://medium.com/@jamis/weekly-programming-challenge-1-55b63b9d2a1)
Ruby
6
star
37

scruffy-labrador

A flexible JavaScript implementation of a grid/graph, and some maze generation algorithms
JavaScript
6
star
38

artifex

A D&D4e NPC generator
CoffeeScript
5
star
39

jamis.github.io

Basil & Fabian - A Wizard & His Man
CoffeeScript
5
star
40

hercule

A logic puzzle for PalmOS (historical interest only, mostly, unless you have a really old device)
C
5
star
41

dnd-templates

A templating system written in C. (Deprecated, obsolete, etc!)
4
star
42

sqlite-ruby

bindings for the SQLite 2.x embedded database
Ruby
4
star
43

korean-proverbs

Translations of Korean proverbs
4
star
44

runeo

An ActiveRecord-inspired wrapper for the Neo4j REST API
Ruby
4
star
45

dnd-writetem

A templating system written in C, with a stream wrapper system. (Deprecated, obsolete, etc!)
3
star
46

hangul-tools

Romanize Korean text
Ruby
3
star
47

piece-by-piece

An evidence-oriented genealogical research database, inspired by the GENTECH data model
CoffeeScript
3
star
48

tinker

CYOA-style game system inspired by Sarah Allen's "pie" project.
Ruby
3
star
49

strings2go

A string figure collection for the iPhone
Ruby
2
star
50

kincaid

A DSL for creating dungeon maps for tabletop RPG's
Ruby
2
star
51

test_session_manager

Allow tests for Rails applications to inject session data (including flash) into test requests
Ruby
1
star
52

buckblog

The Buckblog -- assorted ramblings by Jamis Buck -- http://weblog.jamisbuck.org
HTML
1
star
53

derring-do

The over-eager progress monitor for Ruby.
Ruby
1
star
54

taleswapper

A home for creating and sharing stories, with an emphasis on role-playing games
Ruby
1
star