C Code for Generating Mazes

Started by
5 comments, last by Toom 23 years, 2 months ago
I've noticed more than one request lately for info about generating random 2D mazes so I wrote a little program to do just that. It's short, simple, fast, and free. It's also all of 15 minutes old and not tested completely. You can get the source at www.markmorley.com/maze.c Enjoy, Toom PS: You can see a graphical online version of this in action at www4.islandnet.com/cgi-bin/maze.pl (I wrote that one in Perl). Edited by - Toom on February 7, 2001 2:31:57 PM
Advertisement
I downloaded the c version, and ran it. All that showed up was a series of vertical lines, with holes at randomly spaced heights - not the most difficult maze. The graphical version acted very differently...

A while ago I wrote a maze generator in turbo pascal for dos / assembler - and it''s extremely fast. I did rip the method off the book "Art of Assembly", but I didn''t actually copy any code.

It may not be a brilliant maze generator, but at the speed it generates new mazes, you can quickly find a good one.
Source code
Executable
Don''t know what you''re doing, but it works fine for me. What you describe sounds like you might need to tweak the array shuffler. I wrote this on a FreeBSD box and I run it from the command line (all I had available at that particular time).

It generates a maze 71 x 23 cells big that''s different each time and has no vertical or horizontal striping. Works exactly like the online version, just outputs an ASCII maze instead of a graphical one.

Toom
Here's a sample of the output (ran it just now):

########################################################################S#       # #             #   #             #                         ## # ##### # # ### ####### # # # ##### ##### # ######### ############# ##   # #   #     # # #     # #   #     #   # # #         # #   #   #   ###### # ######### # # ##### ##### ##### # # ### ######### # # # # # #### #     #         # #   #   #     #     # #     #   #     # #   #   # ## # ##### ######### ### ##### ##### ##### ####### ### # ### ######### ## #       # #     #   #       #   #   #     #     #   #     #         ## ######### # # ### # ######### # ### # ##### ### # ######### # ######## #       #   #   # #   #     # #     #       # #   #   #   # #       ## # # ##### ##### # # ### # ### ############### ##### # # # # ####### ##   # #   #     #   #   # #     #                     #   # #       # ## ### # # ### ######### # ##################### ########### ####### # ##   #   #     #         #           #         #       #   #       # # #### ########### ### ############### # ####### ####### # # ####### # # ##   #         # #   #   #     #   #   #     #   # #   # #       # # # ## ##### ##### # ### # # # ### # # ##### ### ### # # ### ####### # # # ##       #   # #   # # # #   # # # #   # #   #   #       #       #   # ############ # ### ### # ### # # # # # # # ### ####################### ##     #     #   #   # # #   # # # # #   # #   #           #           ## ### # # ##### ### # # # ### # # # ##### # ### ######### # ####### # ##   #   #         #   #     #   #       #       #           #       #E########################################################################  


And if your browser messes that up (looks ok with MSIE) you can try www.markmorley.com/maze.html to see the same output.

Edited by - toom on February 7, 2001 12:39:25 AM
Hmmm - I''ll try copying and pasting it into a different compiler, and see what I get.
Hmmm - I''ll try copying and pasting it into a different compiler, and see what I get.
I just wanted to point out that the algo you''re using isn''t all that good for maze generating. It creates very few branches and the mazes are *always* easy to solve because of the algorithm.

I suggest at least adding extra holes to the maze. Yes, adding extra holes makes the maze harder even though it doesn''t sound very logical at first. Trust me.

A better way to create mazes would be this:
Instead of generating the empty spaces, generate the *walls* using the same algorithm. Use lots of wall-generators moving simultaneously, all of them starting from the border of your maze. This way you''ll get much more complex mazes. I hope I was clear enough :/.

This topic is closed to new replies.

Advertisement