rem Challenge: random maze creator
rem Author: acelepage (challenge setter)
set display mode 1024,768,32
hide mouse
backdrop on
color backdrop 0
sync on
top=30
size=10
xoff=20
yoff=20
dim grid(top,top)
doagain:
xtop=1
ytop=1
for x=0 to top
  for y=0 to top
    grid(x,y)=0
  next y
next x
x=1
y=1
quit=0
count=0
while quit=0
  direction=rnd(3)
  accept=1
  xnew=x
  ynew=y
  select direction
    case 0
      xnew=x-2
      if xnew<0
        xnew=x
        accept=0
      endif
    endcase
    case 1
      ynew=y-2
      if ynew<0
        ynew=y
        accept=0
      endif
    endcase
    case 2
      xnew=x+2
      if xnew>top
        xnew=x
        accept=0
      endif
    endcase
    case 3
      ynew=y+2
      if ynew>top
        ynew=y
        accept=0
      endif
    endcase
  endselect
  if grid(xnew,ynew)>0
    accept=0
  endif
  if accept=1
    if x>xnew
      xsgn=-1
    else
      xsgn=1
    endif
    if y>ynew
      ysgn=-1
    else
      ysgn=1
    endif
    for i=x to xnew step xsgn
      for j=y to ynew step ysgn
        grid(i,j)=1
      next j
    next i
    x=xnew
    y=ynew
    count=0
  else
    inc count
    if count > 8
      if x-1>0 and grid(x-1,y)=1
        x=x-1
        grid(x+1,y)=4
        x=x-1
        grid(x+1,y)=4
      else
        if x+1<top and grid(x+1,y)=1
          x=x+1
          grid(x-1,y)=4
          x=x+1
          grid(x-1,y)=4
        else
          if y-1>0 and grid(x,y-1)=1
            y=y-1
            grid(x,y+1)=4
            y=y-1
            grid(x,y+1)=4
          else
            if y+1<top and grid(x,y+1)=1
              y=y+1
              grid(x,y-1)=4
              y=y+1
              grid(x,y-1)=4
            endif
          endif
        endif
      endif
      if x=1 and y=1 then quit=1
      if x=top-1 and y>ytop
        xtop=x
        ytop=y
      endif
    endif
  endif
  gosub showgrid
  sync
endwhile
grid(1,0)=1
grid(1,1)=1
grid(xtop,ytop)=1
grid(top,ytop)=1
gosub showgrid
ink rgb(255,0,0),0
line top*size+size+xoff,ytop*size+size*0.5+yoff,top*size+size*2+xoff,ytop*size+size*0.5+yoff
line top*size+size*2+xoff,ytop*size+size*0.5+yoff,top*size+size*1.5+xoff,ytop*size+yoff
line top*size+size*2+xoff,ytop*size+size*0.5+yoff,top*size+size*1.5+xoff,ytop*size+size+yoff
text xoff,yoff+top*size+size,"press [SPACE] for next maze"
 
sync
while not spacekey()
endwhile
goto doagain
end
 
showgrid:
  line xoff+size*1.5,yoff-size*0.5,xoff+size*1.5,yoff-size*1.5
  line xoff+size*1.5,yoff-size*0.5,xoff+size,yoff-size
  line xoff+size*1.5,yoff-size*0.5,xoff+size*2,yoff-size
  ink rgb(255,255,255),0
  line xoff,yoff,top*size+size+xoff,yoff
  line xoff,yoff,xoff,top*size+size+yoff
  line xoff,top*size+size+yoff,top*size+size+xoff,top*size+size+yoff
  line top*size+size+xoff,yoff,top*size+size+xoff,top*size+size+yoff
  for i=0 to top
    for j=0 to top
      if grid(i,j)=3 then grid(i,j)=1
      if grid(i,j)>0 and grid(i,j)<5
        ink rgb(255,255,255),0
        box i*size+xoff,j*size+yoff,i*size+size+xoff,j*size+size+yoff
      endif
      if grid(i,j)=5
        ink rgb(0,255,0),0
        box i*size+xoff,j*size+yoff,i*size+size+xoff,j*size+size+yoff
      endif
    next j
  next i
return