Posted in ColdFusion | Posted on 07-23-2009 | 3,463 views
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.
2mazeWidth = 30;
3mazeHeight = 30;
4
5maze = arrayNew(2);
6moves = [];
7width = 2*mazeWidth+1;
8height = 2*mazeHeight+1;
9
10for(x=1;x<=height;x++) {
11 for(y=1;y<=width;y++){
12 maze[x][y]=1;
13 }
14}
15
16xPos = 2;
17yPos = 2;
18maze[xPos][yPos]=0;
19moves[1] = yPos+(xPos*width);
20
21while(arrayLen(moves)) {
22
23 possibleDirections = "";
24
25 if(arrayLen(maze) gte xPos+2 && maze[xPos+2][yPos] == 1 && xPos+2!=0 && xPos+2!=height-0){
26 possibleDirections &= "S";
27 }
28
29 if(xpos-2 gt 0 && maze[xPos-2][yPos]==1 && xPos-2!=0 && xPos-2!=height-0){
30 possibleDirections &= "N";
31 }
32 if(ypos-2 gt 0 && maze[xPos][yPos-2]==1 && yPos-2!=0 && yPos-2!=width-0){
33 possibleDirections &= "W";
34 }
35 if(arrayLen(maze[xPos]) gte yPos+2 && maze[xPos][yPos+2]==1 && yPos+2!=0 && yPos+2!=width-0) {
36 possibleDirections &= "E";
37 }
38
39 if(len(possibleDirections)) {
40 move = randRange(1,len(possibleDirections));
41 switch (mid(possibleDirections, move, 1)){
42
43 case "N": maze[xPos-2][yPos]=0;
44 maze[xPos-1][yPos]=0;
45 xPos -=2;
46 break;
47 case "S": maze[xPos+2][yPos]=0;
48 maze[xPos+1][yPos]=0;
49 xPos +=2;
50 break;
51 case "W": maze[xPos][yPos-2]=0;
52 maze[xPos][yPos-1]=0;
53 yPos -=2;
54 break;
55 case "E": maze[xPos][yPos+2]=0;
56 maze[xPos][yPos+1]=0;
57 yPos +=2;
58 break;
59 }
60 moves[arrayLen(moves)+1] = yPos + (xPos*width);
61 }
62 else {
63 back = moves[arrayLen(moves)];
64 arrayDeleteAt(moves, arrayLen(moves));
65 xPos = fix(back/width);
66 Ypos = back mod width;
67 }
68
69}
70
71// drawing the maze
72writeOutput("<code style='font-size:10px;line-height:8x'>");
73
74for(x=1;x<=height;x++){
75 for(y=1;y<=width;y++){
76 if(maze[x][y]==1){
77 writeOutput("##");
78 }
79 else {
80 writeOutput(" ");
81 }
82 }
83 writeOutput("<br>");
84}
85writeOutput("</code>");
86</cfscript>
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.


REALLY liking the new look/feel of the blog
PS: being Emanuele italian, I suppose you should use "he/him".. I know the long hair in the blog's cartoon can be misleading ;)
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.
You da' man!
}
else {
writeOutput(" ");
}
}
to:
}
else {
writeOutput(" ");
}
}
To get it to work... and now that I've got it working the rest of my day is a write off.
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
[Add Comment] [Subscribe to Comments]