Eventually he will reach the edge where the stones are reinforced or he will run out of pairs of stones to break (evidently he feels that knocking down just one stone is somehow beneath him), at which point he will backtrack until another direction can be taken where two stones block the way. The result is a maze exactly like these. This is an example of how a complicated-looking construction can be fabricated from relatively simple steps.
The formula goes like this:
- Look for two stones to break down.
- Choose a direction at random.
- Knock down those two stones.
- Repeat
This is one of the first C / C++ programs I wrote, which I later translated into JavaScript. The function is recursive in that it makes a call to itself. It's a function within a function! I learned a trick to debugging these. Just make a few copies of the function with other names and then when it works right, combine them all together.
What's really "amazing" about this algorithm is it generates a spanning tree, with branches. Because it's a tree, there are no closed loops. This means that no matter where we put our ingress and egress, there is always one and only one route between them!
function mazeStep(r,c){
var i,vector=[[0,0],[0,0],[0,0]]; /* 3 possible directions */
function R(val){
if(typeof val=="undefined")return vector[i][0];
vector[i][0]=val;
}
function C(val){
if(typeof val=="undefined")return vector[i][1];
vector[i][1]=val;
}
while(1){
i=0; /* create a list of possible options */
if(r>1 &&a[r-2][c]!==" "){R(r-2);C(c);i++;}
if(r< id.rows*2-1 &&a[r+2][c]!==" "){R(r+2);C(c);i++;}
if(c>1 &&a[r][c-2]!==" "){R(r);C(c-2);i++;}
if(c< id.cols*2-1 &&a[r][c+2]!==" "){R(r);C(c+2);i++;}
/* i is never > 3 because path behind is cleared */
if(i==0)break; /* check for dead end */
i=Math.floor((Math.random()*i))|0; /* random direction */
a[R()][C()]=" "; /* knock out block */
a[(R()+r)/2|0][(C()+c)/2|0]=" "; /* clear to it */
mazeStep(R(),C());
}
}This algorithm carves out a random maze by erasing nearby blocks.
step 1 step 2 step 3 step 4 step 5 step 6
@@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@ @@@@@ @@@ @@@ @@@ @@@ @@@ @ @@@
@@@@@@@@@ @@@@@@@@@ @@@@@ @@@ @@@@@ @@@ @@@ @ @@@ @@@ @ @@@
@@@@@@@ @ @@@@@ @ @@@@@ @ @@@@@ @ @@@ @ @ @@@ @ @
@@@@@@@ @ @@@@@@@ @ @@@@@@@ @ @@@@@@@ @ @@@@@@@ @ @@@@@@@ @
step 7 step 8 step 9 step 10 step 11 step 12
@@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @ @@@@@@@ @ @@@@@ @ @@@ @ @ @ @
@@@@@@@@@ @ @@@@@@@ @ @@@@@@@ @ @@@@@@@ @ @@@@@@@ @ @@@@@ @
@ @@@ @ @@@ @ @@@ @ @@@ @ @@@ @ @ @
@@@ @ @@@ @@@ @ @@@ @@@ @ @@@ @@@ @ @@@ @@@ @ @@@ @@@ @ @@@
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@@@@@@@ @ @@@@@@@ @ @@@@@@@ @ @@@@@@@ @ @@@@@@@ @ @@@@@@@ @Amaze Your Friends
You can still get the cross-platform C program to draw mazes from the command line. Compile with any standard C compiler. Put it in the "My Documents" folder, then go to Start->Run and type "cmd" in the box. type "amaze" for instructions. Terminal-savvy users may redirect the output to a file using a command like amaze 10 25 > maze.txt.
Is ASCII art not good enough? Would an image be better? This complicated line will convert the output into a .gif image on Linux: (requires installation of netpbm-progs and ImageMagick)
| `./amaze 40 69 >maz~tmp&&asciitopgm $(wc -lL maz~tmp) | convert - maze.gif&&rm maz~tmp` |
|---|
Programming
Writing JavaScript or C programs is not much different than giving a set of simple instructions or writing a shopping list. It's just a bit more exact since the computer is more selective about what commands it understands.
Those looking to get started with computer programming should pick whatever language interests them. I prefer Python, C, PHP, and JavaScript. Python code is clean and easy to read. It has its own help system and the on line manuals and tutorials are fairly good. C is more complicated. The main difference that I have encountered with C is the extra work setting aside memory to put things and then freeing it later. Other languages do this automatically; however, C is often many times faster and more optimized for the hardware. C gives the programmer more control over the internal workings of the computer. For this reason, C compilers exist for almost any type of hardware, making it inherently cross-platform. C will probably remain the dominant language for operating system developers.
Keep it going.Support development or hire an expert programmer for your next project.
Copyright © 2026 Henry Kroll III, thenerdshow.com This web page is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.