Update!
I've gotten the probability portion to work, with a 95% chance that it created a solvable maze, but the areas near the exit and entrance looked unacceptably blank. So I moved on to creating a pathing routine. I used the following guidelines:
1.) The path should not cross itself
2.) The path builder needs to know how far it can travel in a given direction before it hits either another section of the path, or the edge of the maze
3.) The path cannot travel in any one direction more than one third of the available distance, unless that distance is less than three cells
4.) The path cannot "double back" on itself (see rule #1)
5.) The path MUST end at lastRow, lastCol ( maxRows-1, maxCols-1)
This is what I've got for the pathing function:
PHP Code:
function makePath() {
global $cardinal, $lastRow, $lastCol, $path;
$row = $col = 0;
$that_a_way = array_flip($cardinal);
$lCount = $cCount = 0;
$dir = "e";
while (!(($row === $lastRow) and ($col === $lastCol))) {
$maxDistance = 0;
$zeroCount = 0;
while ($maxDistance === 0) {
$zeroCount++;
$dir = newDirection($dir);
$maxDistance = getMaxDistance($row, $col, $dir);
if($zeroCount > 100) {
/*
This section cheats a little, since sometimes the script "paints itself
into a corner". Nine times out of ten, this violates rule #1
*/
$x = $col + 5;
$x = ($x > $lastCol) ? $lastCol : $x;
$y = $row + 5;
$y = ($y > $lastRow) ? $lastRow : $y;
for ($col = $col; $col <= $x; $col++) {
$path[$row][$col] = 1; // head east
}
for ($row = $row; $row <= $y; $row++) {
$path[$row][$col] = 2; // head south
}
break;
}
}
$travel = rand(1, round($maxDistance / 3) + 1);
for($l = 0; $l < $travel; $l++) {
$path[$row][$col] = $that_a_way[$dir];
switch ($dir) {
case "n":
$row--;
break;
case "s":
$row++;
break;
case "e":
$col++;
break;
case "w":
$col--;
}
$row = ($row > $lastRow) ? $lastRow : $row;
$col = ($col > $lastCol) ? $lastCol : $col;
}
$lCount++;
}
}
This seems to work as intended, creating a random path that extends from the northeast corner (0,0) to the southwest corner (lastRow, lastCol). Can anyone tell me if there's perhaps a better way to do this? That section of code is a bit of a mess.
Bookmarks