#include <dfs_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 Private Member Functions

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

Detailed Description

Depth First Search Generator - Generate a maze using a Depth First Search (DFS) strategy.

Depth First Search (DFS) Maze Generator is a randomized version of the depth-first search traversal algorithm. Implemented with a stack, this approach is one of the simplest ways to generate a 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 45 of file dfs_generator.hxx.

Member Typedef Documentation

Definition at line 52 of file dfs_generator.hxx.

Definition at line 51 of file dfs_generator.hxx.

Definition at line 53 of file dfs_generator.hxx.

Member Function Documentation

static std::vector<std::shared_ptr<Cell> > huc::maze::DFSGenerator::GetNeighbours ( const Maze maze,
const Cell cell 
)
inlinestaticprivate

GetNeighbours - Retrieve available neighbours

Parameters
mazeGrid within the search occurs
cellCell of which neighbours will be found.
Returns
vector of neighbours that has not been visited yet, empty vector if none.

Definition at line 112 of file dfs_generator.hxx.

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

Definition at line 55 of file dfs_generator.hxx.

57  {
58  if (width < 1 || height < 1 || startPoint.x >= width || startPoint.y >= height)
59  return nullptr;
60 
61  auto maze(std::unique_ptr<Maze>(new Maze(width, height)));
62  std::mt19937 mt(seed); // Random generator - Mersenne Twister algorithm
63  std::stack<std::shared_ptr<Cell>> pathStack; // Keep track of the cell path
64 
65  // Create a stack to keep track of the path and add the starting cell within.
66  (*maze)[startPoint.x][startPoint.y]->info.rootDistance = 0;
67  (*maze)[startPoint.x][startPoint.y]->info.isVisited = true;
68  pathStack.push((*maze)[startPoint.x][startPoint.y]);
69 
70  // While there is cell within the stack
71  while (!pathStack.empty())
72  {
73  // Handle the Cell at the top of the stack
74  auto cell = pathStack.top();
75  pathStack.pop();
76 
77  // Get available neighbours and connect to them
78  auto neighbours = GetNeighbours(*maze, *cell.get());
79 
80  // Add them to the stack and select one to be processed next
81  if (!neighbours.empty())
82  {
83  // Randomly select a cell to be processed
84  auto randIdx = mt() % neighbours.size();
85 
86  // For each available cell we make a connection and push it into the stack of being processed
87  // Only the choosen item should be add to the top following a DFS strategy
88  for (uint8_t i = 0; i < neighbours.size(); ++i)
89  {
90  neighbours[i]->info.isVisited = true;
91  neighbours[i]->info.rootDistance = cell->info.rootDistance + 1;
92 
93  if (i != randIdx) pathStack.push(neighbours[i]);
94  }
95  pathStack.push(neighbours[randIdx]);
96 
97  // Connect cell with all neighbours
98  maze->Connect(cell, neighbours);
99  }
100  }
101 
102  return maze;
103  }
Grid< CellInfo > Maze
static std::vector< std::shared_ptr< Cell > > GetNeighbours(const Maze &maze, const Cell &cell)

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