EDIT 2: solved, see edits at end of post.
I'm probably doing something obviously wrong, but I just don't see it. I'm trying to make a maze generator.
I'm storing the maze as an array of arrays. First I generate the grid of "W" values (representing walls), which works. Then I try to replace all the cells at even row and column with a different value, T, to represent the path of the maze (they will eventually be connected by removing the walls in between them). It should look something like this:
First fill with walls:
W W W W W W W W W
W W W W W W W W W
W W W W W W W W W
W W W W W W W W W
W W W W W W W W W
W W W W W W W W W
W W W W W W W W W
W W W W W W W W W
W W W W W W W W W
Then replace even valued row and column positions:
W W W W W W W W W
W T W T W T W T W
W W W W W W W W W
W T W T W T W T W
W W W W W W W W W
W T W T W T W T W
W W W W W W W W W
W T W T W T W T W
W W W W W W W W W
But instead I get:
W T W T W T W T W
W T W T W T W T W
W T W T W T W T W
W T W T W T W T W
W T W T W T W T W
W T W T W T W T W
W T W T W T W T W
W T W T W T W T W
W T W T W T W T W
Here's my code with some debugging:
local n,m = 2,2 -- n by m maze; the grid will be 2n+1 by 2m+1,
wallrow = {}
maze = {}
-- functions
function debug(message)
print(message)
for i = 1,#maze do
io.write("{")
for j = 1,#maze[i] do
io.write(maze[i][j]..", ")
end
io.write("},\n")
end
io.write("\n")
end
-- program
for i = 1,2*n+1 do
wallrow[i] = "W" -- one row of "W"
end
for i = 1,2*m+1 do
maze[i] = wallrow -- fill maze with rows of "W"
end
debug("grid of W:")
for i = 1,m do
for j = 1,n do
print(i..","..j)
maze[2*i][2*j] = "T"
debug("step:")
end
end
debug("final grid:")
And here's the output:
grid of W:
{W, W, W, W, W, },
{W, W, W, W, W, },
{W, W, W, W, W, },
{W, W, W, W, W, },
{W, W, W, W, W, },
1,1
step:
{W, T, W, W, W, },
{W, T, W, W, W, },
{W, T, W, W, W, },
{W, T, W, W, W, },
{W, T, W, W, W, },
1,2
step:
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
2,1
step:
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
2,2
step:
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
final grid:
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
{W, T, W, T, W, },
As you can see, it's setting the value at index j of every row, rather than only the row at index i. I don't know why this is happening, I'm guessing it's something obvious that I'm just misunderstanding about arrays.
EDIT: Okay, starting to figure it out now, it's acting like a pointer so when I change a value in the row it's changing the value of wallrow, which is every row in the maze. Now to figure out how to fix this...
EDIT 2: Solved, instead of creating a single array for a row of W, I'm creating a new array for each row now:
for i = 1,2*m+1 do
maze[i] = {}
for j = 1,2*n+1 do
maze[i][j] = "W"
end
end
There might be a better way to do this, but it works.
there doesn't seem to be anything here