huc::maze::SidewinderGenerator Class Reference

#include <sidewinder_generator.hxx>

Public Types

typedef Grid< CellInfoBaseMaze
 
typedef Maze::Cell Cell
 

Public Member Functions

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

Detailed Description

Sidewinder Generator - Generate a maze using a sidewinder strategy.

Sidewinder Maze Generator is very similar to the Binary Tree algorithm, and only slightly more complicated. Furthermore, the Sidewinder algorithm only needs to consider the current row, and therefore can be used to generate infinitely large mazes (like the Binary Tree).

While binary tree mazes have two of its four sides being one long passage, a Sidewinder mazes have just one long passage.

Parameters
widthdesired width for the maze.
heightdesired height for the maze.
seednumber used to initiate the random generator.
Returns
Operator() returns Maze Grid pointer to be owned, nullptr if construction failed.

Definition at line 47 of file sidewinder_generator.hxx.

Member Typedef Documentation

Member Function Documentation

std::unique_ptr<Maze> huc::maze::SidewinderGenerator::operator() ( const uint32_t  width,
const uint32_t  height,
const uint32_t  seed = 0 
)
inline

Definition at line 53 of file sidewinder_generator.hxx.

54  {
55  if (width < 1 || height < 1)
56  return nullptr;
57 
58  auto maze(std::unique_ptr<Maze>(new Maze(width, height)));
59  std::mt19937 mt(seed); // Random generator based on Mersenne Twister algorithm
60  std::set<std::shared_ptr<Cell>> runSet; // Keep track of possible paths to expand
61 
62  // Initialize an empty “run” set to keep track of the current line path.
63  // Scan grid line by line starting with the cell[0,0]:
64  for (uint32_t y = 0; y < height; ++y)
65  {
66  for (uint32_t x = 0; x < width; ++x)
67  {
68  // Add current cell to the path (avoid useless insertion)
69  if (y > 0)
70  runSet.insert((*maze)[x][y]);
71 
72  // Randomly carve to east or north
73  // If a passage was carved, continue line scan.
74  // First row can only be a single passage, crave it to the east
75  if (x + 1 < width && (mt() % 2 == 0 || y == 0))
76  {
77  maze->Connect((*maze)[x][y], (*maze)[x+1][y]);
78  }
79  // Otherwise randomly choose a cell in the run set and carve north
80  else if (y > 0)
81  {
82  auto cellIt = runSet.begin();
83  std::advance(cellIt, mt() % runSet.size());
84 
85  maze->Connect(*cellIt, (*maze)[(*cellIt)->x][(*cellIt)->y - 1]);
86  runSet.clear(); // Empty the run set and continue line scan
87  }
88  }
89 
90  runSet.clear();
91  }
92 
93  return maze;
94  }

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