Code: Select all
' Change the maze properties here.
CONST cellsize = 16
CONST numcellsh = 48
CONST numcellsv = 32
RANDOMIZE TIMER
SCREENRES 800,600,8
PAINT (0,0),15
FOR cy = 0 TO numcellsv * cellsize STEP cellsize
FOR cx = 0 TO numcellsh * cellsize STEP cellsize
LINE (cx,0)-(cx,numcellsv * cellsize),16
LINE (0,cy)-(numcellsh * cellsize,cy),16
NEXT
NEXT
TYPE cellwalls
AS UBYTE n,s,e,w
END TYPE
TYPE cellinfo
AS INTEGER xloc,yloc,visit
AS cellwalls wall
END TYPE
TYPE cellcoor
AS INTEGER x,y
END TYPE
DIM SHARED AS cellinfo mazecells(numcellsh,numcellsv)
DIM SHARED AS cellcoor route(cellsize ^ 4)
DIM SHARED AS INTEGER xx,yy,routecount,direct
DIM SHARED AS INTEGER nm,ee,ss,ww,ender
SUB initmaze()
FOR cy = 0 TO numcellsv - 1
FOR cx = 0 TO numcellsh - 1
WITH mazecells(cx,cy)
.xloc = cx
.yloc = cy
.visit = 0
WITH .wall
.n = 1
.s = 1
.e = 1
.w = 1
END WITH
END WITH
NEXT
NEXT
CLOSE
xx = RND * numcellsh
yy = RND * numcellsv
mazecells(xx,yy).visit = 1
WITH route(routecount)
.x = xx
.y = yy
END WITH
END SUB
SUB savemaze()
? curdir & "\tehmaze.maze"
OPEN curdir & "\tehmaze.maze" FOR OUTPUT AS #1
FOR cy = 0 TO numcellsv - 1
FOR cx = 0 TO numcellsh - 1
WITH mazecells(cx,cy)
print #1, "mazecells(" & cx & "," & cy & ")"
print #1, "X: " & .xloc
print #1, "Y: " & .yloc
print #1, "Visited?: " & .visit
WITH .wall
? #1, " North wall: " & .n
? #1, " South wall: " & .s
? #1, " East wall: " & .e
? #1, " West wall: " & .w
END WITH
END WITH
print #1, ""
NEXT
NEXT
CLOSE
END SUB
SUB move(cellx,celly)
DIM AS INTEGER x1,x2,y1,y2
DIM AS INTEGER nn,ee,ss,ww,direction
direction = INT(RND * 4)
IF mazecells(cellx,celly - 1).visit = 0 AND celly > 0 THEN
nn = 1
END IF
IF mazecells(cellx + 1,celly).visit = 0 AND cellx < (numcellsh - 1) THEN
ee = 1
END IF
IF mazecells(cellx,celly + 1).visit = 0 AND celly < (numcellsv - 1) THEN
ss = 1
END IF
IF mazecells(cellx - 1,celly).visit = 0 AND cellx > 0 THEN
ww = 1
END IF
' North
IF direction = 0 AND nn = 1 THEN
mazecells(cellx,celly).wall.n = 0
mazecells(cellx,celly - 1).visit = 1
mazecells(cellx,celly - 1).wall.s = 0
x1 = cellx * cellsize + 1
y1 = celly * cellsize
x2 = x1 + cellsize - 2
y2 = y1
LINE (x1,y1)-(x2,y2),15
yy -= 1
' East
ELSEIF direction = 1 AND ee = 1 THEN
mazecells(cellx,celly).wall.e = 0
mazecells(cellx + 1,celly).visit = 1
mazecells(cellx + 1,celly).wall.w = 0
x1 = cellx * cellsize + cellsize
y1 = celly * cellsize + 1
x2 = x1
y2 = y1 + cellsize - 2
LINE (x1,y1)-(x2,y2),15
xx += 1
' South
ELSEIF direction = 2 AND ss = 1 THEN
mazecells(cellx,celly).wall.s = 0
mazecells(cellx,celly + 1).visit = 1
mazecells(cellx,celly + 1).wall.n = 0
x1 = cellx * cellsize + 1
y1 = celly * cellsize + cellsize
x2 = x1 + cellsize - 2
y2 = y1
LINE (x1,y1)-(x2,y2),15
yy += 1
' West
ELSEIF direction = 3 AND ww = 1 THEN
mazecells(cellx,celly).wall.w = 0
mazecells(cellx - 1,celly).visit = 1
mazecells(cellx - 1,celly).wall.e = 0
x1 = cellx * cellsize
y1 = celly * cellsize + 1
x2 = x1
y2 = y1 + cellsize - 2
LINE (x1,y1)-(x2,y2),15
xx -= 1
' No direction available so backtrack
ELSEIF nn = 0 AND ee = 0 AND ss = 0 AND ww = 0 THEN
IF routecount > 0 THEN
routecount -= 1
WITH route(routecount)
xx = .x
yy = .y
END WITH
END IF
' change direction
ELSE
direction += 1
IF direction = 4 THEN direction = 0
END IF
' add coordinates to the route
IF nn = 1 OR ee = 1 OR ss = 1 OR ww = 1 THEN
routecount += 1
WITH route(routecount)
.x = xx
.y = yy
END WITH
ender += 1
END IF
END SUB
' +------------------------------------------------------------------+
' |******************************************************************|
' |**************************** +------+ ****************************|
' |**************************** | Main | ****************************|
' |**************************** +------+ ****************************|
' |******************************************************************|
' +------------------------------------------------------------------+
initmaze
FOR i = 0 TO cellsize ^ 4
LINE (xx * cellsize + 3,yy * cellsize + 3)-(xx * cellsize + 7,yy * cellsize + 7),15,bf
move(xx,yy)
LINE (xx * cellsize + 3,yy * cellsize + 3)-(xx * cellsize + 7,yy * cellsize + 7),4,bf
IF MULTIKEY(1) THEN EXIT FOR
NEXT
savemaze
sleep