Jump to content

User:Eriksiers/Maze generation

From Rosetta Code

These two short programs generate mazes using a random walk. I didn't put them up as solutions to Maze generation because that task requires the use of the depth-first algorithm, which is much faster than what I've done here.

Text

Works with: QBasic

This uses # for walls and . for floors.

OPTION BASE 1

CONST scrW = 80
CONST scrH = 50

WIDTH scrW, scrH

RANDOMIZE TIMER

TYPE cell
    x AS INTEGER
    y AS INTEGER
END TYPE

DIM working AS cell, old AS cell

working.x = (INT(RND * scrW) AND -2)
working.y = (INT(RND * scrH) AND -2)

'Fill with walls:
FOR y = 1 TO scrH
    PRINT STRING$(79, "#");
NEXT

'Random walk:
DO
    chk = chk + 1
    'This check really slows the program down, so we only do it every 100 iterations.
    IF (chk MOD 100) = 0 THEN
        chk = 0
        FOR x = 2 TO scrW - 2 STEP 2
            FOR y = 2 TO scrH - 2 STEP 2
                IF SCREEN(y, x) = 35 THEN GOTO cntT
            NEXT
        NEXT
        EXIT DO
    END IF
cntT:
    old = working
    'The IFs inside the SELECT CASE ensure that drawing doesn't happen off the screen.
    SELECT CASE INT(RND * 4)
        CASE 0
            working.x = working.x + 2
            IF working.x >= scrW THEN working.x = working.x - 4
        CASE 1
            working.y = working.y + 2
            IF working.y >= scrH THEN working.y = working.y - 4
        CASE 2
            working.x = working.x - 2
            IF working.x < 1 THEN working.x = working.x + 4
        CASE 3
            working.y = working.y - 2
            IF working.y < 1 THEN working.y = working.y + 4
    END SELECT
    IF SCREEN(working.y, working.x) = 35 THEN
        LOCATE working.y, working.x
        PRINT ".";
        LOCATE (old.y + working.y) \ 2, (old.x + working.x) \ 2
        PRINT ".";
    END IF
    IF INKEY$ <> "" THEN EXIT DO
LOOP

The generated mazes look like this:

###############################################################################
#.#...#.#.#.#.#.#.#.....#.#...#.....#.......#...#.#.#.#.#.....#...#.#.#.......#
#.###.#.#.#.#.#.#.#.#####.#.#.###.#####.#######.#.#.#.#.#####.###.#.#.#.#######
#...#.....#.#.#...........#.#.#.................#...#...#...........#.......#.#
#.###.###.#.#.#.###.#########.###.#.#.#########.#.###.#####.###.###.#.###.#.#.#
#.#.#.#...#.....#.#.#.....#.....#.#.#.....#...#...#...#...#.#.#.#.#...#...#.#.#
#.#.#.###.###.#.#.#.###.###.#.###.#.#####.#.###.###.#.###.###.#.#.#####.#####.#
#.#...#.......#...#.....#.#.#.#.#.#.....#.....#.....#...........#.....#.#.....#
#.#.###.#######.#.#####.#.#.#.#.#######.###.###.#############.###.#########.###
#.#...#...#.....#.#.........#.#.#.#.......#.#...#.......#...#.#...#.#.....#.#.#
#.#.###.#########.#####.###.###.#.#.#.#####.###.#.#.#######.#.###.#.#####.#.#.#
#.....#...#...#.#.#...#.#.#.#.....#.#.....#...#.#.#.......#.#.....#.#...#...#.#
#####.#######.#.###.#.#.#.#####.###.#.#####.#.###.#####.###.#####.#.###.#.###.#
#.....#.......#.....#...#.......#.#.#.....#.#.#.....#.......#.#.#...#.........#
#.###.###.#.###.#.###.#####.###.#.#.#####.###.###.#.#####.###.#.#.###.###.###.#
#.#...#...#.#...#...#.....#.#.....#.....#...#...#.#...#.............#...#...#.#
###.###.###########.#####.#####.###.#######.#######.#.#.#######.#.###.###.#####
#.#...#...#.#.........#.#.#...#...#.#.#.#.....#.#...#.#...#.....#...#.#.#.....#
#.#.#.#.#.#.###.#.###.#.#.#.###.#####.#.#.#.###.###.###.###.#.###.#####.#.#.###
#...#.#.#.....#.#.#.....#.#.#...#.#...#.#.#.#.....#.#.....#.#.#.#.#.#.....#...#
#.###########.###.#####.###.###.#.###.#.###.###.#####.#.#####.#.###.#.###.###.#
#.....#.#.........#.........#.#.#.#...#...#.#.......#.#...#...........#.#...#.#
#.#####.#.###.###.#.#####.#.#.#.#.#.#.#.#.###.#####.#.#######.#.#.#.#.#.#.#####
#.#.....#.#.....#.#...#.#.#.#.#.....#...#.......#...#...#.#.#.#.#.#.#.#...#...#
###.###.###.###.###.###.#.#.#.#.#######.###.#.###.###.###.#.#.###.###.#######.#
#...#.#.....#...#.....#.#.#...#.#...#.#.#...#.#...#.......#...#.....#.#...#...#
###.#.#####.###.#####.#.###.#.#.###.#.###.###.###.#######.#########.#.#.#.#.#.#
#...#.#.....#...#.#.....#...#.#.#...#.#.#.#...#...#.#.......#...#...#.#.#...#.#
#####.###.#######.#####.###.#.#####.#.#.#######.#.#.#########.###.#.###.#.#####
#.......#.......#.....#.#...#.#.......#...#.....#.....#.....#.#...#...#.#...#.#
###.#####.#####.#.#.#.###.###.###.#####.#########.###.#.#.###.#.###.#.###.###.#
#.#.........#...#.#.#.#.#...#.....#.#.......#.#.....#...#.#.....#...#...#...#.#
#.###.###.#.#.###.#####.###.#####.#.#######.#.#.#.#.###.###.#####.###.###.#.#.#
#.....#...#.#...#...#.#...#.#.#.#.......#.#.....#.#.#...#...#.......#.#.#.#.#.#
#.#######.#####.###.#.#.###.#.#.#######.#.###.#########.###.###########.#.###.#
#.......#.#.......#.........#.#...#.............#.#.#.......#.......#.#.....#.#
#####.#####.###.#.###.###.#.#.#.#########.#.#.###.#.###.#####.#.###.#.###.###.#
#.........#...#.#.#.#.#...#.....#.....#...#.#...#.#.#.#.#.#.#.#.#.#.....#...#.#
#.#.###.#########.#.#################.#########.#.#.#.###.#.#.###.###.#.#.###.#
#.#...#.....#.#.#.#...#.....#...#.....#...#.......#.........#...#...#.#...#...#
#######.#.###.#.#.#.###.#####.#.#####.#.###.#.#######.#######.###.###.#####.###
#.#.....#.#.#.....#.#.#.......#.#.....#.#.#.#...#.....#...........#...#...#...#
#.###.#####.#.#.#.#.#.#.###.###.#.#####.#.#####.#.###.#.###.#.#####.#.#.###.###
#.#.....#...#.#.#.#.#...#.#.#...#...#.#...#.#.....#...#...#.#.#...#.#.....#...#
#.#.#######.###.#.#.#.#.#.#.###.###.#.#.###.###.#####.#.###.###.#.#.###.#.#.###
#.....#.......#.#...#.#.#.#.#.#...........#.......#...#...#.#...#...#...#.....#
#####.#.###.#.#.#.###.###.###.#####.#####.###.#.###.#############.###.#.#.###.#
#.........#.#.#.#.#...............#.....#.....#...#...............#...#.#...#.#
###############################################################################

Graphical

Works with: FreeBASIC
A sample maze.

This is a graphical variation of the above. It uses white for floors and black for walls. FreeBASIC is required due to how I write the results to the image file. (This works under QBasic aside from the very last line. Changing that would let it work under any QB-alike.)

OPTION BASE 1

CONST scrW = 400
CONST scrH = 300

ScreenRes scrW, scrH

RANDOMIZE TIMER

TYPE cell
    x AS INTEGER
    y AS INTEGER
END TYPE

DIM working AS cell, old AS cell

working.x = (INT(RND * scrW) AND -2)
working.y = (INT(RND * scrH) AND -2)

'random walk
DO
    chk = chk + 1
    IF (chk MOD 1000) = 0 THEN
        chk = 0
        FOR x = 0 TO (scrW - 1) STEP 2
            FOR y = 0 TO (scrH - 1) STEP 2
                IF POINT(x, y) = 0 THEN GOTO cntT
            NEXT
        NEXT
        EXIT DO
    END IF
cntT:
    old = working
    SELECT CASE INT(RND * 4)
        CASE 0
            working.x = working.x + 2
            IF working.x >= scrW THEN working.x = working.x - 4
        CASE 1
            working.y = working.y + 2
            IF working.y >= scrH THEN working.y = working.y - 4
        CASE 2
            working.x = working.x - 2
            IF working.x < 0 THEN working.x = working.x + 4
        CASE 3
            working.y = working.y - 2
            IF working.y < 0 THEN working.y = working.y + 4
    END SELECT
    IF (POINT(working.x, working.y)) = 0 THEN LINE (old.x, old.y)-(working.x, working.y), 15
    IF INKEY$ <> "" THEN EXIT DO
LOOP

BSAVE "maze.bmp", 0
Cookies help us deliver our services. By using our services, you agree to our use of cookies.