Maze Generation not working

  1. I need my maze generation able to be solved

  2. It produces a maze that looks like it can be solved, but there are no walls deleted inbetween the start and the first position

  3. I have tried reading how the maze generation (Wilson’s Algorithm) works and its fine but just the second problem I don’t know how to fix

The maze generation algorithm I am using (Wilson’s Algorithm) does Loop-Erased Random Walk (which means it walks to a random direction, if its part of the path then it erases the stuff after that path, then repeating until the maze is finished)

here is the code for it

local width = 20
local height = 20
local heightbounds = 1
local cells = {}

for w=1,width do
	cells[w] = {}
	for h=1,height do
		cells[w][h] = {}
		cells[w][h]["walls"] = {}
		if w < width then
			cells[w][h]["walls"]["R"] = true
		end
		if h < height+heightbounds and h > heightbounds then
			cells[w][h]["walls"]["D"] = true
		end
		cells[w][h]["visited"] = false
	end
end
local poswalls = {R=Vector3.new(.5,1,0),D=Vector3.new(0,1,-.5)}
local sizewalls = {R=Vector3.new(0,1,1),D=Vector3.new(1,1,0)}
function makemaze()
	print("makemaze")
	for w,v in pairs(cells) do
		for h,v in pairs(v) do
			local cell = cells[w][h]
			local floor = Instance.new("Part")
			floor.Anchored = true
			floor.Color = Color3.fromRGB(0,0,math.random(0,255))
			floor.Parent = workspace
			floor.Position = Vector3.new(w,.5,h)
			floor.Size = Vector3.new(1,.1,1)
			floor.Name = tostring(w).."_"..tostring(h)
			for i,v in pairs(cell["walls"]) do
				if v == true then
					local wall = Instance.new("Part")
					wall.Anchored = true
					wall.Color = Color3.fromRGB(0,0,math.random(0,255))
					wall.Parent = workspace
					wall.Position = Vector3.new(w,0,h) + poswalls[i]
					wall.Size = sizewalls[i]
					wall.Name = tostring(w).."_"..tostring(h).."_"..i
				end
			end
		end
	end
end
makemaze()
local trypos = {1,1}
local walldiff = {}
walldiff[1] = {}
walldiff[1][0] = "R"
walldiff[-1] = {}
walldiff[-1][0] = "L" 
walldiff[0] = {}
walldiff[0][1] = "U"
walldiff[0][-1] = "D"
local path = {}
local origconnect = nil
local restart = false
local waitnum = 0
local waitevery = 3
local visitedcells = {}
local startcells = {}
function doit(s)
	if s == true then
		cells[trypos[1]][trypos[2]].visited = true
		table.insert(visitedcells,trypos)
		randomwalk(trypos)
	else
		startcells = {}
		local path2 = {}
		for i,v in pairs(visitedcells) do
			local availablepaths = {{1,0},{-1,0},{0,1},{0,-1}}
			for i2,v2 in pairs(availablepaths) do
				if cells[v[1]+v2[1]] and cells[v[1]+v2[1]][v[2]+v2[2]] and cells[v[1]+v2[1]][v[2]+v2[2]].visited == false then
					table.insert(startcells,{v[1]+v2[1],v[2]+v2[2]})
					table.insert(path2,{v[1],v[2]})
				end
			end
		end
		local num = math.random(#startcells)
		local randomcell = startcells[num]
		path[#path+1] = path2[num]
		randomwalk(randomcell,path2[num])
	end
end
function randomwalk(pos,s)
	print("start")
	if restart == true then
		path = {}
		origconnect = nil
		restart = false
		for w=1,width do
			cells[w] = {}
			for h=1,height do
				cells[w][h] = {}
				cells[w][h].walls = {}
				if w < width then
					cells[w][h]["walls"]["R"] = true
				end
				if h < height+heightbounds and h > heightbounds then
					cells[w][h]["walls"]["D"] = true
				end
				cells[w][h].visited = false
			end
		end
		for i,v in pairs(game.Workspace:GetChildren()) do
			if string.find(v.Name,"_") then
				v:Destroy()
			end
		end
		makemaze()
		wait(1)
		path = {}
		origconnect = nil
		restart = false
		pos = nil
		s = nil
![bandicam 2024-07-04 10-07-48-408|video](upload://w6f6nwNt7wqi8KJIB39TVsy65L9.mp4)

		doit(true)
	else
		waitnum += 1
		if waitnum%waitevery == 0 then
			task.wait()
		end
		if s ~= nil then
			origconnect = s or pos
			cells[origconnect[1]][origconnect[2]].visited = true
			workspace[tostring(pos[1]).."_"..tostring(pos[2])].Color = Color3.fromRGB(255,0,0)
		end
		local x = pos[1]
		local y = pos[2]
		local availablepaths = {{1,0},{-1,0},{0,1},{0,-1}}
		local pathchosen = nil
		for i,v in pairs(availablepaths) do
			local x = v[1] + x
			local y = v[2] + y
			for i2,v2 in pairs(path) do
				if v[1] == v2[1] and v[2] == v2[2] then
					table.remove(path,i)
				end
			end
		end
		print("erm1")
		repeat
			local num = math.random(#availablepaths)
			if num > 0 then
				local randompath = availablepaths[num]
				randompath[1] += x
				randompath[2] += y
				if randompath[1] <= width and randompath[1] > 0 and randompath[2] >= heightbounds and randompath[2] <= height then
					pathchosen = randompath
				else
					table.remove(availablepaths,num)
				end
			end
		until pathchosen ~= nil or #availablepaths <= 0
		print("erm2")
		if pathchosen ~= nil then
			local successwalk = true
			for i,v in pairs(path) do
				if v[1] == pathchosen[1] and v[2] == pathchosen[2] then
					--print("erase")
					successwalk = false
					for i2,v in pairs(path) do
						if i2 > i then
							path[i2] = nil
							local isntvisited = true
							for fgdbsjkgfdhskfdsa,v2 in pairs(visitedcells) do
								if v[1] == v2[1] and v[2] == v2[2] then
									isntvisited = false
								end
							end
							if isntvisited then
								workspace[tostring(v[1]).."_"..tostring(v[2])].Color = Color3.fromRGB(0,0,math.random(0,255))
							end
						end
					end
					print("Done Erase")
					randomwalk(v)
				end
			end
			--print(cells[pathchosen[1]][pathchosen[2]].visited)
			if cells[pathchosen[1]][pathchosen[2]].visited == true then-- or (pathchosen[1]-trypos[1] == 0 and pathchosen[2]-trypos[2] == 0) then
				print("erm3")
				if origconnect and path[1] then
					local v = origconnect
					local v2 = path[1]
					print(v)
					print(v2)
					local xdiff = v2[1]-v[1]
					local ydiff = v2[2]-v[2]
					if walldiff[xdiff] and walldiff[xdiff][ydiff] then
						local cell1 = cells[v[1]][v[2]]
						local cell2 = cells[v2[1]][v2[2]]
						cell1.visited = true
						cell2.visited = true
						table.insert(visitedcells,v)
						table.insert(visitedcells,v2)
						local walldestroy = walldiff[xdiff][ydiff]
						workspace[tostring(v[1]).."_"..tostring(v[2])].Color = Color3.fromRGB(255,255,0)
						workspace[tostring(v2[1]).."_"..tostring(v2[2])].Color = Color3.fromRGB(255,255,0)
						if walldestroy == "R" then
							cell1.walls["R"] = false
							local wall = workspace[tostring(v[1]).."_"..tostring(v[2]).."_R"]
							wall.Transparency = 1
							wall.CanCollide = false
						elseif walldestroy == "L" then
							cell2.walls["R"] = false
							local wall = workspace[tostring(v2[1]).."_"..tostring(v2[2]).."_R"]
							wall.Transparency = 1
							wall.CanCollide = false
						elseif walldestroy == "U" then
							local wall = workspace[tostring(v2[1]).."_"..tostring(v2[2]).."_D"]
							wall.Transparency = 1
							wall.CanCollide = false
						elseif walldestroy == "D" then
							local wall = workspace[tostring(v[1]).."_"..tostring(v[2]).."_D"]
							wall.Transparency = 1
							wall.CanCollide = false
						end
					end
				end
				for i,v in pairs(path) do
					if path[i+1] ~= nil then
						task.wait()
						local v2 = path[i+1]
						local xdiff = v2[1]-v[1]
						local ydiff = v2[2]-v[2]
						if walldiff[xdiff] and walldiff[xdiff][ydiff] then
							local cell1 = cells[v[1]][v[2]]
							local cell2 = cells[v2[1]][v2[2]]
							cell1.visited = true
							cell2.visited = true
							table.insert(visitedcells,v)
							table.insert(visitedcells,v2)
							local walldestroy = walldiff[xdiff][ydiff]
							workspace[tostring(v[1]).."_"..tostring(v[2])].Color = Color3.fromRGB(255,255,0)
							workspace[tostring(v2[1]).."_"..tostring(v2[2])].Color = Color3.fromRGB(255,255,0)
							if walldestroy == "R" then
								cell1.walls["R"] = false
								local wall = workspace[tostring(v[1]).."_"..tostring(v[2]).."_R"]
								wall.Transparency = 1
								wall.CanCollide = false
							elseif walldestroy == "L" then
								cell2.walls["R"] = false
								local wall = workspace[tostring(v2[1]).."_"..tostring(v2[2]).."_R"]
								wall.Transparency = 1
								wall.CanCollide = false
							elseif walldestroy == "U" then
								local wall = workspace[tostring(v2[1]).."_"..tostring(v2[2]).."_D"]
								wall.Transparency = 1
								wall.CanCollide = false
							elseif walldestroy == "D" then
								local wall = workspace[tostring(v[1]).."_"..tostring(v[2]).."_D"]
								wall.Transparency = 1
								wall.CanCollide = false
							end
						end
					end
				end
				path = {}
				print("Done Connect")
			elseif successwalk then
				path[#path+1] = pathchosen
				workspace[tostring(pathchosen[1]).."_"..tostring(pathchosen[2])].Color = Color3.fromRGB(0,255,0)
				--print("walk")
				print("Done Walk")
				randomwalk(pathchosen)
			end
		end
	end
end
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr) -- this is only for testing i will delete it after
	restart = true
end)
doit(true)
while wait(.5) do
	doit()
end
1 Like

Videos:


1 Like