Détail du package

maze-generation

JRIngram16MIT4.0.0

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

maze, generator, maze-gen, maze-gen

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

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

4.0.0 - 2025-02-22

Added

  • TypeScript support
  • Replace semistandard with eslint and prettier
  • Change to using ESM over CommonJs for modules.

Dependencies

  • Bump @babel/traverse from 7.21.2 to 7.23.2.
  • Bump husky from 8.0.1 to 9.1.7
  • Bump jest from 29.1.2 to 29.7.0.

3.1.2 - 2023-01-25

Dependencies

  • Bump json5 from 2.2.1 to 2.2.3.

3.1.1 - 2022-10-11

Added

  • Maze Solver accessible via the generateSolution function on the returned maze.

Dependencies

  • Bump jest from 27.5.1 to 29.1.2
  • Bump husky from 7.0.4 to 8.0.1
  • Bump minimist from 1.2.5 to 1.2.6
  • Bump semistandard from 16.0.0 to 16.0.1.

3.0.1 - 2022-02-26

Added

  • Donation Link

    Changes

  • Bump jest from 26.6.3 to 27.5.1.
  • Bump tmpl from 1.0.4 to 1.0.5.
  • Bump browserslist from 4.16.3 to 4.19.3.
  • Bump ws from 7.4.4 to 7.5.7.
  • Bump hosted-git-info from 2.8.8 to 2.8.9.
  • Bump path-parse from 1.0.6 to 1.0.7.

3.0.0 - 2021-05-18

Changes

  • Package now takes an options object as its parameter (which contains height, width, seed and algorithm) rather than taking 4 individual parameters.
  • Bump from semi-standard 14.2.3 to 16.0.0.
  • Bump jest from 26.5.2 to 26.6.3.
  • Test coverage improved.
  • Bump prando from 5.1.2 to 6.0.1.

2.1.0 - 2020-10-10

Added

  • Additional error handling
  • New tests

Changes

  • New max dimensions, to stop heap out of memory errors
  • Minor documentation fixes

2.0.0 - 2020-04-21

Added

  • Ability to use a seeded random number generator to generate a maze.
  • Feature request template.
  • .npmignore.
  • Hunt and kill algorithm.
  • Created a CONTRIBUTING.md file

Changes

  • String representation of the maze to be more clear
  • Bump jest from 25.1.0 to 25.4.0
  • Improved bug report template.
  • Order of parameters for generateMaze.
  • Tidied tests and remove unused functions.
  • Restructured README:
    • Added links to changelog and contributing
    • Added algorithm descriptions
  • Moved linting and unit tests to distinct jobs in circleci

Security

  • Bump acorn from 6.4.0 to 6.4.1

1.0.2 - 2020-02-24

Fixed

  • Remove duplicate badge in README
  • Add link to release version for v1.0.1
  • Update README to reference name of the package in NPM

1.0.1 - 2020-02-23

Added

  • Badges to README

Fixed

  • Fixed issues with README

1.0.0 - 2020-02-23

Added

  • Ability to generate maze using the Recursive Backtracker / Depth First Algorithm.
  • Ability to get string representation of the maze.
  • Ability to get JSON object representation of the maze.