How would I prevent maze generator from making "rooms"

I’m trying to make a randomly generating maze, but I’m not sure how I would go about preventing tiles from spawning right next to each other.

Currently, the generator makes areas like these:
image

I’m trying to make the generator create something more like this:

I don’t want tiles spawning directly next to each other and forming “room” like areas if that makes sense.

This is the generator I made so far:

local iterations = 5 --How many times the maze will generate new tiles
local tileBase = game:GetService("ReplicatedStorage"):WaitForChild("Tile") --Tile template used for maze generation
local tileTable = {} --Store generated tiles
local prevTable = {} --Stores last iteration of tiles

for i = 0,iterations,1 do
	print(i)
	prevTable = {}
	if i == 0 then --Initalize generation
		local newTile = tileBase:Clone()
		table.insert(tileTable,newTile)
		newTile.Parent = workspace
		newTile:PivotTo(workspace.StartPosition.CFrame)
		workspace.StartPosition:Destroy()
	else
		for i,v in pairs(tileTable) do
			for index, currentConnector in pairs(v:FindFirstChild("Connectors"):GetChildren()) do
				if currentConnector ~= nil then
					local newTile = tileBase:Clone()
					local lastConnectorTable = v:WaitForChild("Connectors"):GetChildren()
					local currentConnectorTable = newTile:WaitForChild("Connectors"):GetChildren()
					local directionChosen = math.random(1,#lastConnectorTable+1)
					
					--Generate new tile
					if directionChosen ~= #lastConnectorTable+1 then
						newTile.Parent = workspace
						newTile.PrimaryPart = currentConnectorTable[directionChosen]
						newTile:PivotTo(lastConnectorTable[directionChosen].CFrame)

						for a = 1,4,1 do
							for n,c in pairs(newTile.Floor:GetTouchingParts()) do
								if c.Name == "Floor" and c ~= newTile.Floor then
									--wait(1)
									newTile.PrimaryPart.CFrame = newTile.PrimaryPart.CFrame*CFrame.Angles(0, math.pi/2, 0)
									newTile:PivotTo(lastConnectorTable[directionChosen].CFrame)
									break
								end
							end
						end
						for a,b in pairs(currentConnectorTable) do
							for c,d in pairs(b:GetTouchingParts()) do
								if d:FindFirstAncestor(newTile) == nil and d.Parent.Name == "Connectors" then
									d:Destroy()
									b:Destroy()
								end
							end
						end
						table.insert(prevTable,newTile)
					end
				end
			end
		end
		tileTable = prevTable
		--print(tileTable,prevTable)
	end
end

Any ideas on how I would be able to achieve this?

1 Like

Perhaps this video can help you.

Or maybe

Good luck! :slight_smile:

2 Likes