473,395 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Maze Generation Algorithm(s)

57
I am writing (or trying to) a small program to draw a maze, that can then be traversed by a user. I have set up a grid of squares that can be either present (blocking the path) or not (allowing the user to navigate into that square).
My first problem, is that all algorithms I can find on the net (Prim, Kruskal etc) deal with a cell having walls (or Nodes having edges/vertexes, if you prefer) - and I can't think of a way to convert my current design.

I have written a small routine to draw a (random) path from bottom left to a random point at the top or right, and have played with some different ways to randomly populate the remaining board (first time literally random whether a particular block was presentor not, then differing probability based on how many blocks surround it, and finally by drawing different paths at random through the maze in different directions). None of these options give a very nice looking maze - sometimes the path is obvious (or the only way you could legitimately access), sometimes it's too open, and sometimes it just looks, well, just messy.

To cut a long story short, I wondered whether anyone might know (of) an algorithm that might be suited to this type of maze - or whetehr anyone had any ideas on how I might adapt one of the more common Node-edge or Cell-Wall algorithms to this purpose (ie: Prim/Jarnek (DJP), Kruskal are probably no good to me unless you can suggest how to modify them)...
I suspect I could use a Recursive Division algorithm, but am unsure where to find a definitive explanation of exactly how it works.

Any suggestions welcome....

<Edit> I just realised my algorithm to create the original path wasn't quite correct (the path would only go up or right, never left or down). Having corrected it, I now get way too much whitespace (I think its making very intricate paths - the problem is I have no 'boundary' between cells....)
</edit>
Jul 23 '08 #1
5 9634
sicarie
4,677 Expert Mod 4TB
As this question is more about an algorithm than the language, I'm moving it over to the Software Development forum.
Jul 23 '08 #2
Brosert
57
Which says:
Please do not post Technical Questions here
I also considered Miscellaneous Questions
Jul 24 '08 #3
JosAH
11,448 Expert 8TB
Given the following constraints and definitions:

1) a maze consists of a rectangle of rectangular rooms;
2) a room is 'open' if it doesn't have four walls around it;
3) a room is 'closed" if it has four walls around it.

A maze can be generated as follows:

0) all rooms are closed;
1) start in a random room in the rectangle;
2) find a random closed adjacent room; if not found go to step 4
3) open the wall between the two rooms; move to the other room; go to step 2.
4) if all the rooms are open goto step 6
5) find an random open room with an adjacent closed room; go to step 2
6) stop

After the algorithm has stopped you have created a maze with no entry and exit
yet; open two random walls at the border of the rectangle. Only one path leads
from the entry to the exit and there are no cycles in the maze which makes it
quite easy to solve the maze (the 'right hand wall rule'). If you break down a few
other random walls in the maze you have created a complex maze with cycles.

kind regards,

Jos
Jul 27 '08 #4
Brosert
57
Thanks Jos,

Actually the problem was that I was using cells without the concept of walls - that is, this cell is either filled in or it isn't. In this way, each 'cell' was a filled square - and was either there or not (so the whole thing could be stored in a boolean array.
Everything I found on the net seemed to suggest the same sort of method you specified, but I really didn't want to change the way I was drawing the maze on the screen.
Eventually, I decided to bring the concept of walls in, but in such a way that only squares with both x & y coordinates even would be cells, and any other cell would be a wall (with odd x & y ALWAYS being a wall).
Using an algorithm similar to yours, I manged to generate a maze as desired (in non language-specific code):

Expand|Select|Wrap|Line Numbers
  1. Pick Random_Cell
  2. RandomCell->Visited=true;
  3. wallList Add(Random_Cell's Surrounding_walls)
  4. while( wallList Not Empty) loop
  5.  
  6.    wall = wallList->getRandomWall();
  7.    if(wall has unvisited cell on either side)
  8.    {
  9.      unvisited cell = visited;
  10.      wallList Add(wall->newly_visited_cell's- Surrounding_walls);
  11.    }
  12.    Remove wall from wallList;
  13. }
  14.  
Same principle, I suppose...Gives you a unique path to any cell in the maze.

Currently, the maze is 29x29 cells, and the pattern generated is sufficiently complex to make the RH rule tedious, and lengthy (for a human user), although I will consider removing some random walls just to add to the challenge!!

Although counting the steps taken and reporting back to the user how many they took, and the minimum they could have taken works well for now.

Tanks Again!!
Jul 30 '08 #5
JosAH
11,448 Expert 8TB
Currently, the maze is 29x29 cells, and the pattern generated is sufficiently complex to make the RH rule tedious, and lengthy (for a human user), although I will consider removing some random walls just to add to the challenge!!
You do that; it makes those mazes much more complex; human beings aren't
that good in seeing a complex cycle hidden in those mazes.

kind regards,

Jos
Jul 30 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Bernard Fields | last post by:
Greets, all. As the title suggests, I'm trying to make a maze. Specifically, it's a top-down, 2-d maze, preferably randomly generated, though I'm willing to forego that particular aspect as...
2
by: Roger Douglass | last post by:
I've been working on this program for school and I just about got it before I had to turn it in. Here's my final code on it. I didn't finish it, but I think I'm pretty close. M is the starting...
1
by: Ghada Al-Mashaqbeh via DotNetMonster.com | last post by:
Hi all, I want to implement Lee Maze routing algorithm in C#, I have many problems especially in gaining usefull resources about the algorithm work, please if can any body help me to find...
1
by: daneshjo | last post by:
Hi im an IT student.I have registered as a member of this site recently.I have a question about the solution of maze program. I want to write maze program in c++ .I want to solve the maze class...
2
by: dev24 | last post by:
Hi guys, I was looking at random maze generators on the web. I found this prims algorithm but everytime I run it, it doesnt display the random maze as it should. It just displays a blank applet....
1
by: faroz | last post by:
anyone has any idea about sokoban maze algorithm?? thx for helping...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.