Here is a brief explanation of how it works. Trust me, it is not that difficult!
OK, here's a brief description. Imagine you want a maze of size 3, i.e. s=3
width of array is calc'd (t) = s*2+1 = 7
main array is initialised m(7,7) which gives you:
0000000
0000000
0000000
0000000
0000000
0000000
0000000
edges are set to '1'
1111111
1000001
1000001
1000001
1000001
1000001
1111111
the 'd' array is then setup. This is 4 direction variables for east,south,west,north
think of it as d(direction,1) = x movement, d(direction,2) = y movement
so if x was currently 2 and y was currently 6, you could go north by reducing y by 1.
Right, the hard bit:
I have set the start x=2 and y=2. THis means start at position 2 across(x) and 2 down(y)
First off is randomly sort the 4 directions you can go to. So you may end up with north,east,south,west
the method is to randomly pick a direction and leave a trail of breadcrumbs (2's) in the array until you are trapped. so you may end up with:
1111111
1222001
1002021
1002021
1002021
1002221
1111111
as you can see, you are now at top right and can no go futher.
when trapped, you keep backtracking along your breadcrumbs replacing breadcrumbs (2's) with 3's. You keep backtracking till you have another free space you can branch off to.
so, in our example, you would backtrack to:
1111111
1222031
1002031
1002031
1002031
1002331
1111111
you would now be at bottom middle position and can now go left.
1111111
1222031
1002031
1202031
1202031
1222331
1111111
you then backtrack. You would then finish because you have backtracked all the way back to the start
1111111
1333031
1003031
1303031
1303031
1333331
1111111
you are then left with a nice easy array where 1 and 0 = wall, and 3=corridor.
The end!
The programmer formerly known as sonic