Rem Programme:   Maze Master
Rem Start date:  10th October 2007
Rem Edited date: 23rd October 2007
Rem Author:      Doc Evil
 
Rem Define arrays
 
Rem directions around current position
Dim direction (4,8)
Rem direction(1,direction)= xmovement
Rem direction(2,direction)= ymovement
Rem direction(3,direction)= map value
Rem direction(4,direction)= possible to move flag
 
Rem map of maze
Dim map(2048,2048)
 
Rem routes
Dim route(2,10000,1)
 
gosub DECLAIREVARIABLES
 
Gosub SETUPSCREEN
 
Gosub ERASEMAZE
 
Gosub GETMAZEFROMDATA
 
Gosub RESETMAPDATA
 
Gosub DRAWMAZE
 
Gosub DRAWCONTROLPANEL
 
Gosub REPORT
 
Rem *** MAIN LOOP STARTS HERE ***
Do
Rem put robot on screen if maze fits on screen
if scale>1
ink rgb(0,0,255),rgb(0,0,0)
box (xposition-1)*scale+3,(yposition-1)*scale+23,(xposition*scale)-3,yposition*scale+17
Endif
 
Rem Reached search limit
if attempts=15 and logic=5 and foundexit=0 then attempts=attempts-1:foundexit=-1:currently$="Waiting instruction"
if maximumattempts<9000 and attempts>maximumattempts and foundexit =0 then attempts=attempts-1:foundexit=-1 :currently$="Waiting instruction"
if maximumattempts<9000 and attempts>maximumattempts and foundexit =1 then attempts=attempts-1:currently$="Showing shortest route":showflag=1
 
Gosub REPORT
 
rem display shortest route
if showflag=1
gosub CLEARMAZE
gosub CONVERTMAZE
gosub DRAWMAZE
for rn=1 to shortestdistance#
xposition=route(2,rn,0)
yposition=route(2,rn,1)
gosub DRAWROUTE
next rn
gosub DRAWSTART
gosub DRAWEXIT
currently$="Waiting instruction"
showflag=0
endif
 
if speed=1 then b=50
if speed=2 then b=10
if speed<3
for a=1 to b
if mouseclick()then exit
wait 1
next a
Endif
 
Gosub TESTMOUSE
 
if currently$<>"Waiting instruction"
 
if currently$="Self-destruct initiated!" then exit
 
Rem Look in all squares adjacent to position
direction(1,1)=0 : direction(2,1)=-1 `  direction 1 = looking north
direction(1,5)=1 : direction(2,5)=-1 `  direction 5 = looking north east
direction(1,2)=1 : direction(2,2)=0 `   direction 2 = looking east
direction(1,6)=1 : direction(2,6)=1 `   direction 6 = looking south east
direction(1,3)=0 : direction(2,3)=1 `   direction 3 = looking south
direction(1,7)=-1 : direction(2,7)=1 `  direction 7 = looking south west
direction(1,4)=-1 : direction(2,4)=0 `  direction 4 = looking west
direction(1,8)=-1 : direction(2,8)=-1 ` direction 8 = looking north west
For d = 1 to 8
Rem this allows the maze to wrap round
if xposition+direction(1,d)>mazewidth then direction(1,d)=-mazewidth+1
if xposition+direction(1,d)<1 then direction(1,d)=mazewidth-1
if yposition+direction(2,d)>mazeheight then direction(2,d)=-mazeheight+1
if yposition+direction(2,d)<1 then direction(2,d)=mazeheight-1
Rem look in all adjacent squares
direction(3,d) = map(xposition + direction(1,d) , yposition + direction(2,d))
Next d
 
Rem See which directions are valid for movement
For d = 1 to 8
c = d : e = d : direction(4,d)=0
If d>4 then c=d-4 : e=d-3 : If e=5 then e=1
If direction(3,d)< wall and direction(3,c)< wall and direction(3,e) < wall then direction(4,d)=1
Next d
 
Rem Random move logic 1
if logic=1
direction = -1
while direction=-1
d=rnd(movediagonal-1)+1
if direction(4,d)=1 then direction=d
endwhile
Endif
 
Rem Random move but no u turns logic 2
if logic=2
d$=""
d=direction
if d<5 then b=d+2:if b>4 then b=b-4
if d>4 then b=d+2:if b>8 then b=b-4
for a=1 to movediagonal
if direction(4,a)=1 and a<>b then d$=d$+str$(a)
next a
if d$="" then d$=str$(b)
direction=val(mid$(d$,rnd(len(d$)-1)+1))
endif
 
Rem follow left wall logic 3
if logic=3
if direction=0 and direction(3,2)=>wall and direction(4,3)=1 then direction=3
if direction=0 and direction(3,3)=>wall and direction(4,4)=1 then direction=4
if direction=0 and direction(3,4)=>wall and direction(4,1)=1 then direction=1
if direction=0 and direction(3,1)=>wall and direction(4,2)=1 then direction=2
if direction=0
for a=1 to 4
if direction(4,a)=1 then direction=a:exit
next a
endif
d=direction
e=direction-1:if e=0 then e=4
f=direction+1:if f=5 then f=1
g=direction+4
h=direction+5:if h=9 then h=5
i=direction+2:if i=6 then i=1
j=direction+6:if j>8 then j=j-4
k=direction+7:if k>8 then k=k-8
direction=0
if direction(3,e)=> wall and direction(4,d)=1 then direction=d
if direction=0 and direction(3,j)=>wall and direction(4,e)=1 then direction=e
if direction=0 and direction(3,g)=>wall and direction(4,f)=1 then direction=f
if direction=0 and direction(3,h)=>wall and direction(4,i)=1 then direction=i
if direction=0 and direction(4,d)=1 then direction=d
if direction=0 and direction(3,d)=>wall and direction(3,g)=>wall and direction(4,f)=1 then direction=f
if direction=0 and direction(3,f)=>wall and direction(3,g)=>wall and direction(4,i)=1 then direction=i
if direction=0 and direction(3,i)=>wall and direction(3,j)=>wall and direction(4,e)=1 then direction=e
if direction=0 and direction(3,e)=>wall and direction(3,k)=>wall and direction(4,d)=1 then direction=d
if direction=0 and direction(4,d)=1 then direction=d
if direction=0 and direction(4,f)=1 then direction=f
if direction=0 and direction(4,i)=1 then direction=i
if direction=0 and direction(4,e)=1 then direction=e
endif
 
Rem Find least visited adjacent square to move to logic 5
if logic=5
direction = 0
lowvalue = wall
For d = 1 to movediagonal :rem  set to 4 for horizontal and vertical set to 8 for all directions
If direction(4,d)=1 and direction(3,d)<=lowvalue then lowvalue=direction(3,d): direction = d
Next d
if randommove=1
for d= 1 to movediagonal
if direction(3,d)=lowvalue and direction(4,d)=1 and rnd(1)=1 then direction=d
next d
Endif
endif
 
 
Rem Update map by incrementing the number of visits to current square
map(xposition,yposition)=map(xposition,yposition)+1
 
Rem If map square has been visited maximumattempts, call it a day
rem if logic=5 and map(xposition,yposition)=maximumattempts then currently$="I'm calling it a day"+str$(map(xposition,yposition)) :gosub report :exit
 
 
Rem Set alterflag to only update map as a dead end if not at start or exit square
alterflag=1
if xposition=xexit and yposition=yexit or xposition=xstart and yposition=ystart then alterflag=0
 
Rem Test to see if a dead end and turn square into wall
deadendvalue=0
eraserobot=0
deadendvalue=direction(3,1)+direction(3,2)+direction(3,3)+direction(3,4)
If rememberdeadends=1 and alterflag=1 and deadendvalue=>wall*3 then Gosub DRAWFALSEWALL
 
Rem Turn redundant space into walls
if rememberdeadends=1 and alterflag=1
if direction(3,1)=>wall and direction(3,2)=>wall and direction(3,4)<wall and direction(3,7)<wall and direction(3,3)<wall then gosub DRAWFALSEWALL
if direction(3,2)=>wall and direction(3,3)=>wall and direction(3,4)<wall and direction(3,8)<wall and direction(3,1)<wall then Gosub DRAWFALSEWALL
if direction(3,3)=>wall and direction(3,4)=>wall and direction(3,1)<wall and direction(3,5)<wall and direction(3,2)<wall then Gosub DRAWFALSEWALL
if direction(3,4)=>wall and direction(3,1)=>wall and direction(3,3)<wall and direction(3,6)<wall and direction(3,2)<wall then Gosub DRAWFALSEWALL
rem if direction(3,4)=>wall and direction(3,1)<wall and direction(3,5)<wall and direction(3,2)<wall and direction(3,6)<wall and direction(3,3)<wall then gosub drawfalsewall
rem if direction(3,1)=>wall and direction(3,2)<wall and direction(3,6)<wall and direction(3,3)<wall and direction(3,7)<wall and direction(3,4)<wall then gosub drawfalsewall
rem if direction(3,2)=>wall and direction(3,3)<wall and direction(3,7)<wall and direction(3,4)<wall and direction(3,8)<wall and direction(3,1)<wall then gosub drawfalsewall
rem if direction(3,3)=>wall and direction(3,4)<wall and direction(3,8)<wall and direction(3,1)<wall and direction(3,5)<wall and direction(3,2)<wall then gosub drawfalsewall
endif
 
Rem Erase robot from screen and display number of visits to that square
if eraserobot=0
if scale>1
Ink 0,0
box (xposition-1)*scale,(yposition-1)*scale+20,xposition*scale,yposition*scale+20
endif
rem if scale>13 and map(xposition,yposition)<100 then ink rgb(0,150,0),rgb(0,0,0) : set text opaque: text (xposition-1)*scale,(yposition-1)*scale+20,str$(map(xposition,yposition))
endif
 
Rem show start or exit if robot on that square
if xposition=xstart and yposition=ystart then gosub DRAWSTART
if xposition=xexit and yposition=yexit then gosub DRAWEXIT
 
Rem If searchdistance = maximumrange then abandon search and go back to start
If int(searchdistance#)=>maximumrange and maximumrange<>9000
xposition=xstart : yposition=ystart
direction=0 : searchdistance#=0.0:attempts=attempts+1 :operations =1
Endif
 
Rem Update robot coordinates to new square to visit
if direction>0
xposition=xposition+direction(1,direction)
yposition=yposition+direction(2,direction)
if direction<5 then searchdistance#=searchdistance#+1.0
if direction>4 then searchdistance#=searchdistance#+1.414
Rem record position for showing shortest route
distance=int(searchdistance#)
route(1,distance,0)=xposition
route(1,distance,1)=yposition
operations = operations +1
Rem If found exit don't search more than shortest distance found if complete search = NO
if completesearch=0 and searchdistance#>shortestdistance#
xposition=xstart : yposition=ystart
operations =1
Endif
Rem If reached exit go back to start and do it all again!
if xposition=xexit and yposition=yexit
rem found a shorter route
if searchdistance#<shortestdistance# then shortestdistance#=int(searchdistance#*1000.0)/1000.0:leastoperations=operations
dt=int(searchdistance#)
for distance=1 to dt
route(2,distance,0)=route(1,distance,0)
route(2,distance,1)=route(1,distance,1)
next distance
 
xposition=xstart : yposition=ystart
direction=0 : searchdistance#=0.0:operations=1:foundexit=1
gosub CLEARPATH : gosub DRAWMAZE
endif
Rem If robot back at start so increment attempts and reset search distance
if xposition=xstart and yposition=ystart then searchdistance#=0.0:attempts=attempts+1
endif
 
totaloperations=totaloperations+1
endif
loop
rem Self-destruct sequence
set text transparent
set text size 100
for a=10 to 0 step -1
ink rgb(127,127,127),rgb(0,0,0)
box 860,510,980,600
ink rgb(200,0,0),rgb(127,127,127)
text 860,510,right$("0"+str$(a),2)
wait 1000
next a
get image 1,0,0,1023,767
for c=1 to 40
a=rnd(20):b=rnd(20)
x=10-a:y=10-b
cls
paste image 1,x,y,0
wait 125
next c
end
 
Rem *** Subrutines start here ***
End
 
SETUPSCREEN:
Set display mode 1024,768,32
Ink rgb(0,200,0),rgb(0,0,0)
Cls
set text transparent
set text font "Lucida Console"
set text size 12
set text to normal
text 400,6,"MAZE MASTER - A Doc Evil Creation"
return
 
DRAWCONTROLPANEL:
ink rgb(127,127,127),rgb(0,0,0)
box 815,20,1023,767
ink rgb(200,200,200),rgb(0,0,0)
line 814,1023,814,20
line 814,20,1023,20
ink rgb(54,54,54),rgb(0,0,0)
line 1023,21,1023,767
line 1023,767,815,767
ink rgb(0,0,0),rgb(0,0,0)
set text transparent
text 835,25,"Doc Evil's Control Panel"
xbutton=820:ybutton=40:lbutton=43:button$="Load":gosub drawbutton
xbutton=870:ybutton=40:lbutton=43:button$="Save":gosub drawbutton
xwindow=920:ywindow=40:lwindow=98:window$=left$(filename$,13):gosub drawtextwindow
xbutton=820:ybutton=60:lbutton=62:button$="Default":gosub drawbutton
xbutton=889:ybutton=60:lbutton=60:button$="Edit":gosub drawbutton
xbutton=956:ybutton=60:lbutton=62:button$="Random":gosub drawbutton
set text transparent
ink rgb(255,255,255), rgb(0,0,0)
text 820,82,"Size"
xwindow=860:ywindow=80:lwindow=69:window$=str$(mazewidth)+"x"+str$(mazeheight):gosub drawtextwindow
set text transparent
ink rgb(255,255,255), rgb(0,0,0)
text 820,102,"Start"
xwindow=860:ywindow=100:lwindow=69:window$=str$(xstart)+","+str$(ystart):gosub drawtextwindow
set text transparent
ink rgb(255,255,255), rgb(0,0,0)
text 820,122,"Exit"
xwindow=860:ywindow=120:lwindow=69:window$=str$(xexit)+","+str$(yexit):gosub drawtextwindow
xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow
xwindow=820:ywindow=160:lwindow=198:window$="":gosub drawtextwindow
 
ink rgb(0,0,0),rgb(0,0,0)
set text transparent
text 855,222,"Solving Parameters"
 
set text transparent
ink rgb(255,255,255), rgb(0,0,0)
text 820,242,"Maximum Attempt"
xbutton=932:ybutton=240:lbutton=20:button$="-":gosub drawbutton
xbutton=959:ybutton=240:lbutton=20:button$="+":gosub drawbutton
window$=str$(maximumattempts)
window$=str$(maximumattempts):if maximumattempts=9000 then window$="None"
xwindow=985:ywindow=240:lwindow=33:gosub drawtextwindow
set text transparent
ink rgb(255,255,255), rgb(0,0,0)
text 820,262,"Maximum Range"
xbutton=932:ybutton=260:lbutton=20:button$="-":gosub drawbutton
xbutton=959:ybutton=260:lbutton=20:button$="+":gosub drawbutton
window$=str$(maximumrange):if maximumrange=9000 then window$="None"
xwindow=985:ywindow=260:lwindow=33:gosub drawtextwindow
xbutton=820:ybutton=280:lbutton=132:button$="Remember Dead Ends":gosub drawbutton
if logic=3 then rememberdeadends=0
if rememberdeadends=0 then window$="NO"
if rememberdeadends=1 then window$="YES"
xwindow=959:ywindow=280:lwindow=26:gosub drawtextwindow
 
xbutton=820:ybutton=300:lbutton=132:button$="Complete Search":gosub drawbutton
if completesearch=0 then window$="NO"
if completesearch=1 then window$="YES"
xwindow=959:ywindow=300:lwindow=26:gosub drawtextwindow
 
xbutton=820:ybutton=320:lbutton=105:button$="Diagonal Move":gosub drawbutton
if movediagonal=4 then window$="NO"
if movediagonal=8 then window$="YES"
xwindow=932:ywindow=320:lwindow=26:gosub drawtextwindow
 
xbutton=820:ybutton=340:lbutton=105:button$="Random Move":gosub drawbutton
if randommove=0 then window$="NO"
if randommove=1 then window$="YES"
xwindow=932:ywindow=340:lwindow=26:gosub drawtextwindow
 
xbutton=820:ybutton=360:lbutton=50:button$="Logic":gosub drawbutton
if logic=1 then window$="Random Task movement"
if logic=2 then window$="Rnd, NO going back"
if logic=3 then window$="Follow left wall"
if logic=4 then window$="Not defined yet"
if logic=5 then window$="Boldly go"
xwindow=877:ywindow=360:lwindow=141:gosub drawtextwindow
 
ink rgb(0,0,0),rgb(0,0,0)
set text transparent
text 860,422,"Control Functions"
xbutton=820:ybutton=440:lbutton=62:button$="Speed":gosub drawbutton
if speed=1 then window$="Too frickin' slow"
if speed=2 then window$="Mini Me speed"
if speed=3 then window$="'Turbo'"
xwindow=889:ywindow=440:lwindow=129:gosub drawtextwindow
xbutton=820:ybutton=460:lbutton=62:button$="Solve":gosub drawbutton
xbutton=889:ybutton=460:lbutton=60:button$="Abort":gosub drawbutton
xbutton=956:ybutton=460:lbutton=62:button$="Clear":gosub drawbutton
 
xbutton=820:ybutton=480:lbutton=198:button$="SELF-DESTRUCT SEQUENCE":gosub drawbutton
return
 
RANDOMDIALOGUE:
if timer()>lasttimer+8000 or lasttimer=0
lasttimer=timer()
rnddialogue=rnd(20)
window$=""
if rnddialogue=0 then window$="I'll not tolorate failiure!"
if rnddialogue=1 then window$="Fetch me Mr Bigglesworth."
if rnddialogue=2 then window$="Kill Austin Powers!"
if rnddialogue=3 then window$="I want a warhead - NOW!"
if rnddialogue=4 then window$="Get me Austin's mojo."
if rnddialogue=5 then window$="This is my underground lair."
if rnddialogue=6 then window$="Throw me a frikin' bone!"
if rnddialogue=7 then window$="Eliminate someone - ANYONE!"
if rnddialogue=8 then window$="We have no sharks!"
if rnddialogue=9 then window$="I want an hefty ransom!"
if rnddialogue=10 then window$="Find me a hollow volcano."
if rnddialogue=11 then window$="Down Mini Me, down!"
if rnddialogue=12 then window$="It's 'Doctor Evil'."
if rnddialogue=13 then window$="Any sharks with lasers?"
if rnddialogue=14 then window$="Evil is my second name."
if rnddialogue=15 then window$="Give me 100 billion dollars!"
if rnddialogue=16 then window$="Zip it - zip - zip - zip!"
if rnddialogue=17 then window$="Muhahahaha (evil laugh)"
if rnddialogue=18 then window$="Scotty don't!"
 
 
xwindow=820:ywindow=160:lwindow=198:gosub drawtextwindow
endif
return
 
REPORT:
ink rgb(0,0,0),rgb(0,0,0)
set text transparent
text 855,602,"Number Two's Report"
xwindow=820:ywindow=620:lwindow=198:window$=currently$:gosub drawtextwindow
set text transparent
ink rgb(255,255,255), rgb(0,0,0)
text 820,642,"Attempt"
xwindow=874:ywindow=640:lwindow=26:window$=str$(attempts):gosub drawtextwindow
ink rgb(255,255,255), rgb(0,0,0)
text 910,642,"Distance"
xwindow=972:ywindow=640:lwindow=46:window$=left$(str$(int(searchdistance#*1000.0)/1000.0),6):gosub drawtextwindow
text 820,662,"Current Operation"
xwindow=952:ywindow=660:lwindow=66:window$=left$(str$(operations),9):gosub drawtextwindow
text 820,682,"Current Location"
xwindow=952:ywindow=680:lwindow=66:window$=str$(xposition)+","+str$(yposition):gosub drawtextwindow
 
window$=""
extra$=""
if foundexit=-1 and logic=5 and maximumattempts >20 then window$="Maze is impossible!":extra$="Unless we use a 'Laser'"
if foundexit=-1 and logic=5 and maximumattempts <9 then window$="Can't find exit!":extra$="Increse maximum attempts."
 
if foundexit=-1 and logic<5 then window$="Can't find exit!": extra$="Try a better logic."
if foundexit=0 and attempts>0 then window$="Exit not yet found!"
if foundexit=1 then a$=str$(int(shortestdistance#*1000)) : window$="Exit distance "+left$(a$,len(a$)-3)+"."+right$(a$,3)
 
xwindow=820:ywindow=700:lwindow=198:gosub drawtextwindow
xwindow=820:ywindow=720:lwindow=198:window$=extra$:gosub drawtextwindow
text 820,742,"Total Operations"
xwindow=952:ywindow=740:lwindow=66:window$=left$(str$(totaloperations),9):gosub drawtextwindow
return
 
DRAWBUTTON:
ink rgb(127,127,127), rgb(0,0,0)
box xbutton,ybutton,xbutton+lbutton,ybutton+15
ink rgb(200,200,200), rgb(0,0,0)
line xbutton-1,ybutton+15,xbutton-1,ybutton-1
line xbutton-1,ybutton-1,xbutton+lbutton,ybutton-1
ink rgb(54,54,54),rgb(0,0,0)
line xbutton,ybutton+15,xbutton+lbutton,ybutton+15
line xbutton+lbutton,ybutton+15,xbutton+lbutton,ybutton-2
set text transparent
ink rgb(255,255,255), rgb(0,0,0)
text (xbutton+(lbutton/2))-(text width(button$)/2),ybutton+2,button$
Return
 
DRAWTEXTWINDOW:
ink rgb(150,150,150), rgb(0,0,0)
box xwindow,ywindow,xwindow+lwindow,ywindow+15
ink rgb(54,54,54), rgb(0,0,0)
line xwindow-1,ywindow+15,xwindow-1,ywindow-1
line xwindow-1,ywindow-1,xwindow+lwindow,ywindow-1
ink rgb(200,200,200),rgb(0,0,0)
line xwindow,ywindow+15,xwindow+lwindow,ywindow+15
line xwindow+lwindow,ywindow+15,xwindow+lwindow,ywindow-2
set text transparent
ink rgb(255,255,255), rgb(0,0,0)
text xwindow+3,ywindow+2,window$
Return
 
TEXTINPUT:
do
if mouseclick()=0 and inkey$()="" then exit
loop
characterstart=1
before$=textinput$
carpos=1
insert=1
 
do
textinput$=left$(textinput$+space$(maline),maline)
if carpos>displaysize then characterstart=carpos-displaysize+1
if carpos<characterstart then characterstart=carpos
rem display text line
a$=right$(textinput$,maline-characterstart+1)
ink rgb(255,255,255),rgb(150,150,150)
set text opaque
text xloccurs,yloccurs,left$(a$,displaysize)
rem display cursor on current character
rem do
ink rgb(255,255,255),rgb(150,150,150)
set text transparent
text xloccurs+((carpos-characterstart)*7),yloccurs,"_"
rem wait for key input
do
khit$=inkey$()
keyscancode=scancode()
keycode=asc(khit$)
if mouseclick()=1 then keycode=13
if khit$<>"" or keycode=13 or keyscancode=210 or keyscancode=203 or keyscancode=205 or keyscancode=14 or keyscancode=211 or keyscancode=199 or keyscancode=207 then exit
 
loop
 
rem remove cursor
a$=right$(textinput$,maline-characterstart+1)
ink rgb(255,255,255),rgb(150,150,150)
set text opaque
text xloccurs,yloccurs,left$(a$,displaysize)
 
if keycode=13
for a=len(textinput$) to 1 step -1
if mid$(textinput$,a)<>" " then exit
next a
textinput$=left$(textinput$,a)
do
if mouseclick()=0 then exit
loop
return
endif
 
wait 125
rem move cursor left
if keyscancode=203
if carpos>1 then carpos=carpos-1
endif
rem move cursor right
if keyscancode=205
if carpos<maline then carpos=carpos+1
endif
rem delete character to left
if keyscancode=14 and carpos>1
textinput$=left$(textinput$,carpos-2)+right$(textinput$,maline-carpos+1)+" "
carpos=carpos-1
if carpos<characterstart then characterstart=characterstart-1
endif
 
rem delete character
if keyscancode=211
textinput$=left$(textinput$,carpos-1)+right$(textinput$,maline-carpos)+" "
endif
rem move to start of input home
if keyscancode=199
carpos=1:characterstart=1
endif
rem move to end of input end
if keyscancode=207
for a=maline to 1 step -1
if mid$(textinput$,a)<>" " then exit
next a
a=a+1
if a>maline then a=maline
carpos=a
endif
 
rem insert on or off
if keyscancode=210 then insert=insert+1:if insert=2 then insert=0
 
rem display new character
if keycode>13 and keycode<127
textinput$=left$(textinput$,carpos-1)+khit$+right$(textinput$,maline-carpos+insert)
carpos=carpos+1
if carpos>maline then carpos=maline
if carpos-characterstart=displaysize then characterstart=characterstart+1
ink rgb(255,255,255),rgb(150,150,150)
set text opaque
rem text xloccurs+((carpos-characterstart)*7),yloccurs,khit$
rem exit
endif
rem loop
loop
return
 
TESTMOUSE:
Do
randomize timer()
gosub RANDOMDIALOGUE
if mouseclick()
xm=mousex():ym=mousey()
 
rem load
if xm>820 and xm<863 and ym>40 and ym<54
textinput$=filename$
maline=20:xloccurs=923:yloccurs=42:displaysize=13
xwindow=820:ywindow=140:lwindow=198:window$="Enter file name to load":gosub drawtextwindow
gosub TEXTINPUT
fileflag=0
if lower$(right$(textinput$,4))<>".bmp" then textinput$=textinput$+".bmp"
gridsize=1
if file exist(textinput$)
fileflag=1
filename$=textinput$
rem remstart
maline=3:xloccurs=983:yloccurs=142:displaysize=3
xwindow=820:ywindow=140:lwindow=198:window$="Pixels per grid square":gosub drawtextwindow
textinput$="1"
gosub TEXTINPUT
gridsize=val(texinput$)
if gridsize<1 then gridsize=1
 
rem remend
 
xwindow=820:ywindow=140:lwindow=198:window$="Loading file...":gosub drawtextwindow
wait 500
 
load bitmap filename$,1
mazewidth=bitmap width(1)/gridsize
mazeheight=bitmap height(1)/gridsize
gosub ERASEMAZE
set current bitmap 1
for y=1 to mazeheight
for x=1 to mazewidth
c=point((x-1)*gridsize,(y-1)*gridsize)
r=rgbr(c)
g=rgbg(c)
b=rgbb(c)
if c=0 then map(x,y)=wall
if r>127 and g>127 and b>127 then map(x,y)=0
if g>r and g>b then xstart=x:ystart=y:map(x,y)=0
if r>g and r>b then xexit=x:yexit=y:map(x,y)=0
next x
next y
set current bitmap 0
delete bitmap 1
gosub RESETMAPDATA
gosub DRAWMAZE
xwindow=820:ywindow=140:lwindow=198:window$="File loaded.":gosub drawtextwindow
wait 250
xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow
endif
if fileflag=0
xwindow=820:ywindow=140:lwindow=198:window$="Fricken' file not found!":gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow
endif
 
endif
 
rem save
if xm>870 and xm<913 and ym>40 and ym<54
textinput$=filename$
maline=20:xloccurs=923:yloccurs=42:displaysize=13
xwindow=820:ywindow=140:lwindow=198:window$="Enter file name to save":gosub drawtextwindow
gosub TEXTINPUT
fileflag$="Y"
if lower$(right$(textinput$,4))<>".bmp" then textinput$=textinput$+".bmp"
filename$=textinput$
if file exist(filename$)
xwindow=820:ywindow=140:lwindow=198:window$="File already exists!":gosub drawtextwindow
xwindow=820:ywindow=160:lwindow=198:window$="Eliminate it (Y/N)?":gosub drawtextwindow
do
fileflag$=upper$(inkey$())
if fileflag$="Y" or fileflag$="N" then exit
loop
xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow
xwindow=820:ywindow=160:lwindow=198:window$="":gosub drawtextwindow
endif
 
if fileflag$="Y"
xwindow=820:ywindow=140:lwindow=198:window$="Saving file...":gosub drawtextwindow
xwindow=820:ywindow=160:lwindow=198:window$="1 pixel per grid square.":gosub drawtextwindow
wait 2000
a=mazewidth:if mazeheight>mazewidth then a=mazeheight
bitmapwidth=0
for b=3 to 11
if 2^b=>a then bitmapwidth=2^b:exit
next b
if bitmapwidth=0 then wait key:end
create bitmap 1,bitmapwidth,bitmapwidth
for y=1 to mazeheight
for x=1 to mazewidth
if map(x,y)<>wall then dot x-1,y-1,rgb(255,255,255)
if map(x,y)=wall then dot x-1,y-1,rgb(0,0,0)
next x
next y
dot xstart-1,ystart-1,rgb(0,255,0)
dot xexit-1,yexit-1,rgb(255,0,0)
get image 1,0,0,bitmapwidth,bitmapwidth,1
save image filename$,1
delete image 1
set current bitmap 0
xwindow=820:ywindow=140:lwindow=198:window$="File saved.":gosub drawtextwindow
wait 2000
xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow
xwindow=820:ywindow=160:lwindow=198:window$="":gosub drawtextwindow
endif
endif
 
remstart rem file name
if xm>920 and xm<1018 and ym>40 and ym<54
textinput$=filename$
maline=20:xloccurs=923:yloccurs=42:displaysize=13
xwindow=820:ywindow=140:lwindow=198:window$="Enter file name":gosub drawtextwindow
gosub TEXTINPUT
filename$=textinput$
endif
remend
 
rem default read from data in programme
if xm>820 and xm<882 and ym>60 and ym<74
Gosub ERASEMAZE
Gosub DECLAIREVARIABLES
Gosub GETMAZEFROMDATA
Gosub DRAWMAZE
endif
 
rem edit maze
if xm>889 and xm<949 and ym>60 and ym<74
xwindow=820:ywindow=140:lwindow=198:window$="Left mouse = wall":gosub drawtextwindow
xwindow=820:ywindow=160:lwindow=198:window$="Right mouse = Path":gosub drawtextwindow
xwindow=820:ywindow=620:lwindow=198:window$="Editing maze":gosub drawtextwindow
wait 250
do
if scale<=1 then xwindow=820:ywindow=140:lwindow=198:window$="Maze too large to edit!":gosub drawtextwindow:wait 500:exit
xm=mousex():ym=mousey()
mc=mouseclick()
if xm=>0 and xm<=811 and ym=>20 and ym<=767
x=(xm/scale+1)
y=((ym-20)/scale+1)
if x<=mazewidth and y<=mazeheight then xwindow=952:ywindow=680:lwindow=66:window$=str$(x)+","+str$(y):gosub drawtextwindow
if x<=mazewidth and y<=mazeheight and mc=1 then map(x,y)=wall:ink rgb(200,200,200),rgb(0,0,0):box (x-1)*scale,(y-1)*scale+20,x*scale,y*scale+20
if x<=mazewidth and y<=mazeheight and mc=2 then map(x,y)=0:ink rgb(0,0,0),rgb(0,0,0):box (x-1)*scale,(y-1)*scale+20,x*scale,y*scale+20
endif
if mc=1 and xm>819 then exit
loop
xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow
xwindow=820:ywindow=160:lwindow=198:window$="":gosub drawtextwindow
 
endif
 
rem random maze
if xm>956 and xm<1018 and ym>60 and ym<74
gosub ERASEMAZE
xstart=rnd(mazewidth-3)+2:ystart=rnd(mazeheight-3)+2
x=xstart:y=ystart
map(x,y)=3
do
do
do
nx=x:ny=y
move=move-1
if move<1 then d=rnd(3)+1:move=rnd(3)+1
if d=1 and y>2 then ny=y-1:exit
if d=2 and x<mazewidth-1 then nx=x+1:exit
if d=3 and y<mazeheight-1 then ny=y+1:exit
if d=4 and x>2 then nx=x-1:exit
loop
flag=0
if map(nx,ny-1)>0 and map(nx+1,ny-1)>0 and map(nx+1,ny)>0 then flag=1
if map(nx+1,ny)>0 and map(nx+1,ny+1)>0 and map(nx,ny+1)>0 then flag=1
if map(nx,ny+1)>0 and map(nx-1,ny+1)>0 and map(nx-1,ny)>0 then flag=1
if map(nx-1,ny)>0 and map(nx-1,ny-1)>0 and map(nx,ny-1)>0 then flag=1
deadend=rnd(3)+1
if d=1 and deadend<>1 and map(nx,ny)=0 and ( map(nx-1,ny)=1 or map(nx-1,ny-1)=1 or map(nx,ny-1)=1 or map(nx+1,ny-1)=1 or map(nx+1,ny)=1 ) then flag=1
if d=2 and deadend<>1 and map(nx,ny)=0 and ( map(nx,ny-1)=1 or map(nx+1,ny-1)=1 or map(nx+1,ny)=1 or map(nx+1,ny+1)=1 or map(nx,ny+1)=1 ) then flag=1
if d=3 and deadend<>1 and map(nx,ny)=0 and ( map(nx+1,ny)=1 or map(nx+1,ny+1)=1 or map(nx,ny+1)=1 or map(nx-1,ny+1)=1 or map(nx-1,ny)=1 ) then flag=1
if d=4 and deadend<>1 and map(nx,ny)=0 and ( map(nx,ny+1)=1 or map(nx-1,ny+1)=1 or map(nx-1,ny)=1 or map(nx-1,ny-1)=1 or map(nx,ny-1)=1 ) then flag=1
if flag=0 then exit
loop
gosub RANDOMDIALOGUE
x=nx:y=ny
if map(x,y)=0 then map(x,y)=1
moves#=moves#+1.0
pd#=moves#/(mazewidth*mazeheight*2)*100.0
currently$="Generating maze "+str$(int(pd#))+"%":gosub REPORT
if moves# => mazewidth*mazeheight*2 or mouseclick() then exit
loop
currently$="Waiting instruction":gosub REPORT
map(x,y)=4
for y=1 to mazeheight
for x=1 to mazewidth
if map(x,y)=0 then map(x,y)=wall
if map(x,y)=1 then map(x,y)=0
if map(x,y)=3 then map(x,y)=0: xstart=x : ystart=y
if map(x,y)=4 then map(x,y)=0: xexit=x : yexit=y
next x
next y
gosub RESETMAPDATA
gosub DRAWMAZE
endif
 
rem maze size
if xm>860 and xm<929 and ym>80 and ym<94
do
maline=9:textinput$=str$(mazewidth)+"x"+str$(mazeheight):xloccurs=863:yloccurs=82:displaysize=9
xwindow=820:ywindow=140:lwindow=198:window$="Enter size width x height":gosub drawtextwindow
gosub TEXTINPUT
b=0:x=0:y=0
for a=1 to len(textinput$)
if mid$(textinput$,a)="x" or mid$(textinput$,a)="X" then b=a:exit
next a
if b=0 then xwindow=820:ywindow=140:lwindow=198:window$="Seperate with a x":gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow
if b>0 then x=val(left$(textinput$,b-1)):if x<8 or x>170 then xwindow=820:ywindow=140:lwindow=198:window$="Invalid width (8-170)" : gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow:x=0
if b>0 and x>0 then y=val(right$(textinput$,len(textinput$)-b)):if y<8 or y>170 then xwindow=820:ywindow=140:lwindow=198:window$="Invalid height (8-170)" : gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow:y=0
if x>0 and y>0 then exit
loop
mazewidth=x:mazeheight=y
if xstart>mazewidth or ystart>mazeheight then xstart=2:ystart=2
if xexit>mazewidth or yexit>mazeheight then xexit=mazewidth-1:yexit=mazeheight-1
gosub CLEARMAZE
gosub CONVERTMAZE
gosub DRAWMAZE
endif
 
rem start
if xm>860 and xm<929 and ym>100 and ym<114
do
maline=9:textinput$=str$(xstart)+","+str$(ystart):xloccurs=863:yloccurs=102:displaysize=9
xwindow=820:ywindow=140:lwindow=198:window$="Enter start location x,y":gosub drawtextwindow
gosub TEXTINPUT
b=0:x=0:y=0
for a=1 to len(textinput$)
if mid$(textinput$,a)="," then b=a:exit
next a
if b=0 then xwindow=820:ywindow=140:lwindow=198:window$="Seperate x and y with a ,":gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow
if b>0 then x=val(left$(textinput$,b-1)):if x<1 or x>mazewidth then xwindow=820:ywindow=140:lwindow=198:window$="Not a valid x value" : gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow:x=0
if b>0 and x>0 then y=val(right$(textinput$,len(textinput$)-b)):if y<1 or y>mazeheight then xwindow=820:ywindow=140:lwindow=198:window$="Not a valid y value" : gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow:y=0
if x>0 and y>0 then exit
loop
if scale>1
ink rgb(0,0,0),rgb(0,0,0)
box (xstart-1)*scale,(ystart-1)*scale+20,xstart*scale,ystart*scale+20
endif
xstart=x:ystart=y:xposition=xstart:yposition=ystart
gosub DRAWSTART
endif
 
rem exit
if xm>860 and xm<929 and ym>120 and ym<134
do
maline=9:textinput$=str$(xexit)+","+str$(yexit):xloccurs=863:yloccurs=122:displaysize=9
xwindow=820:ywindow=140:lwindow=198:window$="Enter exit location x,y":gosub drawtextwindow
gosub TEXTINPUT
b=0:x=0:y=0
for a=1 to len(textinput$)
if mid$(textinput$,a)="," then b=a:exit
next a
if b=0 then xwindow=820:ywindow=140:lwindow=198:window$="Seperate x and y with a ,":gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow
if b>0 then x=val(left$(textinput$,b-1)):if x<1 or x>mazewidth then xwindow=820:ywindow=140:lwindow=198:window$="Not a valid x value" : gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow:x=0
if b>0 and x>0 then y=val(right$(textinput$,len(textinput$)-b)):if y<1 or y>mazeheight then xwindow=820:ywindow=140:lwindow=198:window$="Not a valid y value" : gosub drawtextwindow : wait 2000:xwindow=820:ywindow=140:lwindow=198:window$="":gosub drawtextwindow:y=0
if x>0 and y>0 then exit
loop
if scale>1
ink rgb(0,0,0),rgb(0,0,0)
box (xexit-1)*scale,(yexit-1)*scale+20,xexit*scale,yexit*scale+20
endif
xexit=x:yexit=y
gosub DRAWEXIT
endif
 
rem remember dead ends
if xm>820 and xm<952 and ym>280 and ym<294 and logic<>3 then rememberdeadends=rememberdeadends+1 : if rememberdeadends=2 then rememberdeadends=0
rem complete search
if xm>820 and xm<925 and ym>300 and ym<314 then completesearch=completesearch+1:if completesearch=2 then completesearch=0
rem diagonal
if xm>820 and xm<925 and ym>320 and ym<334 and logic<>3 then movediagonal=movediagonal+4:if movediagonal=12 then movediagonal=4
rem random move
if xm>820 and xm<925 and ym>340 and ym<354 and logic<>3 and logic<>1 then randommove=randommove+1:if randommove=2 then randommove=0
 
rem logic
if xm>820 and xm<870 and ym>360 and ym<374
logic=logic+1 : if logic=6 then logic =1
if logic=1 then randommove=1:maximumattempts=10
if logic=3 then movediagonal=4:rememberdeadends=0:randommove=0:maximumattempts=5
if logic=4 then logic=5
if logic=5 then maximumattempts=8
endif
 
 
rem maximum attempts
a=len(str$(maximumattempts))-1 : b=10^a
if xm>932 and xm<952 and ym>240 and ym<254 and maximumattempts > 1 then maximumattempts=maximumattempts-b
if maximumattempts=0 then maximumattempts=b-(10^(a-1))
if xm>959 and xm<979 and ym>240 and ym<254 and maximumattempts < 9000 then maximumattempts=maximumattempts+b
 
rem maximum range
a=len(str$(maximumrange))-1 : b=10^a
if xm>932 and xm<952 and ym>260 and ym<274 and maximumrange > 1 then maximumrange=maximumrange-b
if maximumrange=0 then maximumrange=b-(10^(a-1))
if xm>959 and xm<979 and ym>260 and ym<274 and maximumrange < 9000 then maximumrange=maximumrange+b
 
rem speed
if xm>820 and xm<882 and ym>440 and ym<454 then speed=speed+1: if speed=4 then speed=1
rem solve
if xm>820 and xm<882 and ym>460 and ym<474
 
gosub CLEARMAZE
gosub CONVERTMAZE
gosub RESETMAPDATA
gosub DRAWMAZE
currently$="Mapping maze"
attempts=1
endif
rem pause
if xm>889 and xm<949 and ym>460 and ym<474 then currently$="Time for a cup of 'tea'."
rem clear path
if xm>956 and xm<1018 and ym>460 and ym<474
gosub CLEARMAZE
gosub CONVERTMAZE
gosub RESETMAPDATA
gosub DRAWMAZE
endif
 
rem self-destruct
if xm>820 and xm<1018 and ym>480 and ym<494
currently$="Self-destruct initiated!"
endif
 
gosub DRAWCONTROLPANEL
gosub REPORT
endif
if currently$="Mapping maze" or currently$="Self-destruct initiated!" then exit
 
loop
Return
 
DECLAIREVARIABLES:
xstart = 0
ystart = 0
xexit = 0
yexit = 0
direction = 0
lowvalue = 0
wall = 1000
deadendvalue=0
mazewidth=3
mazeheight=3
alterflag=0
eraserobot=0
maximumattempts=30
maximumrange=9000
movediagonal=8: rem 4=don't move diagonal - 8=move diagonal
rememberdeadends=1: rem 0=NO, 1=YES
completesearch=0: rem 0=NO, 1=YES
randommove=1:rem 0=NO, 1=YES
logic=5:rem how to move 1=random, 2=no u turns, 3=follow left, 4=follow right, 5=boldly go
speed=3:rem 1=paint drying, 2=slow motion, 3=turbo, 4=fricken fast
filename$=""
Return
 
RESETMAPDATA:
xposition = xstart
yposition = ystart
searchdistance#=0.0
operations=0
totaloperations=0
attempts=0
leastoperations=10000000
shortestdistance#=10000000.0
foundexit=0: Rem found exit flag 0=no, 1=yes, -1=not possible
currently$="Waiting instruction"
showflag=0
return
 
DRAWMAZE:
ink 0,0
box 0,20,811,767
Rem calculate scale
widthratio=740/mazewidth
heightratio=740/mazeheight
scale=widthratio: if heightratio<widthratio then scale=heightratio
if scale<=1
set text opaque
ink rgb(200,0,0),0
text 600,500,"MAZE IS TOO LARGE TO DISPLAY, BUT CAN BE SEARCHED"
endif
if scale>1
Rem Draw map on screen
for y=1 to mazeheight
for x=1 to mazewidth
Rem draw a wall section
if map(x,y)=wall then ink rgb(200,200,200),rgb(0,0,0):box (x-1)*scale,(y-1)*scale+20,x*scale,y*scale+20
rem if map(x,y)>wall then ink rgb(100,100,100),rgb(0,0,0):box (x-1)*scale,(y-1)*scale+20,x*scale,y*scale+20
next x
next y
endif
Rem show start
Gosub DRAWSTART
Rem show exit
Gosub DRAWEXIT
Return
 
DRAWFALSEWALL:
if scale>1
ink 0,0
box (xposition-1)*scale,(yposition-1)*scale+20,xposition*scale,yposition*scale+20
 
ink rgb(100,100,100),rgb(0,0,0)
rem box (xposition-1)*scale,(yposition-1)*scale+20,xposition*scale,yposition*scale+20
endif
Rem put deadend in map
map(xposition,yposition)=wall+1
eraserobot=1
Return
 
DRAWSTART:
if scale>1
ink rgb(0,200,0),rgb(0,0,0)
box (xstart-1)*scale,(ystart-1)*scale+20,xstart*scale,ystart*scale+20
endif
Return
 
DRAWEXIT:
if scale>1
ink rgb(200,0,0),rgb(0,0,0)
box (xexit-1)*scale,(yexit-1)*scale+20,xexit*scale,yexit*scale+20
endif
Return
 
DRAWROUTE:
if scale>1
ink rgb(200,200,0),rgb(0,0,0)
box (xposition-1)*scale+3,(yposition-1)*scale+23,(xposition*scale)-3,yposition*scale+17
rem box (xposition-1)*scale,(yposition-1)*scale+20,xposition*scale,yposition*scale+20
endif
return
 
GETMAZEFROMDATA:
Rem Put maze in to map array  1=wall 0=path 3=start 4=exit
rem one data line is one row of maze
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
data 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1
data 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1
data 1,0,0,1,0,1,1,1,1,1,0,1,1,1,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1
data 1,0,0,1,0,1,3,1,0,1,0,1,0,0,1,0,1,0,0,0,0,0,1,0,0,1,0,1,1,1,1,1
data 1,1,0,1,0,1,0,1,0,1,0,1,0,0,1,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1
data 1,0,0,1,0,1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,1,0,1,1,1,0,1
data 1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,1,1,1,1,1,1,1,0,0,1,0,1,0,1,0,1
data 1,0,0,1,0,1,0,0,0,1,0,1,0,0,1,0,1,0,0,0,1,0,0,0,0,1,0,1,0,1,0,1
data 1,1,0,1,0,1,1,1,1,1,0,1,1,1,1,0,1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1
data 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,1,0,1,1,1,1,1
data 1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1,0,0,1,0,1,0,0,0,0,0,1
data 1,0,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,1
data 1,1,1,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,0,1,0,0,1,0,1,0,0,0,1,0,1
data 1,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,0,0,1,0,1,0,1,0,1,0,1
data 1,0,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,0,1,0,0,1,0,1,0,1,0,1,0,1
data 1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,1,1,1,0,1,0,1,0,0,0,1
data 1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1,0,0,0,0,1,0,1,1,1,1,1
data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,1,0,0,0,0,0,1
data 1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,0,0,1,1,1,1,0,1,1
data 1,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
data 1,0,0,0,1,0,0,1,0,0,0,0,1,0,1,0,0,0,1,1,1,1,1,0,1,1,1,1,0,1,0,1
data 1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,1,0,0,1,0,0,0,1,0,1,0,0,1,0,0,0,1
data 1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,4,0,0,0,0,0,0,1,0,1,0,1
data 1,0,0,0,1,1,0,1,1,1,1,1,0,0,0,1,0,0,1,0,0,0,1,0,1,0,0,1,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,0,1,0,0,1,0,0,0,1
data 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,0,0,1,1,0,1,1,0,1,0,0,0,1
data 1,1,1,1,0,0,1,1,0,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,1,0,0,0,1
data 1,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1
data 1,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1
data 1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1
data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
 
maximumattempts=30
maximumrange=9000
completesearch=0
movediagonal=8: rem 4=don't move diagonal - 8=move diagonal
rememberdeadends=1: rem 0=NO, 1=YES
randommove=1
logic=5:rem how to move 1=random, 2=no u turns, 3=follow left, 4=follow right, 5=boldly go
speed=3:rem 1=paint drying, 2=slow motion, 3=turbo, 4=fricken fast
filename$="calcyman's"
mazewidth=32
mazeheight=32
restore
 
for y=1 to mazeheight
for x=1 to mazewidth
read m
if m=1 then map(x,y)=wall
if m=3 then map(x,y)=0: xstart=x : ystart=y
if m=4 then map(x,y)=0: xexit=x : yexit=y
next x
next y
Rem Put robot in maze at stat position
xposition=xstart : yposition=ystart
Return
 
CLEARMAZE:
for y=1 to mazeheight
for x=1 to mazewidth
if map(x,y)<wall or map(x,y)>wall then map(x,y)=0
if map(x,y)=wall then map(x,y)=1
next x
next y
map(xstart,ystart)=3
map(xexit,yexit)=4
return
 
CLEARPATH:
for y=1 to mazeheight
for x=1 to mazewidth
if map(x,y)<wall then map(x,y)=0
next x
next y
return
 
CONVERTMAZE:
for y=1 to mazeheight
for x=1 to mazewidth
if map(x,y)=1 then map(x,y)=wall
if map(x,y)=3 then map(x,y)=0: xstart=x : ystart=y
if map(x,y)=4 then map(x,y)=0: xexit=x : yexit=y
next x
next y
Return
 
ERASEMAZE:
for y=1 to 2000
for x=1 to 2000
map(x,y)=0
next x
next y
return