User:Eriksiers/Maze generation: Difference between revisions

From Rosetta Code
Content added Content deleted
(created)
 
m (→‎Graphical: dangit)
 
(One intermediate revision by the same user not shown)
Line 6: Line 6:
This uses <tt>#</tt> for walls and <Tt>.</Tt> for floors.
This uses <tt>#</tt> for walls and <Tt>.</Tt> for floors.


<lang qbasic>OPTION BASE 1
<syntaxhighlight lang="qbasic">OPTION BASE 1


CONST scrW = 80
CONST scrW = 80
Line 67: Line 67:
END IF
END IF
IF INKEY$ <> "" THEN EXIT DO
IF INKEY$ <> "" THEN EXIT DO
LOOP</lang>
LOOP</syntaxhighlight>


The generated mazes look like this:
The generated mazes look like this:
Line 127: Line 127:
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.)
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.)


<lang qbasic>OPTION BASE 1
<syntaxhighlight lang="qbasic">OPTION BASE 1


CONST scrW = 400
CONST scrW = 400
Line 178: Line 178:
LOOP
LOOP


BSAVE "maze.bmp", 0</lang>
BSAVE "maze.bmp", 0</syntaxhighlight>

Latest revision as of 04:41, 1 September 2022

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