Smart n Sweet Maze Maker (Source Included)

BlitzMax Forums/BlitzMax Programming/Smart n Sweet Maze Maker (Source Included)

dw817(Posted 2015) [#1]
With the learning of a new inkey$ routine, I thought I would share with you a neat little maze program I wrote. Unlike other maze programs, this one guarantees only one path through. Meaning, if you take a different course (starting either at the exit or beginning), you will not be able to finish it.

It is random each time and you can also change the size of it, too. Default it is a pretty simple maze, but increase the size and it gets rather tricky to circumnavigate to say the least.

Perfect for those dungeons where monsters lurk around every corner !

Enjoi !



'    ______________________________________
'   //                                   //
'  //  Smart N' Sweet Maze Maker v0.03  //
' //    Written by David W - 12/17/15  //
'//___________________________________//

' THANKS TO TOM TOAD FOR INKEY$() ROUTINE !

' KEYSTROKES:
' SPACE to knock out a wall
' R to rapid finish rest of map
' ENTER when maze drawn to exit
Strict
SeedRnd MilliSecs()
Global sizex=32,sizey=32 ' change to size of maze you want (must be EVEN # !)
Global recx=1024.0/sizex
Global recy=738/sizey
Global map[sizex+1,sizey+1]
Local a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
Local ax,ay,cc$

Local watch=1 ' change to 0 for instant maze making

' /// MAIN PROGRAM BEGINS ///

sizey:-1
sizex:-1

Graphics 1024,768
For i=0 To sizey
  For j=0 To sizex
    If(j*i)Mod 2=0
      map[j,i]=1
    ElseIf j*i>0 And j<sizex And i<sizey
      c:+1
    EndIf
  Next
Next

Repeat
  If f+watch=2
    map[x,y]=2
    nano
    Repeat
      Flip
      cc$=inkey$()
      If cc$=Chr$(27)
        End
      EndIf
      If Lower$(cc$)="r"
        watch=0
      EndIf
    Until cc$=" " Or watch=0
    map[x,y]=0
  EndIf
  Repeat
    If e=0
      e=4
      Repeat
        x=1+rn(sizex/2)*2
        y=1+rn(sizey/2)*2
        t=bricks(x,y)
      Until(f=0 And t=1)Or(f=1 And t=3)
      f=1
      r=rn(4)
    EndIf
    ax=0
    ay=0
    e:-1
    If r=0
      ay=-1
    ElseIf r=1
      ay=1
    ElseIf r=2
      ax=-1
    Else
      ax=1
    EndIf
    r=(r+1)Mod 4
    t=bricks(x+ax*2,y+ay*2)
  Until t=1Or t=6
  e=4
  x:+ax
  y:+ay
  p:+1
  r=rn(4) ' REM this out for a truly TWISTY maze
  map[x,y]=0
  x:+ax
  y:+ay
Until p=c-1
map[0,1]=0
map[sizex-1,sizey-2]=0
nano
Repeat
  Flip
Until GetChar()=13

Function rn(n)
  Return Rand(0,n-1)
EndFunction

Function bound(a,b,c)
  If a<b Then Return b
  If a>c Then Return c
  Return a
EndFunction

Function mymap(x,y)
  Return map[bound(x,0,sizex),bound(y,0,sizey)]
EndFunction

Function bricks(x,y)
Local i,j,c
  For i=-1To 1
    For j=-1To 1
      If mymap(x+j,y+i)=0
        c:+1
      EndIf
    Next
  Next
  Return c
EndFunction

Function nano()
Local i,j
  For i=0 To sizey-1
    For j=0 To sizex-1
      SetColor 255,0,0
      If map[j,i]=0 Then SetColor 0,0,0
      If map[j,i]=1 Then SetColor 192,128,64
      DrawRect j*recx,i*recy,recx,recy
    Next
  Next
EndFunction

Function inkey$()
  Return Chr(GetChar())
EndFunction