Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Problems with maze generator ported from JS to Lua?

Asked by
compUcomp 417 Moderation Voter
5 years ago

A while back, I made a maze generator in JavaScript using a popular algorithm. It never fails to produce a solvable maze. I thought it would be interesting if I could port it to Lua, so I started working. This is what I came up with:

01local function genMaze()
02    local tilesDone = 1
03    local tilesGoal = width * height
04    local stack = {{
05        pos = {math.random(0, width - 1), math.random(0, height - 1)},
06        open = {false, false, false, false}
07    }}
08    maze = {}
09    maze[stack[1].pos[2] * width + stack[1].pos[1]] = stack[1]
10    while tilesDone < tilesGoal do
11        local currentNode = stack[#stack]
12        local path = randPath(currentNode);
13        while path == -1 do
14            -- if tilesDone < tilesGoal, stack.length will never be 0
15            table.remove(stack, #stack)
View all 37 lines...

I rewrote the script line by line. Lua's 1-based indexing was a problem, but I got the mistakes ironed out... or so I thought. This version rarely produces a solvable maze. Isolated cells are made frequently, and large chunks of the map are often cut off. No errors are produced. What am I doing wrong?

Javascript version

Full script:

001local width, height = 10, 10
002local function mapRange(v, x1, y1, x2, y2)
003    return (v - x1) * (y2 - x2) / (y1 - x1) + x2
004end
005local function line(v1, v2)
006    local angle = math.atan2(v2.Y - v1.Y, v2.X - v1.X)
007    local frame = Instance.new("Frame", script.Parent.Puzzle)
008    frame.Name = "Line"
009    frame.BackgroundColor3 = Color3.new(0, 0, 0)
010    frame.BorderSizePixel = 0
011    frame.Size = UDim2.new(
012        mapRange(
013            (v1 - v2).Magnitude,
014            0,
015            script.Parent.AbsoluteSize.X,
View all 158 lines...
1
thanks for the javascript and html code bro, this will help me LOL greatneil80 2647 — 5y
1
I don't have a lot of time to look at it, but I noticed on line 22 you are indexing the open table with a value between 0 and 3 instead of 1 and 4. Will let you know if I find anything else vector3_zero 1056 — 5y
0
Fixed, has no effect compUcomp 417 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

I found your problem. Change line 22 to:

1open[((path-1)+2)%4+1] = true

or more succinctly:

1open[(path+1)%4+1] = true

With just this line changed in the above code everything works like it should.

0
Thanks for your answer! I'll test it soon and accept it if it works compUcomp 417 — 5y
0
It works! tysm compUcomp 417 — 5y
Ad

Answer this question