#include <kruskals_generator.hxx>

Classes

struct  CellInfoBucket
 

Public Types

typedef Grid< CellInfoBucketMaze
 
typedef Maze::Cell Cell
 
typedef Maze::Edge Edge
 
typedef Maze::Point Point
 

Public Member Functions

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

Static Private Member Functions

static void MergeBucket (std::vector< std::vector< std::shared_ptr< Cell >>> &buckets, uint32_t fromId, uint32_t ToId)
 

Detailed Description

Kruskal's Generator - Generate a maze using a kruskal's strategy.

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

Kruskal's is interesting because it doesn't "grow" the Maze like a tree, but rather carves passage segments all over the Maze at random, making it very fun to watch. Still, it results in a perfect Maze in the end.

The counterpart is to require storage proportional to the size of the Maze, along with the ability to enumerate each edge between cells in random order (Using here a set of edges and taking them randomly).

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 50 of file kruskals_generator.hxx.

Member Typedef Documentation

Member Function Documentation

static void huc::maze::KruskalsGenerator::MergeBucket ( std::vector< std::vector< std::shared_ptr< Cell >>> &  buckets,
uint32_t  fromId,
uint32_t  ToId 
)
inlinestaticprivate

MergeBucket - Merge two buckets of node together and update node bucket Id.

Parameters
bucketsvector containing all the buckets
fromIdbucket id that will be merged within the other bucket
ToIdbucket id that will receive the other bucket cells
Returns
void.

Definition at line 118 of file kruskals_generator.hxx.

120  {
121  if (fromId == ToId)
122  return;
123 
124  // Set new bucket id for each melding cell
125  for (auto it = buckets[fromId].begin(); it != buckets[fromId].end(); ++it)
126  {
127  (*it)->info.bucketId = ToId;
128  buckets[ToId].push_back(*it);
129  }
130 
131  buckets[fromId].clear();
132  }
std::unique_ptr<Maze> huc::maze::KruskalsGenerator::operator() ( const uint32_t  width,
const uint32_t  height,
const uint32_t  seed = 0 
)
inline

Definition at line 61 of file kruskals_generator.hxx.

62  {
63  if (width < 1 || height < 1)
64  return nullptr;
65 
66  auto maze(std::unique_ptr<Maze>(new Maze(width, height)));
67  std::mt19937 mt(seed); // Random generator - Mersenne Twister algorithm
68  std::set<Edge> edges; // Big sac with all possible edges
69  std::vector<std::vector<std::shared_ptr<Cell>>> bucketCells; // List of all buckets
70 
71  // Fill big sac of edges and set the bucket ids to each cell
72  uint64_t nodeId = 0;
73  bucketCells.resize(height * width); // (height - 1) * width + (width - 1) * height);
74  for (uint32_t x = 0; x < maze->Width(); ++x)
75  for (uint32_t y = 0; y < maze->Height(); ++y, ++nodeId)
76  {
77  (*maze)[x][y]->info.bucketId = nodeId;
78  bucketCells[nodeId].push_back((*maze)[x][y]);
79 
80  // Insert Right edge
81  if (x + 1 < maze->Width())
82  edges.insert(Edge((*maze)[x][y], (*maze)[x+1][y]));
83  // Insert Bottom edge
84  if (y + 1 < maze->Height())
85  edges.insert(Edge((*maze)[x][y], (*maze)[x][y+1]));
86  }
87 
88  // While the set of edges is not empty randomly get an edge (connecting two cells):"
89  while (!edges.empty())
90  {
91  auto edgeIt = edges.begin();
92  std::advance(edgeIt, mt() % edges.size());
93  const auto firstBucket = edgeIt->first->info.bucketId;
94  const auto secondBucket = edgeIt->second->info.bucketId;
95 
96  // Add connection and merge buckets if not already in the same one
97  if (firstBucket != secondBucket)
98  {
99  maze->Connect(edgeIt->first, edgeIt->second);
100  MergeBucket(bucketCells, firstBucket, secondBucket);
101  }
102 
103  // Remove computed cell from the set
104  edges.erase(edgeIt);
105  }
106 
107  return maze;
108  }
static void MergeBucket(std::vector< std::vector< std::shared_ptr< Cell >>> &buckets, uint32_t fromId, uint32_t ToId)

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