Class: Maze

Maze~Maze()

2D Grid of Cell objects containing maze data generated using explicit recursive depth-first search.

Explanation from Wikipedia as follows:

A disadvantage of the recursive implicit stack approach is a large depth of recursion. In the worst case, the routine may need to recur on every cell of the area being processed, which may exceed the maximum recursion stack depth in many environments. As a solution, the same backtracking method can be implemented with an explicit stack, which is usually allowed to grow much bigger with no harm.

Wikipedia

Constructor

new Maze()

Source:

Members

columns

Properties:
Name Type Description
columns int

Maximum number of columns in the maze.

Source:

generated :Object

2D Mapping of Cells containing previously generated maze data. Keyed by [row][column].

Type:
  • Object
Source:

ordered :Cell|Array

1D List of cells that keeps the order of the cells in which they were chosen during generation.

Type:
Source:

orderedSolution :Cell|Array

1D List of cells that keeps the ordered solution of the cells in which they were chosen during generation.

Type:
Source:

rows

Properties:
Name Type Description
rows int

Maximum number of rows in the maze.

Source:

solution :Object

2D Mapping of Cells containing the solved maze data. Keyed by [row][column].

Type:
  • Object
Source:

Methods

generate(startRow, startColumn, endRow, endColumn) → {Object|Object|Array}

Main generator method for generating the maze. Uses row and column instead of GridIndex to have easier implementation for consumers.

Parameters:
Name Type Description
startRow int

Starting row index of the maze.

startColumn int

Starting column index of the maze.

endRow int

End row index of the maze.

endColumn int

End column index of the maze.

Source:
Returns:

The generated maze data and the solution data, respectively. Both keyed by [row][column]. The first cell is always the cell chosen by index at (row, column).

Type
Object | Object | Array
Example
// Algorithm (Recursive depth-first search with explicit stack):
1. Choose the initial cell, mark it as visited and push it to the stack.
2. While the stack is not empty.
     1. Pop a cell from the stack and make it a current cell.
     2. If the current cell has any neighbours which have not been visited.
         1. Push the current cell to the stack.
         2. Choose one of the unvisited neighbours.
         3. Remove the wall between the current cell and the chosen cell.
         4. Mark the chosen cell as visited and push it to the stack.

getUnvisitedCells(indices) → {Cell|Wall|Array}

Given a mapping of Walls to Index:

  1. First determine whether the index is valid.
  2. If the index is valid and the cell at that index is unvisited, push it to the unvisited neighbors array.
Parameters:
Name Type Description
indices Map.<Wall, GridIndex>

A mapping of Walls to Index.

Source:
Returns:

A tuple-like array of length 2 with the unvisited cell as the first parameter and the respective wall as the second parameter.

Type
Cell | Wall | Array
Example
Maze.getUnvisitedCells(Cell.getNeighborIndices()) // Gets all unvisited neighbors

reset()

Reset the maze.

Source:

validCellIndex(index) → {boolean}

Determines whether a given 2D Index is out of range in the cells array.

Parameters:
Name Type Description
index GridIndex

The 2D index to check for.

Source:
Returns:

If the given index is in range.

Type
boolean