npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

maze-generation

v3.1.2

Published

A package to generate mazes using the depth first or hunt and kill algorithm. Mazes can be generated with seed values for reproducibility

Downloads

24

Readme

maze-generation

npm NPM CircleCI npm bundle size

An npm package used to generate mazes with a specified width and height.

Mazes can be generated using either recursive backtracker (Depth First) or hunt and kill algorithms. See below for details of these algorithms.

Additionally mazes can be generated using seeds, which allows for reproducible maze generation.

Contributing

Please view our CONTRIBUTING.md file for information on how to contribute, report issues and request features.

Changelog and versioning

Please view our CHANGELOG.md file for information on our updates.

We use SemVer for versioning.

Using maze-generation

Installing

Ensure Node.js is installed on your machine.

Run npm i maze-generation

Usage

Generation

Add an options object to your code, with the following to fields:

  • REQUIRED width and height should be replaced by ints corresponding to how wide and tall you want your maze.
  • OPTIONAL seed should be replaced by an int or string and will be used as the seed for the random number generator. Defaults to a random number.
  • OPTIONAL algorithm should be: 'DEPTHFIRST' or 'HUNTANDKILL'. Defaults to 'DEPTHFIRST'.

To stop heap out of memory errors, the maximum allowed height and width is 3000.

const mazegeneration = require('maze-generation');

const options = {
    width: 10,
    height: 10,
    seed: 12345,
    algorithm: 'HUNTANDKILL'
}

// Generate a maze
const generatedMaze = mazegeneration(options);

To get the string representation of the generated maze write:

const stringRepresentation = generatedMaze.toString();
console.log(stringRepresenation);

Example output:

 _ _ _ _ _ _ _ _ _ _
|   |  _   _ _   _  |
| |_|_  |_ _  | |  _|
|_ _ _ _|   | |_  | |
|    _  | |_ _|  _ _|
| | |_  |_  | |_ _  |
|_|_  | |  _|_ _  | |
|  _ _|_|_ _  | | | |
|  _ _    | |_  |  _|
| |_  | |_ _|  _| | |
|_ _ _|_ _ _ _|_ _ _|

To get the JSON representation of the generated maze write:

let JSONRepresentation = generatedMaze.toJSON();

The outputed JSON object has the following structure (example is a 3 by 3 cell):

    {
        rows: [
            [[Object],[Object],[Object]],
            [[Object],[Object],[Object]],
            [[Object],[Object],[Object]],
        ]
    };

Where each object is a Cell object, which as the following JSON structure:

    {  
        left: bool,
        right: bool, 
        up: bool, 
        down: bool, 
        visited: bool
    }

The left,right,up,down fields correspond to if the wall exists in that direction. The visited field corresponds to if the cell has been visited. This should be marked as true for all completed mazes.

Algorithms
Depth First
   CURRENT_CELL = random cell
   Create an empty CELL_STACK
   While CURRENT_CELL has unvisited neighbour:
    Select random unvisited neighbour
    Remove walls between the two
    CURRENT_CELL = that random unvisited neighbour
    Mark CURRENT_CELL as visited and push it to the CELL_STACK
    IF CURRENT_CELL has no unvisited neighbour:
      CURRENT_CELL = CELL_STACK.pop()
Hunt And Kill
  Choose a random starting cell and set that to CURRENT_CELL
  Perform a randomised walk from CURRENT_CELL
  WHILE there's unvisited cells with adjacent visited cells: 
    Find the first unvisited cell with adjacent visited cells and set that cell as the CURRENT_CELL
    Remove the wall between the CURRENT_CELL and a random visited neighbour
    Perform a randomised walk from CURRENT_CELL
  • More information can be found on Jamis Buck's blog.
  • This algorithm hunts for the "first unvisited cell with adjacent visited cells". First is found by searching each cell on the top row, before moving to the next row.

Solver

The generated maze object contains a function called generateSolution which can be used to create a path through the maze. It takes two parameters, a start cell and a goal cell.

Like the Maze object, the solution object has two methods which can be used for printing the output: toString() and toJSON().

toJSON() returns an array of cells, which represent the path taken from the start to the goal.

// example usage

const options = {
    width: 5,
    height: 5,
    seed: 12345,
    algorithm: 'HUNTANDKILL'
}

const generatedMaze = mazegeneration(options);
console.log(generatedMaze.toString());
//  _ _ _ _ _
// | |   |   |
// |  _| | |_|
// |_  |_ _| |
// | | |_    |
// |_ _ _ _|_|

const start = {
    row: 0,
    column: 0
};
const goal = {
    row: 4,
    column: 4
};
const solution = generatedMaze.generateSolution(start, goal);
console.log(solution.toString());
//  _ _ _ _ _
// |S|   |   |
// |↓ _| | |_|
// |↳ ↴|_ _| |
// | |↓|_ ↱ ↴|
// |_ ↳ → ⇗|G|
//    ¯ ¯ ¯ ¯

console.log(solution.toJSON())
// [
//   { row: 0, column: 0 },
//   { row: 1, column: 0 },
//   { row: 2, column: 0 },
//   { row: 2, column: 1 },
//   { row: 3, column: 1 },
//   { row: 4, column: 1 },
//   { row: 4, column: 2 },
//   { row: 4, column: 3 },
//   { row: 3, column: 3 },
//   { row: 3, column: 4 },
//   { row: 4, column: 4 }
// ]

Support

  • Donations to support this project - or even just to say thanks - are welcome here. Please donate via my Kofi page.

Acknowledgments