Raymond Camden's Blog Rss

CFUNITED Demo Derby Code

6

Posted in ColdFusion | Posted on 08-15-2009 | 3,008 views

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:

view plain print about
1public function getExits(numeric x, numeric y) {
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:

view plain print about
1function describePosition(numeric x, numeric y) {
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):

view plain print about
1public function move(string dir, numeric x, numeric y) {
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.

Download attached file

Comments

[Add Comment] [Subscribe to Comments]

Hey Ray! Here's a link to a bit of nostalgia for ya - :-)http://www.vaporware-inc.com/ta/
Is this your site?
Yes sir - and here's the blog article about it:

http://www.thecomputerwizards.org/index.cfm/2009/5...
Reminds me of some old MUD games I used to play.
So Sid - maybe you know the answer to this. It is my impression that the client side game player thing (forgot the name) can save a game to the file system and restore it as well. So in theory, one could write a CFM wrapper and store their game data into a unique file.

Right?
@Ray - One of the issues with the Java Applet it runs on is that fact that browser security does not allow for a "save game" - there is another z-engine out there that will store the save info as a URL variable that you can bookmark and I am looking at taking that - grabbing the URL variable with CF and storing it to a unique file so that I can then call those files from inside CF - feed it back as a URL variable and "load" a "saved" game .

[Add Comment] [Subscribe to Comments]