Code: Select all

```
10 REM Recursive Backtracker maze algorithm
11 REM via Jamis Buck
12 REM BBC BASIC version by lurkio @ Stardot, 2019
15 REM -------------------------------------------
20 MODE0
40 CLEAR:VDU23,0,10,8;0;0;0:INPUT'"Size of maze? "mSize%
45 IF mSize%<2ORmSize%>15 mSize%=1+RND(14):VDU7,11:PRINTSTRING$(14,CHR$9);mSize%;" "
50 DIM grid% mSize%^2
60 FOR I%=0TOmSize%^2STEP4:I%!grid%=0:NEXT
70 DIM stack%(mSize%^2)
90 VDU23,0,10,0;0;0;0
100 PROCprint(1):PRINTSTRING$(2*mSize%+1,CHR$11);:PRINTTAB(0);:H%=VPOS
105 REM -------------------
120 REM ## GENERATE MAZE ##
140 DIM buff%(3):REM cells surrounding current cell
150 sp%=0:REM stack pointer
170 cell%=RND(mSize%^2)-1:REM current cell
180 V%=1:REM number of cells visited
190 num%=0
210 REPEAT
230 stack%(sp%)=cell%:sp%=sp%+1
240 R%=cell% DIV mSize%:C%=cell%-R%*mSize%
260 IF num%>0 V%=V%+1
280 PRINTTAB(1+C%*4,H%+(mSize%-R%)*2-1);:IFV%>1PRINT" . ";ELSEPRINT" * ";
290 VDU8
300 IF cell%?grid% AND 1 VDU8,8,11,32,32,32,8,8,8,10,9,9
310 IF cell%?grid% AND 8 VDU8,8,8,32,9,9
320 IF cell%?grid% AND 4 VDU9,32,8,8
330 IF cell%?grid% AND 2 VDU8,8,10,32,32,32,8,8,8,11,9,9
350 num%=0
360 FORI%=0TO3:buff%(I%)=-1:NEXT
370 cell_n%=mSize%*(R%+1)+C%
380 IF R%<mSize%-1 AND cell_n%?grid%=0 buff%(0)=cell_n%:num%=num%+1
390 cell_s%=mSize%*(R%-1)+C%
400 IF R%>0 AND cell_s%?grid%=0 buff%(1)=cell_s%:num%=num%+1
410 cell_e%=mSize%*R%+C%+1
420 IF C%<mSize%-1 AND cell_e%?grid%=0 buff%(2)=cell_e%:num%=num%+1
430 cell_w%=mSize%*R%+C%-1
440 IF C%>0 AND cell_w%?grid%=0 buff%(3)=cell_w%:num%=num%+1
460 rand%=-1:IF num%=1 rand%=1 ELSE IF num%>1 rand%=RND(num%)
470 nextI%=-1
480 FORI%=0TO3:IF buff%(I%)>-1 rand%=rand%-1
490 IF rand%=0 next%=buff%(I%):nextI%=I%:rand%=-1
500 NEXT
510 IF nextI%=0 cell%?grid%=cell%?grid% EOR 1:next%?grid%=next%?grid% EOR 2
520 IF nextI%=1 cell%?grid%=cell%?grid% EOR 2:next%?grid%=next%?grid% EOR 1
530 IF nextI%=2 cell%?grid%=cell%?grid% EOR 4:next%?grid%=next%?grid% EOR 8
540 IF nextI%=3 cell%?grid%=cell%?grid% EOR 8:next%?grid%=next%?grid% EOR 4
550 IF num%>0 cell%=next%
560 IF num%=0 sp%=sp%-2:IF sp%>=0 cell%=stack%(sp%):VDU8,32
580 REMU. sp%<0:V.8,32
590 UNTILV%=mSize%^2
REM 610 FORI%=0TOmSize%^2:stack%(I%)=0:NEXT
630 REM -------------------
650 *FX15
660 SOUND1,-15,100,1
670 IFGET PRINTTAB(0,H%);:PROCprint(0)
680 *FX15
690 SOUND1,-15,100,1
700 IFGET PRINTTAB(0,H%+2*mSize%):GOTO 40
730 REM ## End ##
735 REM -------------------
750 DEFPROCprint(w%):REM blocks? 0/1
760 VDU23,255,255,255,255,255,255,255,255,255
770 DIM T% 2+4*mSize%
780 DIM B% 2+4*mSize%
790 PRINT "+";STRING$(mSize%,"---+");
800 FOR R%=mSize%-1 TO 0 STEP-1:REM top down
810 $T%="|":$B%="+"
820 FOR C%=0 TO mSize%-1
830 IF w%=0 W$=" " ELSE W$=CHR$255+CHR$255+CHR$255
840 cell%=grid%+C%+R%*mSize%
850 IF ?cell% AND 4 THEN E$=" " ELSE E$="|"
860 $T%=$T%+W$+E$
870 IF ?cell% AND 2 THEN S$=" " ELSE S$="---"
880 $B%=$B%+S$+"+"
890 NEXT:REM column
900 PRINT'$T%'$B%;
910 NEXT:REM row
920 ENDPROC
```

The program also works in BBC BASIC For Windows but fails in BBC BASIC For SDL 2.0 (MacOS version 1.02a) with a not-enough-memory error at line 50.