Ideas on how I would draw the maze?

Currently I’m trying to make a random maze generator; I got the maze generation working, now I’m confuse of how I would be able to draw the maze:

local function Find(table : {}, other : any)
	for _, value in next, table do
		if ((value[1] == other[1]) and (value[2] == other[2])) then
			return true
		end
	end
end

return function(self : {}, start : {}, speed : number)
	local currentSquare = start or {1, 1}

	local mazeSquaresData = self:PrecomputedSquareData()
	local maxIndex = (self:GetSizeX() * self:GetSizeY())
	local visited = {currentSquare}
	local stack = {}

	local start = tick()

	while (#visited < maxIndex) do
		local selected = mazeSquaresData[currentSquare[1]][currentSquare[2]]
		local targetSquare = selected[math.random(1, #selected)]

		if (Find(visited, targetSquare)) then
			local available = false

			for _, value in next, selected do
				if (not (Find(visited, value))) then
					targetSquare = value
					available = true break
				end
			end

			if (not available) then
				currentSquare = table.remove(stack, #stack)[2] continue
			end
		end

		table.insert(stack, {currentSquare, targetSquare})
		table.insert(visited, targetSquare)

		task.wait(speed)
	end

	local completedIn = (tick() - start)

	print(("Time: %ims or %is"):format(math.floor(completedIn * 1000), math.floor(completedIn)))
end

draw the maze? As in UI or build it in workspace?

I could easily draw it on a UI since the table is a Vector2, I want it to be drawn on the workspace.

I could also just pre generate the cells on the workspace then remove the cell then just add walls based on the direction.

I feel like you should try random stuff with this one. Try applying vector2 values into vector3 etc etc.
You may find answer with almost no logical thinking.

1 Like

Im planning on changing the script from 2d array to 1d so its much faster, while doing so I’m thinking of a way to draw the maze.

1 Like

Good luck! This code is complex but you will absolutely be able to do it if you put your mind into it

1 Like

After I woke up I got an idea of just generating 2 parts which is the north wall and the east wall, there’s is a slight problem I’m having trouble calculating the position so it’s centered:

local Part = Instance.new("Part")

Part.Anchored = true
Part.Material = Enum.Material.SmoothPlastic

return function(self : {})
	local wallSize = Vector3.new(6, 6, 1)
	local sizeX, sizeY = self:GetSize()

	local MazeFolder = Instance.new("Folder", workspace)

	MazeFolder.Name = "Maze"

	for row = 1, sizeY do
		for column = 1, sizeX do
			local northWall = Part:Clone()

			northWall.Parent = MazeFolder
			northWall.Size = wallSize
			northWall.Rotation = Vector3.new(0, 90, 0)
			northWall.Position = Vector3.new(0, 0, (wallSize.Y / 2) - 0.5) - Vector3.new(row * 6, 0, column * 6)

			local eastWall = Part:Clone()

			eastWall.Parent = MazeFolder
			eastWall.Size = wallSize
			eastWall.Position = Vector3.new((wallSize.X / 2) - 0.5, 0, 0) - Vector3.new(row * 6, 0, column * 6)
		end
	end
end

Any idea on how I would generate the cells from the center point?

I got the maze working it was pretty simple, but still have not found the solution to set it the position so its centered:


Not much coding was actually done, still I plan on making it on a different algorithm, currently this is the Recursive implementation also know as the recursive backtracker; I think I’ll go with the hunt and kill algorithm next.

1 Like