Quick actions

cmd+k|ctrl+k

Navigation

Languages

Maze Maker

Snippet info

Language

Lua

Visibility

public

Author

mrhax0909

Created

2025-04-19T21:32:02.780062Z

Updated

2025-04-19T21:32:02.780062Z

local Maze = {}

for i = 1, 20 do
    Maze[i] = {}
    for j = 1, 10 do
        Maze[i][j] = {
            Walls = {true, true, true, true}, -- up, down, left, right
            Visited = false
        }
    end
end

local Dirs = {
    {-1, 0, 3, 4},
    {0, -1, 1, 2},
    {1, 0, 4, 3},
    {0, 1, 2, 1}
}

local function Visit(X, Y, MoveID)
    local Dx, Dy, WallExit, WallEnter = unpack(Dirs[MoveID])
    local Current = Maze[X + Dx] and Maze[X + Dx][Y + Dy]
    if not Current or Current.Visited then
        return
    end
    Current.Visited = true
    Current.Walls[WallEnter] = false
    Maze[X][Y].Walls[WallExit] = false
    
    local Options = {1, 2, 3, 4}
    
    for i = 1, 4 do
        local Id = table.remove(Options, math.random(#Options))
        if Visit(X + Dx, Y + Dy, Id) then
            return
        end
    end
end

Visit(math.random(#Maze - 2) + 1, math.random(#Maze[1] - 2) + 1, math.random(4))

local Display = {
         -- 0 0 0 0
    "╹", -- 1 0 0 0
    "╻", -- 0 1 0 0
    "┃", -- 1 1 0 0
    "╸", -- 0 0 1 0
    "┛", -- 1 0 1 0
    "┓", -- 0 1 1 0
    "┫", -- 1 1 1 0
    "╺", -- 0 0 0 1
    "┗", -- 1 0 0 1
    "┏", -- 0 1 0 1
    "┣", -- 1 1 0 1
    "━", -- 0 0 1 1
    "┻", -- 1 0 1 1
    "┯", -- 0 1 1 1
    "╋"  -- 1 1 1 1
}

local Output = ""
for y = 1, #Maze[1] do
    for x = 1, #Maze do
        local Id = 0
        
        for i, v in ipairs(Maze[x][y].Walls) do
            if not v then
                Id = Id + 2 ^ (i - 1)
            end
        end
        
        Output = Output..Display[Id]
    end
    Output = Output.."\n"
end

print(Output)
INFO