Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create cells or grids in C++ for a randomized maze?

I'm trying to create a randomized maze in C++, but I can't start because I don't know how to create grids or cells. How could I create it? And I also want to create it using ASCII characters. how can i store it in array? (can any one give a sample code and some explanation so i can understand it better)

Another question: What data stuctures should I need to learn and use? I'm planning to use Eller's algorithm or Kruskal's algorithm.

Thank you guys for helping me! im a begginer programmer, and i want to learn about this, because this is a part of my project, thank you vary much!

like image 996
jessemiel Avatar asked Jan 24 '23 22:01

jessemiel


1 Answers

Are you looking for Maze generation algorithms (more)? Is your problem with the algorithms, or graphics?

The typical algorithms work by considering each "cell" in the maze as a vertex of a graph, start with all "walls", and remove a set of walls that corresponds to a spanning tree. (So in order to randomize it, many of them start with random weights and find the minimum spanning tree.) For small mazes at least, you don't need any special data structure to represent the cells; you can just think of each cell as a pair (x,y) (its coördinates). And you don't need any data structure (adjacency matrix / adjacency list) to store the edges of the graph either, because the neighbours of (x,y) are just (x,y±1) and (x±1,y) (ignoring those that fall outside the boundaries).

In any case, once you have the spanning tree, you know exactly which of the walls "exist" and which do not, so you have a complete description of the maze. If you're going to draw the maze, you know which ones to draw.

To draw with ASCII characters, you just pass through each row one by one: draw the "upper walls" (put a "--" if the wall between (x,y) and (x,y+1) exists), then draw the actual row (put a "|" if the wall between (x,y) and (x+1,y) exists). Finally draw the bottom boundary.

like image 168
ShreevatsaR Avatar answered Jan 30 '23 01:01

ShreevatsaR