Here's some code I wrote a long time ago that generates a perfect maze, ie a maze with only one path. Press a key after each maze to generate another one. End up with x1 and y1 being coords of furthest path
` s=size of maze (2 or more)
` w=pixel size of boxes (1 or more)
` xs=startx (MUST be even)
` ys=starty (MUST be even)
s=40 : w=5 : xs=2 : ys=2
sync on : sync rate 0 : t=s*2+1
dim d(4,2) : d(1,1)=1 : d(2,2)=1 : d(3,1)=-1 : d(4,2)=-1
dim m(t,t)
randomize timer()
do
x=xs : y=ys : z=0 : zm=0
for f=1 to t : m(f,1)=1 : m(f,t)=1 : m(1,f)=1 : m(t,f)=1 : next f
rem generate maze
repeat
k=1
for f=1 to 3
r1=rnd(3)+1 : r2=rnd(3)+1 : p1=d(r1,1) : p2=d(r1,2) : d(r1,1)=d(r2,1) : d(r1,2)=d(r2,2) : d(r2,1)=p1 : d(r2,2)=p2
next f
for f=1 to 4
dx=d(f,1) : dy=d(f,2)
if m(x+dx,y+dy)=0
if m((2*dx)+x,(2*dy)+y)=0
m(x,y)=2 : m(x+dx,y+dy)=2 : x=x+(2*dx) : y=y+(2*dy) : inc z
if z > zm then zm=z : x1=x : y1=y
k=0 : exit
endif
endif
next f
if k=1
for f=1 to 4
dx=d(f,1) : dy=d(f,2) : if m(x+dx,y+dy)=2 then m(x,y)=3 : m(x+dx,y+dy)=3 : x=x+(2*dx) : y=y+(2*dy) : dec z : k=0 : exit
next f
endif
until k=1
m(x,y)=3
rem draw maze
cls : sync
for f=1 to t
for g=1 to t
c=m(f,g)
if (f=x1 and g=y1) or (f=xs and g=ys) or (c < 2)
if c < 2 : ink rgb(0,0,255),0 : else : ink rgb(0,255,0),0 : endif
box (g-1)*w,(f-1)*w,g*w,f*w
endif
next g
next f
sync
wait key
for f=1 to t : for g=1 to t : m(f,g)=0 : next g : next f
loop
Boo!