### Author Topic: A simple Maze Generator :)  (Read 2047 times)

#### onugl

• Guest
##### A simple Maze Generator :)
« on: 2008-Aug-23 »
Ported from a Free Basic code:

`//Maze Maker//ORIGINAL VERSION: Richard D. Clark//Public domain//FreeBasic .17////Ported to GLBASIC by: Zikitrake//*********************************GLOBAL fbWhite = RGB (255, 255, 255)GLOBAL fbBlack = RGB (000, 000, 000)GLOBAL fbYellow = RGB (255, 255, 000)GLOBAL fbOrange = RGB (255, 128, 000)GLOBAL mazY= 25GLOBAL mazX = 25GLOBAL WallWidth = 16GLOBAL north = 1GLOBAL east =  2GLOBAL south = 3GLOBAL west  = 4                       //[6] -> 1=N, 2=S, 3=E, 4=W, 5=VisitedDIM  maze[mazY+1][mazX+1][6]MAIN: SETSCREEN 640, 480, 0 GOSUB SetUpMaze SHOWSCREEN MOUSEWAIT END//get a random number between low AND highFUNCTION Rand:lowerbound, upperbound   RETURN INTEGER( RND(upperbound - lowerbound + 1) + lowerbound)ENDFUNCTIONSUB DrawFullMaze:    LOCAL i, j, x, y    FOR i = 1 TO mazY        FOR j = 1 TO mazX            maze[i][j][5] = FALSE            maze[i][j][1] = FALSE            maze[i][j][2] = FALSE            maze[i][j][3] = FALSE            maze[i][j][4] = FALSE            y = (i - 1) * WallWidth            x = (j - 1) * WallWidth            DRAWRECT x, y,  WallWidth, WallWidth, fbWhite            DRAWRECT x+1, y+1,  WallWidth-1, WallWidth-1, fbBlack        NEXT    NEXT    DRAWLINE mazX*WallWidth, 0, mazX*WallWidth, mazY*WallWidth, fbWhite    DRAWLINE 0, mazY*WallWidth, mazX*WallWidth, mazY*WallWidth, fbWhiteENDSUBFUNCTION CheckLoc:y, x, cdir    LOCAL nx, ny, ret = FALSE    IF cdir = north        nx = x        ny = y - 1        IF ny > 0            IF maze[ny][nx][5] = FALSE                ret = TRUE            ENDIF        ENDIF    ELSEIF cdir = east        nx = x + 1        ny = y        IF nx <= mazX            IF maze[ny][nx][5] = FALSE                ret = TRUE            ENDIF        ENDIF    ELSEIF cdir = south        nx = x        ny = y + 1        IF ny <= mazY            IF maze[ny][nx][5] = FALSE                ret = TRUE            ENDIF        ENDIF    ELSEIF cdir = west        nx = x - 1        ny = y        IF nx > 0            IF maze[ny][nx][5] = FALSE                ret = TRUE            ENDIF        ENDIF    ENDIF    RETURN retENDFUNCTIONFUNCTION GetNewDir:y, x    LOCAL cdir, nx, ny    cdir = Rand(north, west)    IF CheckLoc(y, x, cdir)        RETURN cdir    ENDIF    INC cdir, 1    IF cdir > west THEN cdir = north    IF CheckLoc(y, x, cdir)        RETURN cdir    ENDIF    INC cdir, 1    IF cdir > west THEN cdir = north    IF CheckLoc(y, x, cdir)        RETURN cdir    ENDIF    INC cdir, 1    IF cdir > west THEN cdir = north    IF CheckLoc(y, x, cdir)        RETURN cdir    ENDIF    RETURN 0ENDFUNCTIONFUNCTION DrawMaze: y, x    LOCAL dx, dy, nx, ny, cdir    dy = (y - 1) * WallWidth    dx = (x - 1) * WallWidth    maze[y][x][5] = TRUE    cdir = GetNewDir(y, x)    IF cdir = 0 THEN RETURN    IF cdir = north        //Try north direction        nx = x        ny = y - 1        maze[y][x][1] = TRUE        maze[ny][nx][2] = TRUE        DRAWLINE dx+1, dy, dx + WallWidth-1, dy, fbBlack        maze[ny][nx][5] = TRUE        DrawMaze(ny, nx)    ENDIF    IF cdir = east        //Try east direction        nx = x + 1        ny = y        maze[y][x][3] = TRUE        maze[ny][nx][4] = TRUE        DRAWLINE dx+WallWidth, dy+1, dx + WallWidth, dy + WallWidth-1, fbBlack        maze[ny][nx][5] = TRUE        DrawMaze(ny, nx)    ENDIF    IF cdir = south        //Try south direction        nx = x        ny = y + 1        maze[y][x][2] = TRUE        maze[ny][nx][1] = TRUE        DRAWLINE dx+1, dy+WallWidth, dx + WallWidth-1, dy + WallWidth, fbBlack        maze[ny][nx][5] = TRUE        DrawMaze(ny, nx)    ENDIF    IF cdir = west        //Try west direction        nx = x - 1        ny = y        maze[y][x][4] = TRUE        maze[ny][nx][3] = TRUE        DRAWLINE dx, dy+1, dx , dy + WallWidth-1, fbBlack        maze[ny][nx][5] = TRUE        DrawMaze(ny, nx)    ENDIF    DrawMaze(y, x)ENDFUNCTIONFUNCTION DrawEndPoint: y, x, clr    LOCAL dx, dy    dy = (y - 1) * WallWidth    dx = (x - 1) * WallWidth    DRAWRECT dx+2, dy+2, WallWidth-4, WallWidth-4, clrENDFUNCTIONSUB SetUpMaze:    LOCAL mx, my    GOSUB DrawFullMaze    my = mazY    mx = mazX    maze[my][mx][5] = TRUE    DrawMaze(my, mx)    DrawEndPoint(1, 1, fbYellow)    DrawEndPoint(my, mx, fbYellow)ENDSUB`