Programming:Maze generation

From CPCWiki - THE Amstrad CPC encyclopedia!
Jump to: navigation, search

From Rosetta Code, I was able to translate a Maze generation routine from a QB64 for Locomotive BASIC.

The original code produced a 40x20 maze and recommended using even numbers for the Width and Height.

I have set it up for MODE 1, 38 x 20 and it actually takes a while to prepare, though it should be useful for creating maze style games as it requires very little information to adjust for MODE 0.

10 MODE 1:DEFINT a-z:INK 0,0:INK 1,9:BORDER 0
20 LOCATE 14,12:PEN 1:PRINT"Please Wait."
30 RANDOMIZE TIME:RANDOMIZE RND
40 w=38
50 h=20
60 DIM m$(w,h)
70 FOR x=0 TO w
80 FOR y=0 TO h
90 m$(x,y)="#"
100 NEXT y
110 NEXT x
120 cx=INT(RND*(w-1))
130 cy=INT(RND*(h-1))
140 IF cx MOD 2=0 THEN cx=cx+1
150 IF cy MOD 2=0 THEN cy=cy+1
160 m$(cx,cy) = " "
170 d=0
180 WHILE d=0
190 FOR i=0 TO 99
200 ox=cx
210 oy=cy
220 s=INT(RND*4)+1
230 ON s GOSUB 410,430,450,470
240 IF m$(cx,cy)="#" THEN m$(cx,cy)=" ":m$(INT((cx+ox)/2),INT((cy+oy)/2))=" "
250 NEXT i
260 d=1
270 FOR x=1 TO w-1 STEP 2
280 FOR y=1 TO h-1 STEP 2
290 IF m$(x,y)="#" THEN d=0
300 NEXT y
310 NEXT x
320 WEND
330 LOCATE 1,1
340 FOR y=0 TO h
350 FOR x=0 TO w
360 PRINT m$(x,y);
370 NEXT x
380 PRINT
390 NEXT y
400 END
410 IF cx+2<w THEN cx=cx+2
420 RETURN
430 IF cy+2<h THEN cy=cy+2
440 RETURN
450 IF cx-2>0 THEN cx=cx-2
460 RETURN
470 IF cy-2>0 THEN cy=cy-2
480 RETURN