#include <prims_generator.hxx>

Classes

struct  CellInfo
 

Public Types

typedef Grid< CellInfoMaze
 
typedef Maze::Cell Cell
 
typedef Maze::Point Point
 

Public Member Functions

std::unique_ptr< Mazeoperator() (const uint32_t width, const uint32_t height, const Point &startPoint=Point(0, 0), const uint32_t seed=0)
 

Static Public Member Functions

static std::vector< std::shared_ptr< Cell > > GetNeighbours (const Maze &maze, const Cell &cell, const bool visited)
 

Detailed Description

Prim's Generator - Generate a maze using a Depth First Search (Prims) strategy.

Prim's Maze Generator is a randomized version of Prim's algorithm: a method for producing a minimal spanning tree for a undirected weighted graph.

Prim's algorithm creates a tree by getting the adjacent cells and finding the best one to travel to next. To Generate mazes using Prim's, we will instead take a random cell to travel to the next one.

Although the classical Prim's algorithm keeps a list of edges, here is studied the modified version for our maze generation by maintaining a list of adjacent cells. Running faster, it still requires storage proportional to the size of the Maze.

Parameters
widthdesired width for the maze.
heightdesired height for the maze.
startPointpossible point to start the algorithm, {x: 0, y: 0} by default.
seednumber used to initiate the random generator, 0 by default.
Returns
Operator() returns Maze Grid pointer to be owned, nullptr if construction failed.

Definition at line 52 of file prims_generator.hxx.

Member Typedef Documentation

Definition at line 58 of file prims_generator.hxx.

Member Function Documentation

static std::vector<std::shared_ptr<Cell> > huc::maze::PrimsGenerator::GetNeighbours ( const Maze maze,
const Cell cell,
const bool  visited 
)
inlinestatic

GetNeighbours - Retrieve available neighbours

Parameters
mazeGrid within the search occurs
cellCell of which neighbours will be found.
visitedselect whether or (and only or) not cells that are visited
Returns
vector of neighbours, empty vector if none.

Definition at line 107 of file prims_generator.hxx.

108  {
109  std::vector<std::shared_ptr<Cell>> neighbour;
110 
111  const auto x = cell.x;
112  const auto y = cell.y;
113 
114  // Push left if available
115  if (static_cast<int>(x - 1) >= 0 &&
116  ((visited) ? maze[x-1][y]->info.isVisited : !maze[x-1][y]->info.isVisited))
117  neighbour.push_back(maze[x-1][y]);
118  // Push bottom if available
119  if (static_cast<int>(y - 1) >= 0 &&
120  ((visited) ? maze[x][y-1]->info.isVisited : !maze[x][y-1]->info.isVisited))
121  neighbour.push_back(maze[x][y-1]);
122  // Push top if available
123  if (x + 1 < maze.Width() &&
124  ((visited) ? maze[x+1][y]->info.isVisited : !maze[x+1][y]->info.isVisited))
125  neighbour.push_back(maze[x+1][y]);
126  // Push right if available
127  if (y + 1 < maze.Height() &&
128  ((visited) ? maze[x][y+1]->info.isVisited : !maze[x][y+1]->info.isVisited))
129  neighbour.push_back(maze[x][y+1]);
130 
131  return neighbour;
132  }
std::unique_ptr<Maze> huc::maze::PrimsGenerator::operator() ( const uint32_t  width,
const uint32_t  height,
const Point startPoint = Point(0,0),
const uint32_t  seed = 0 
)
inline

Definition at line 62 of file prims_generator.hxx.

64  {
65  if (width < 1 || height < 1 || startPoint.x >= width || startPoint.y >= height)
66  return nullptr;
67 
68  auto maze(std::unique_ptr<Maze>(new Maze(width, height)));
69  std::mt19937 mt(seed); // Random generator - Mersenne Twister algorithm
70  std::set<std::shared_ptr<Cell>> pathSet; // Keep track of possible paths to expand
71 
72  // While there is cell within the set:
73  (*maze)[startPoint.x][startPoint.y]->info.rootDistance = 0;
74  pathSet.insert((*maze)[startPoint.x][startPoint.y]);
75  while (!pathSet.empty())
76  {
77  auto cellIt = pathSet.begin();
78  std::advance(cellIt, mt() % pathSet.size());
79  (*cellIt)->info.isVisited = true;
80 
81  // Randomly connect it to a cell that is already part of the mze
82  auto neighbours = GetNeighbours(*maze, *(*cellIt).get(), true);
83  if (!neighbours.empty())
84  {
85  auto randIdx = mt() % neighbours.size();
86  (*cellIt)->info.rootDistance = neighbours[randIdx]->info.rootDistance + 1;
87  maze->Connect(*cellIt, neighbours[randIdx]);
88  }
89 
90  // Add available neighbours and remove current cell
91  neighbours = GetNeighbours(*maze, *(*cellIt).get(), false);
92  pathSet.insert(neighbours.begin(), neighbours.end());
93  pathSet.erase(cellIt);
94  }
95 
96  return maze;
97  }
static std::vector< std::shared_ptr< Cell > > GetNeighbours(const Maze &maze, const Cell &cell, const bool visited)

The documentation for this class was generated from the following file: