This particular problem is probably going to be a bit hard to explain, but I’ll give it my best shot.
I’m creating a script that builds a maze, using PHP and CSS. The script works fine, except for one problem. I’ll get to the problem in a sec, but first, a link, and some background:
The idea for this script came about in this thread in the CSS forum. I looked at an entry posted by another member, and wondered if I could create a maze programatically, so I started working on it. This is what I have, so far:
http://www.geekcavecreations.com/css_maze.php?rows=30&cols=50
The problem I’m having is fairly evident, if you refresh the screen a few times, but I’ll describe it here, anyways.
Since the script randomly builds a new maze every time, there’s a fair chance that the maze creates a “blockade” near either the entrance, or the exit, within about 1 to 6 spaces in. This is clearly unacceptable.
What I’m trying to figure out is how to come up with a function that applies a “weight” to the location of the square it’s creating the border for, where the probability of placing a border in any direction is reduced, based on the location of the square.
In other words, if the square is within 1 square of either the entrance or exit, the probability of placing a border is very low (e.g. 5%), whereas the probability of placing a border in any direction of a square in the middle is closer to 80% (Which is what it is, on average, over the entire board). The function I have now already takes into account the border property of the square above it (unless there isn’t one there), or the square to it’s left, with the same caveat.
I’m guessing that the best way is to take a total number of squares (maxRows * maxCols), and creating a percentage, based on the position of the square in question {e.g (col * row) / numCells}, and then apply that number, somehow, to the probability of placing a border. The problem I have is that I don’t have the foggiest notion of how to apply this percentage. Thus, I turn to you folks here. What would a script look like, that can accomplish this goal? Any notions?