Working Mazes, only odd sizes allowed, obviously. Takes about 3 seconds for 300x500, normally a 61x61 will suffice for the hardcore

This commit is contained in:
ExeVirus 2021-02-05 22:23:57 -05:00
parent ab02d91fd5
commit 95a47efad1

View File

@ -50,60 +50,61 @@ local function getNextPoint(current_point,height,width,last_dir)
--keep trying till we get a valid direction --keep trying till we get a valid direction
while(1) do while(1) do
local dir = math.random(3) local dir = math.random(3)
if last_dir == 1 then --west if last_dir == 3 then --east
if dir == 1 then --north if dir == 1 then --north
if current_point[2]-2 > 0 then if current_point[1]-2 > 0 then
return {current_point[1],current_point[2]-2}, 2 return {current_point[1]-2,current_point[2]}, 2
end end
elseif dir == 2 then --east elseif dir == 2 then --east
if current_point[1]+2 <= width then if current_point[2]+2 <= width then
return {current_point[1]+2,current_point[2]}, 3 return {current_point[1],current_point[2]+2}, 3
end end
else --south else --south
if current_point[2]+2 <= height then if current_point[1]+2 <= height then
return {current_point[1],current_point[2]+2}, 4 return {current_point[1]+2,current_point[2]}, 4
end end
end end
elseif last_dir == 2 then --north elseif last_dir == 4 then --south
if dir == 1 then --east if dir == 1 then --east
if current_point[1]+2 <= width then if current_point[2]+2 <= width then
return {current_point[1]+2,current_point[2]}, 3 return {current_point[1],current_point[2]+2}, 3
end end
elseif dir == 2 then --south elseif dir == 2 then --south
if current_point[2]+2 <= height then if current_point[1]+2 <= height then
return {current_point[1],current_point[2]+2}, 4 return {current_point[1]+2,current_point[2]}, 4
end end
else --west else --west
if current_point[1]-2 > 0 then if current_point[2]-2 > 0 then
return {current_point[1]-2,current_point[2]}, 1 return {current_point[1],current_point[2]-2}, 1
end end
end end
elseif last_dir == 3 then elseif last_dir == 1 then --west
if dir == 1 then --south if dir == 1 then --south
if current_point[2]+2 <= height then if current_point[1]+2 <= height then
return {current_point[1],current_point[2]+2}, 4 return {current_point[1]+2,current_point[2]}, 4
end end
elseif dir == 2 then --west elseif dir == 2 then --west
if current_point[1]-2 > 0 then if current_point[2]-2 > 0 then
return {current_point[1]-2,current_point[2]}, 1 return {current_point[1],current_point[2]-2}, 1
end end
else --north else --north
if current_point[2]-2 > 0 then
return {current_point[1],current_point[2]-2}, 2
end
end
else
if dir == 1 then --west
if current_point[1]-2 > 0 then if current_point[1]-2 > 0 then
return {current_point[1]-2,current_point[2]}, 1 return {current_point[1]-2,current_point[2]}, 2
end
end
else --north
if dir == 1 then --west
if current_point[2]-2 > 0 then
return {current_point[1],current_point[2]-2}, 1
end end
elseif dir == 2 then --north elseif dir == 2 then --north
if current_point[2]-2 > 0 then if current_point[1]-2 > 0 then
return {current_point[1],current_point[2]-2}, 2 return {current_point[1]-2,current_point[2]}, 2
end end
else --east else --east
if current_point[1]+2 <= width then if current_point[2]+2 <= width then
return {current_point[1]+2,current_point[2]}, 3 return {current_point[1],current_point[2]+2}, 3
end end
end end
end end
@ -144,17 +145,17 @@ end
local function onChain(chain,point) local function onChain(chain,point)
for _,v in pairs(chain) do for _,v in pairs(chain) do
if point == v then return true end if point[1] == v[1] and point[2] == v[2] then return true end
end end
return false return false
end end
local function backTrack(chain, point) local function backTrack(chain, point)
for i=#chain,1,-1 do for i=#chain,1,-1 do
if point == chain[i] then if point[1] == chain[i][1] and point[2] == chain[i][2] then
return point, chain return point, chain
else else
table.remove(chain) table.remove(chain,i)
end end
end end
error("it was onChain, but not found....") error("it was onChain, but not found....")
@ -167,12 +168,13 @@ function sleep(n) -- seconds
end end
local function wilsonsAlgo(maze,view) local function wilsonsAlgo(maze,view)
local view_count_down = 3 local view_count_down = 1
local current_point = findNextStarting(maze) local current_point = findNextStarting(maze)
local last_dir = 2 --1 = west, 2 = north, 3=east, 4=south local last_dir = 2 --1 = west, 2 = north, 3=east, 4=south
local next_point = {}
local chain = { current_point } local chain = { current_point }
while(1) do --exited with return statement while(1) do --exited with return statement
local next_point, last_dir = getNextPoint(current_point,maze.height,maze.width,last_dir) next_point, last_dir = getNextPoint(current_point,maze.height,maze.width,last_dir)
if onChain(chain,next_point) then if onChain(chain,next_point) then
current_point, chain = backTrack(chain,next_point) current_point, chain = backTrack(chain,next_point)
elseif maze[next_point[1]][next_point[2]] == 1 then elseif maze[next_point[1]][next_point[2]] == 1 then
@ -194,7 +196,7 @@ local function wilsonsAlgo(maze,view)
os.execute("cls") os.execute("cls")
viewMaze(maze, chain) viewMaze(maze, chain)
sleep(1) sleep(1)
view_count_down = 3 view_count_down = 5
end end
end end
end end
@ -228,8 +230,10 @@ local function Generate_Maze(width, height, view)
maze.height = height maze.height = height
--initialize first point --initialize first point
math.randomseed(os.time()) math.randomseed(os.time())
local spot = findNextStarting(maze) local x = math.random(math.floor(width/2))*2+1
maze[spot[1]][spot[2]] = 1 local y = math.random(math.floor(height/2))*2+1
--local spot = findNextStarting(maze)
maze[y][x] = 1
--Execute Algorithm --Execute Algorithm
wilsonsAlgo(maze, view) wilsonsAlgo(maze, view)
@ -238,6 +242,6 @@ local function Generate_Maze(width, height, view)
return maze return maze
end end
local maze = Generate_Maze(21,21,"true") --Only odd sizes allowed local maze = Generate_Maze(531,251,"false") --Only odd sizes allowed
os.execute("cls") os.execute("cls")
viewMaze(maze) viewMaze(maze)