Skip to content

Instantly share code, notes, and snippets.

@jamis
jamis / growing-tree.rb
Created December 31, 2010 05:23
An implementation of the "Growing Tree" algorithm for maze generation.
# --------------------------------------------------------------------
# An implementation of the "Growing Tree" algorithm. This one is
# notable for it's ability to become nearly identical to Prim's
# algorithm, or the Recursive Backtracking algorithm, depending on
# how the cells are removed from the list that aggregates as the
# algorithm runs.
#
# This script allows you to play with those settings by specifying
# the mode after the width and height parameters, as "random" (pull
# the cell from list at random), "newest" (pull the newest cell),
@jamis
jamis / recursive-division.rb
Created January 1, 2011 03:03
An implementation of the "Recursive Division" algorithm for maze generation.
# --------------------------------------------------------------------
# An implementation of the "Recursive Division" algorithm. This is a
# kind of fractal maze algorithm, recursively dividing the maze into
# smaller and smaller cells. This algorithm stops at the integer
# boundary, but theoretically the algorithm could continue infinitely
# to smaller and smaller scales.
#
# Note that this algorithm is implemented as a "wall adder", rather
# than a "passage carver", so the meaning of the bits in each cell
# is reversed: instead of the S bit (for instance) meaning "a passage
@jamis
jamis / binary-tree.rb
Created January 1, 2011 03:33
An implementation of the "Binary Tree" algorithm for maze generation.
# --------------------------------------------------------------------
# An implementation of the "Binary Tree" algorithm. This is perhaps
# the simplest of the maze generation algorithms to implement, and the
# fastest to run, but it creates heavily biased mazes.
#
# It is novel in that it can operate without any state at all; it only
# needs to look at the current cell, without regard for the rest of
# the maze (or even the rest of the row). Thus, like Eller's algorithm
# it can be used to generate mazes of infinite size.
# --------------------------------------------------------------------
@jamis
jamis / sidewinder.rb
Created January 1, 2011 04:12
An implementation of the Sidewinder algorithm for maze generation.
# --------------------------------------------------------------------
# An implementation of the Sidewinder algorithm for maze generation.
# This algorithm is kind of a cross between the trivial Binary Tree
# algorithm, and Eller's algorithm. Like the Binary Tree algorithm,
# the result is biased (but not as heavily).
#
# Because the Sidewinder algorithm only needs to consider the current
# row, it can be used (like the Binary Tree and Eller's algorithms)
# to generate infinitely large mazes.
# --------------------------------------------------------------------
@jamis
jamis / weave.rb
Created March 4, 2011 05:23
A "weave" maze implementation, using the Growing Tree maze algorithm.
# --------------------------------------------------------------------
# An implementation of a "weave" maze (with over/under passages),
# using the Growing Tree maze generation algorithm.
# --------------------------------------------------------------------
# --------------------------------------------------------------------
# Helper data and methods
# --------------------------------------------------------------------
N, S, E, W, U = 0x01, 0x02, 0x04, 0x08, 0x10
@jamis
jamis / kruskals-weave.rb
Created March 5, 2011 05:11
Another approach to generating weave mazes, described by Robin Houston. Scatter the over/under passages randomly across the blank maze, and then generate the maze around them. Kruskal's algorithm is particularly well-suited to this approach.
# --------------------------------------------------------------------
# An implementation of a "weave" maze generator. Weave mazes are those
# with passages that pass both over and under other passages. The
# technique used in this program was described to me by Robin Houston,
# and works by first decorating the blank grid with the over/under
# crossings, and then using Kruskal's algorithm to fill out the rest
# of the grid. (Kruskal's is very well-suited to this approach, since
# it treats the cells as separate sets and joins them together.)
# --------------------------------------------------------------------
# NOTE: the display routine used in this script requires a terminal
@jamis
jamis / kruskal-weave-2.rb
Created March 18, 2011 16:05
A possible improvement on the kruskals-weave.rb algorithm that allows fully adjacent crossings.
# --------------------------------------------------------------------
# An implementation of a "weave" maze generator. Weave mazes are those
# with passages that pass both over and under other passages. The
# technique used in this program was described to me by Robin Houston,
# and works by first decorating the blank grid with the over/under
# crossings, and then using Kruskal's algorithm to fill out the rest
# of the grid. (Kruskal's is very well-suited to this approach, since
# it treats the cells as separate sets and joins them together.)
# --------------------------------------------------------------------
# NOTE: the display routine used in this script requires a terminal
@jamis
jamis / cube-maze.rb
Created May 13, 2011 20:59
Generate a maze that spans all six faces of a cube.
###########################################################################
# cube-maze.rb
# by Jamis Buck (jamis@jamisbuck.org)
# -------------------------------------------------------------------------
# This script generates a PNG image of all six faces of a cube, covered
# with the passages of a maze that wraps across all the faces.
#
# The movements between faces are computed using the WRAPS constant, which
# tells which face lies in each direction from any given face, as well as
# how the coordinates should be transformed when moving from face to face.
@jamis
jamis / lap_timer.rb
Created June 6, 2011 22:02
A simple helper for timing sequences of code.
# Easy to use:
#
# timer = LapTimer.new
# # some task that might take awhile
# timer.mark :step2
# # another task that needs timing
# timer.mark :step3
# # and another task to time
# timer.report
#
@jamis
jamis / cap-transcriber.rb
Created July 25, 2011 14:57
Capistrano Transcriber: a wrapper script for Capistrano that writes session transcripts to a timestamped file.
#!/usr/bin/env ruby
require 'capistrano/cli'
class TranscriptLogger
module TranscriptLoggerMetadata
attr_accessor :tx_logger_metadata
def self.apply_to(object, options={})
object.extend(self) unless object.respond_to?(:tx_logger_metadata)