Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Maze generation
#1
Here's some code to generate a maze (https://en.wikipedia.org/wiki/Maze_generation_algorithm):

Code:
set window "maze", 640, 480
set redraw off

randomize time()
maze = GenerateMaze(24, 24)

side = 16

set color 0, 0, 0
cls
for x = 0 to sizeof(maze) - 1
    for y = 0 to sizeof(maze[0]) - 1
        dx = x*side
        dy = y*side
        set color 255, 255, 255
        if not maze[x][y].l  draw line dx, dy, dx, dy + side - 1
        if not maze[x][y].r  draw line dx + side - 1, dy, dx + side - 1, dy + side - 1
        if not maze[x][y].u  draw line dx, dy, dx + side - 1, dy
        if not maze[x][y].d  draw line dx, dy + side - 1, dx + side - 1, dy + side - 1
    next
next

redraw
while not keydown(KEY_ESCAPE, true)  fwait 60

' GenerateMaze
' ------------
' Return an array of the size w*h, where every element has four fields, l, r, u and d, telling if
' there's a way left, right, up and down.
function GenerateMaze(w, h)
    maze = fill([vis: false, l: false, r: false, u: false, d: false], w, h)
    GenerateMazeRec(maze, rnd(sizeof(maze)), rnd(sizeof(maze[0])), 0)
    return maze
 
    function GenerateMazeRec(maze, x, y, dir)
        if x < 0 or x >= sizeof(maze) or y < 0 or y >= sizeof(maze[0])  return false
        if maze[x][y].vis  return false
        maze[x][y].vis = true
        if dir = 1
            maze[x][y].r = true
            maze[x + 1][y].l = true
        elseif dir = 2
            maze[x][y].l = true
            maze[x - 1][y].r = true
        elseif dir = 3
            maze[x][y].d = true
            maze[x][y + 1].u = true
        elseif dir = 4
            maze[x][y].u = true
            maze[x][y - 1].d = true
        endif
        visit = [1, 2, 3, 4]
        while sizeof(visit)
            index = rnd(sizeof(visit))
            if visit[index] = 1  GenerateMazeRec(maze, x - 1, y, 1)
            elseif visit[index] = 2  GenerateMazeRec(maze, x + 1, y, 2)
            elseif visit[index] = 3  GenerateMazeRec(maze, x, y - 1, 3)
            else  GenerateMazeRec(maze, x, y + 1, 4)
            free key visit, index
        wend
        return true
    endfunc
endfunc

I'll post some more code later, explaining how this can be used for creating a map for the wolf3d library.
Reply


Messages In This Thread
Maze generation - by Marcus - 10-17-2024, 05:20 AM
RE: Maze generation - by johnno56 - 10-17-2024, 06:51 AM
RE: Maze generation - by Marcus - 10-17-2024, 03:54 PM
RE: Maze generation - by Marcus - 10-17-2024, 03:33 PM
RE: Maze generation - by johnno56 - 10-17-2024, 07:50 PM
RE: Maze generation - by Marcus - 10-18-2024, 04:54 PM
RE: Maze generation - by Marcus - 10-18-2024, 04:11 PM
RE: Maze generation - by 1micha.elok - 10-20-2024, 11:31 AM
RE: Maze generation - by kevin - 10-20-2024, 11:26 AM
RE: Maze generation - by johnno56 - 10-21-2024, 11:37 AM
RE: Maze generation - by kevin - 10-21-2024, 11:48 AM
RE: Maze generation - by johnno56 - 10-21-2024, 06:36 PM
RE: Maze generation - by Marcus - 10-21-2024, 06:33 PM
RE: Maze generation - by kevin - 10-21-2024, 07:00 PM
RE: Maze generation - by johnno56 - 10-21-2024, 07:37 PM
RE: Maze generation - by Marcus - 10-23-2024, 04:57 AM
RE: Maze generation - by kevin - 10-23-2024, 07:29 AM
RE: Maze generation - by johnno56 - 10-23-2024, 09:05 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)