Yesterday at CFUNITED I presented some more (useless) code concerning mazes and ColdFusion. I've blogged on this a few times already, but I decided to play around with it a bit more. For the derby, I demonstrated code that generated mazes in a slightly different way than I've had before. I've shown outputting mazes with text. I've shown outputting mazes with graphics. But this version puts one more little spin on it - old school style:

What you are seeing here is a text based description of a maze stored in the session scope. This is done by first figuring out what directions are allowed based on your current position in the maze:
2 var exits = "";
3 //handle west possible
4 if(variables.mazedata.maze[arguments.y][arguments.x-1] == 0) exits = listAppend(exits, "west");
5 //handle east possible
6 if(variables.mazedata.maze[arguments.y][arguments.x+1] == 0) exits = listAppend(exits, "east");
7 //handle north possible
8 if(variables.mazedata.maze[arguments.y-1][arguments.x] == 0) exits = listAppend(exits, "north");
9 //handle south possible
10 if(variables.mazedata.maze[arguments.y+1][arguments.x] == 0) exits = listAppend(exits, "south");
11 return exits;
12 }
This then is used by a simple function to describe the current room. All rooms in the maze have the exact same look - it is only the exits that change:
2 //better not be a wall or you are dead
3 var exits = getExits(arguments.x,arguments.y);
4 var s = "You are in a dark and dusty maze.<br/><br/>";
5 var i = "";
6
7 //Ok, I'm using pos with 0,0 in upper left
8 if(listLen(exits) is 1) {
9 s &= "There is one exit to the #exits#.";
10 } else {
11 s &= "There are exits to the ";
12 for(i=1; i <= listLen(exits); i++) {
13 s &= listGetAt(exits, i);
14 if(i < listLen(exits)-1) s&= ", ";
15 else if (i == listLen(exits)-1) s&= " and ";
16 //else if (listLen(exists) is 2 and i is 1) s
17 }
18 }
19
20 return s;
21 }
Movement is done by simply checking your current position against valid exits. Also note that I allow for shorthand versions of movements (e for east, etc):
2 var pos = {x=arguments.x,y=arguments.y};
3 if(dir == "e") dir="east";
4 if(dir == "w") dir="west";
5 if(dir == "s") dir="south";
6 if(dir == "n") dir="north";
7
8 if(listFindNoCase(getExits(x,y), dir)) {
9 if(dir == "east") pos.x++;
10 if(dir == "south") pos.y++;
11 if(dir == "west") pos.x--;
12 if(dir == "north") pos.y--;
13 }
14 return pos;
15 }
Totally useless - and totally fun - and yes - I have inklings for other maze demos to try as well. I've attached the code (including the Grue version) but note that it is ColdFusion 9 only.


Comment 1 written by Sid Wing on 15 August 2009, at 6:41 AM
Comment 2 written by Raymond Camden on 15 August 2009, at 6:43 AM
Comment 3 written by Sid Wing on 15 August 2009, at 6:56 AM
http://www.thecomputerwizards.org/index.cfm/2009/5...
Comment 4 written by Alexander Sante on 15 August 2009, at 10:30 AM
Comment 5 written by Raymond Camden on 16 August 2009, at 8:38 PM
Right?
Comment 6 written by Sid Wing on 17 August 2009, at 8:14 AM
[Add Comment] [Subscribe to Comments]