Maze#

class mazely.Utilities#

A class to perform maze-related utility functions.

save_grid(grid, file_path, cell_size=15, line_width=2)#

Save a maze as an SVG file.

Parameters:
  • grid (ndarray) – A two-dimensional array of cells representing a rectangular maze.

  • file_path (str) – A path wherein the SVG file is saved.

  • cell_size (int) – The size of each cell in pixels.

  • line_width (int) – The width of the wall lines in pixels.

save_solution(grid, solution_path, file_path, cell_size=15, line_width=2, colormap='RdYlGn')#

Save a maze and its solution as an SVG file.

For more colormap selection, click here.

Parameters:
  • grid (ndarray) – A two-dimensional array of cells representing a rectangular maze.

  • solution_path (list[tuple[int, int]]) – An ordered list of cell locations representing the solution path.

  • file_path (str) – A path wherein the SVG file is saved.

  • cell_size (int) – The size of each cell in pixels.

  • line_width (int) – The width of the wall lines in pixels.

  • colormap (str) – A colormap included with Matplotlib.

show_grid(grid)#

Display a plot of a rectangular, two-dimensional maze.

Visualization is done with Matplotlib.

Parameters:

grid (ndarray) – A two-dimensional array of cells representing a rectangular maze.

show_solution(grid, solution_path)#

Display a plot of a rectangular, two-dimensional maze and its solution path.

Visualization is done with Matplotlib.

Parameters:
  • grid (ndarray) – A two-dimensional array of cells representing a rectangular maze.

  • solution_path (list[tuple[int, int]]) – An ordered list of cell locations representing the solution path.

class mazely.Maze(rows=3, columns=3, path=None, seed=random.randrange(sys.maxsize), generator=RecursiveBacktracking(), solver=ShortestPath())#

A class to represent a rectangular, two-dimensional maze.

rows#

The total number of rows in the maze. Defaults to 3.

Type:

int

columns#

The total number of columns in the maze. Defaults to 3.

Type:

int

grid#

A two-dimensional array of cells representing a rectangular maze.

Type:

ndarray

grid_size#

The total number of cells in the maze.

Type:

int

solution_path#

An ordered list of cell locations representing the solution path.

Type:

list[tuple[int, int]]

start#

The location of the start cell.

Type:

tuple[int, int]

goal#

The location(s) of the goal cell(s).

Type:

set[tuple[int, int]]

path#

A path to a maze file. Defaults to None.

Type:

str, optional

seed#

The seed value used to initialize the random number generator.

Type:

int

generator#

An instance of a MazeGenerator subclass used for generating mazes. Defaults to RecursiveBacktracking.

Type:

MazeGenerator

solver#

An instance of a MazeSolver subclass used for solving mazes. Defaults to ShortestPath.

Type:

MazeSolver

add_goal_cells(*cells)#

Add cell locations as a goal cell.

Parameters:

cells (tuple[int, int]) – A cell location.

Raises:

ValueError – If either row or column is out of range.

static are_cells_adjacent(*cells)#

Whether each cell is adjacent to the next.

Returns:

True if each cell is adjacent to the next. False if otherwise. Also, False if only one cell is provided as an argument.

Return type:

bool

generate(rows, columns, seed=random.randrange(sys.maxsize))#

Generate a new maze and overwrite to grid.

Parameters:
  • rows (int) – The total number of rows of the maze.

  • columns (int) – The total number of columns of the maze.

  • seed (int) – The seed value used to initialize the random number generator.

get_random_cell()#

Get a random cell location.

Returns:

The location of a random cell.

Return type:

tuple[int, int]

load_maze(path)#

Parse a maze file.

Parameters:

path (str) – A path to a maze file.

remove_wall(cell, neighbor)#

Remove the wall between a cell and its neighbor.

Parameters:
  • cell (tuple[int, int]) – The location of the cell.

  • neighbor (tuple[int, int]) – The location of the cell’s neighbor.

Returns:

Whether the wall removal is successful.

Return type:

bool

set_goal_cell(row, column)#

Set a cell location as a goal cell.

Parameters:
  • row (int) – The row of a cell.

  • column (int) – The column of a cell.

Raises:

ValueError – If either row or column is out of range.

set_start_cell(row, column)#

Set a cell location as the start cell.

Parameters:
  • row (int) – The row of a cell.

  • column (int) – The column of a cell.

Raises:

ValueError – If either row or column is out of range.

solve()#

Solve the maze with a specific configuration.