Generating mazes in ColdFusion

This post is more than 2 years old.

People like Ben Forta may rave about so called "important" ColdFusion features like Excel generation, .Net integration, and PDF Manipulation, but honestly, how important are those features in comparison to something really in demand in today's enterprise market: Maze Generation. I can't tell you how many times clients have asked me to generate mazes for them - sometimes even removing less critical items from the contract like performance tuning and security reviews. So with that in mind, last night I wrote a ColdFusion port of Emanuele Feronato's PHP code to generate mazes. You can find her blog entry here.

The port was pretty simple. ColdFusion supports variables with dollar signs in front, but they drive me crazy when I look at them. I mean, would you want code that looks like this?


So I rewrote her variables to get rid of the dollar signs. I also had to deal with the fact that PHP incorrectly uses 0-based array indexes. (Ok, I know, I know, but go with me here.) PHP also, and this is truly odd, will blissfully ignore you trying to access an index in an array that doesn't exist, like -1. Oh I got warnings, but no real errors. I'm not sure what the logic of that is (maybe PHPs coders can't count? I kid!). With that said, here is the completed code. Again, credit goes to Emanuele.

<cfscript> mazeWidth = 30; mazeHeight = 30;

maze = arrayNew(2); moves = []; width = 2mazeWidth+1; height = 2mazeHeight+1;

for(x=1;x<=height;x++) { for(y=1;y<=width;y++){ maze[x][y]=1; } }

xPos = 2; yPos = 2; maze[xPos][yPos]=0; moves[1] = yPos+(xPos*width);

while(arrayLen(moves)) {

 possibleDirections = "";

 if(arrayLen(maze) gte xPos+2 && maze[xPos+2][yPos] == 1 && xPos+2!=0 && xPos+2!=height-0){
      possibleDirections &= "S";

 if(xpos-2 gt 0 && maze[xPos-2][yPos]==1 && xPos-2!=0 && xPos-2!=height-0){
      possibleDirections &= "N";
 if(ypos-2 gt 0 && maze[xPos][yPos-2]==1 && yPos-2!=0 && yPos-2!=width-0){
      possibleDirections &= "W";
 if(arrayLen(maze[xPos]) gte yPos+2 && maze[xPos][yPos+2]==1 && yPos+2!=0 && yPos+2!=width-0) {
      possibleDirections &= "E";

 if(len(possibleDirections)) {
      move = randRange(1,len(possibleDirections));
      switch (mid(possibleDirections, move, 1)){

           case "N": maze[xPos-2][yPos]=0;
                     xPos -=2;
           case "S": maze[xPos+2][yPos]=0;
                     xPos +=2;
           case "W": maze[xPos][yPos-2]=0;
                     yPos -=2;
           case "E": maze[xPos][yPos+2]=0;
                     yPos +=2;
	  moves[arrayLen(moves)+1] = yPos + (xPos*width);
 else {
      back = moves[arrayLen(moves)];
	  arrayDeleteAt(moves, arrayLen(moves));
	  xPos = fix(back/width);
	  Ypos = back mod width;


// drawing the maze writeOutput("<code style='font-size:10px;line-height:8x'>");

for(x=1;x<=height;x++){ for(y=1;y<=width;y++){ if(maze[x][y]==1){ writeOutput("##"); } else { writeOutput(" "); } } writeOutput("<br>"); } writeOutput("


And an example of the output:

You can run a demo of this yourself here. Note that every reload generates a new and random maze. To be honest, I do have kind of a cool idea of something to do with this, but I'm waiting to see if I can get it done for Demomania at CFUNITED.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Lola LB posted on 7/23/2009 at 3:32 PM

Where are the entry and exit points?

Comment 2 by Raymond Camden posted on 7/23/2009 at 3:37 PM

Upper left for entry, bottom right for ext.

Comment 3 by andy matthews posted on 7/23/2009 at 3:41 PM

Ray, if you used a fixed width font such as courier, you could get a maze that was actually a square. Just a thought.

Comment 4 by Raymond Camden posted on 7/23/2009 at 3:51 PM

I added font-family:courier to the code style in the demo code, but it didn't seem to do anything.

Comment 5 by Steve Caldwell posted on 7/23/2009 at 3:55 PM

this kind of uselessness is refreshing!
REALLY liking the new look/feel of the blog

Comment 6 by Yaron Kohn posted on 7/23/2009 at 3:57 PM

this is way better than the new ORM features in CF 9

Comment 7 by Rudi posted on 7/23/2009 at 4:03 PM

Very Clever....and DIG the new look/feel

Comment 8 by Ludovico Clemente posted on 7/23/2009 at 4:04 PM

I suggest adding <cfmaze> support to CF9... something like <cfmaze width=".." height=".." wallChar=".." /> would do.. :)

PS: being Emanuele italian, I suppose you should use "he/him".. I know the long hair in the blog's cartoon can be misleading ;)

Comment 9 by Sean Coyne posted on 7/23/2009 at 4:28 PM

now write an app that solves it :)

Comment 10 by Raymond Camden posted on 7/23/2009 at 5:02 PM

@Ludovico: Ouch - If I misgendered him, I'm definitely sorry!

Comment 11 by Jeff posted on 7/23/2009 at 5:15 PM

I'm lost....

Comment 12 by Dan Vega posted on 7/23/2009 at 5:19 PM

Finally some real enterprise features that I can use!

Comment 13 by Paul Connell posted on 7/23/2009 at 5:30 PM

If you put a <pre> around it, add <cfprocessingdirective pageencoding="utf-8"> and use ? (UTF U+2588) as the block instead of ## it looks awesome!

Comment 14 by Raymond Camden posted on 7/23/2009 at 5:32 PM

FYI, looks like code format on the blog is a bit off. Tabs aren't showing up. Will fix.... some time.

Comment 15 by Sid Wing posted on 7/23/2009 at 6:39 PM

As always - the JediMaster never ceases to a-maze me... {grin}

Comment 16 by andy matthews posted on 7/23/2009 at 6:53 PM


Change this line:
<code style='font-size:10px;line-height:8x'>

to this:
<code style='font-family: courier;letter-spacing: .3em;font-size:10px;line-height:8x'>

And the display will be much better.

Comment 17 by Unibands posted on 7/23/2009 at 8:20 PM

Wow, this is rather interesting! Pretty complicated too. Did you understand the code ray or simply ported it to CF?

You da' man!

Comment 18 by Raymond Camden posted on 7/23/2009 at 8:23 PM

No, I don't get it. I tried... and I kind of think I get some of the logic, but it is definitely a bit above me. I have a healthy respect for algorithms like this. It's like art.

Comment 19 by Kris Korsmo posted on 7/23/2009 at 10:31 PM


Comment 20 by Brian W. posted on 7/24/2009 at 11:17 PM

I had to change:
else {
writeOutput(" ");
else {
writeOutput("  ");
To get it to work... and now that I've got it working the rest of my day is a write off.

Comment 21 by Brian W. posted on 7/24/2009 at 11:19 PM

Ah, my &_nbsp_; didn't show up in your comments... and hence your code posting -- ma' bad :)

Comment 22 by Jody Fitzpatrick posted on 7/26/2009 at 1:42 AM

Ray my tummy hurts...

But your blog made it feel better. I really like the new blog... I found a couple errors using IE6 but no one should use IE6