Maze Program Information
Just some idle thoughts about programming in JavaScript and writing the maze program.
Click here to go back to the maze!
-
Cells are in a 2-dimensional array; for instance: gtMaze.ary[y][x]
Internet Explorer (and Microsoft's JScript) can now handle 2-D arrays, so this new version works for IE.
I have no idea whether it works with Netscape (nor do I care much).
-
The maze-creation algorithm is from an article I wrote for Creative Computing
about 19 years ago!
The algorithm guarantees that there is exactly one path from any two locations.
Each cell has a binary value; 0 means no doors, 1 means a door at the top,
2=door on the left, 4=bottom, and 8=right. For instance, a value of 9 decodes to
a cell with doors on the top and right.
-
The auto-solving algorithm was a bit trickier. I initially wrote a recursive function
SolveFrom(x,y) that was truly elegant but Netscape's JavaScript interpreter
would blow its stack with mazes larger than about 10x10. So I implemented my own
homemade stack and wrote a linear algorithm.
-
The most difficult challenge was in making it possible to watch the maze creation and the action while the program
searches for a solution. IE5 appears to cache screen updates until a function (event handler) is finished.
So I rewrote the entire program do do these in incemental steps, then I start a bunch of timers to trigger each step. It
is a massive kludge, but it holds together pretty well and I haven't a clue as how to do it otherwise (if you know, please
tell me! dan@inorbit.com
-
This new version of the program uses several tricks of Dynamic HTML; for instance, it hides
portions of the document using the
document.all.ID.style.display=hidden command. This also let me get rid of the
two-frame system which was a headache.
-
Each cell in the maze-viewing frame is an anchor that has an
onclick=functionName(). That lets you click on a cell to toggle
its image between filled-with-red and empty. After playing with this for a while,
I decided that it became too cumbersome to click each cell, so I re-wrote the
function to fill all cells between the previously-clicked location and the
current -- as long as there were doors between them!
I experimented with using onMouseOver in the HREF, but I found
that it became harder to trace the solution.
Click here to go back to the maze!
Click here to go back to my Home Page!