I converted @dave1707’s maze creation code into a class, and I have been trying to make a function that converts the maze format into a blocky format that works with the jumper library. Unfortunately, my attempt doesn’t look…well…right. At all
Mazeclass = class()
function Mazeclass:init(dim)
self.dim = dim
self:setup1()
end
function Mazeclass:draw()
if self.change then
return
end
stroke(255)
strokeWidth(1)
if not self.img then
self.img = image((self.dim+1)*self.size+2,(self.dim+1)*self.size+2)
setContext(self.img)
if #self.locations==0 then
self.square[1][4]=0
self.square[self.dim*self.dim][2]=0
stroke(255)
strokeWidth(2)
local v=0
for a=1,self.dim do
for b=1,self.dim do
v=v+1
if self.square[v].x==1 then -- top line
line(a*self.size,(b+1)*self.size,(a+1)*self.size,(b+1)*self.size)
end
if self.square[v].y==1 then -- right line
line((a+1)*self.size,b*self.size,(a+1)*self.size,(b+1)*self.size)
end
if self.square[v].z==1 then -- bottom line
line(a*self.size,b*self.size,(a+1)*self.size,b*self.size)
end
if self.square[v].w==1 then -- left line
line(a*self.size,b*self.size,a*self.size,(b+1)*self.size)
end
end
end
end
setContext()
else
sprite(self.img,WIDTH/2,HEIGHT/2)
end
end
function Mazeclass:setup1(dim)
self.dim = dim or self.dim
self.img = nil
self.tab={vec2(0,1),vec2(1,0),vec2(0,-1),vec2(-1,0)}
self.tab1={3,4,1,2}
self.locations={}
self.square={}
self.size=WIDTH/(self.dim+5)
for z=1,self.dim*self.dim do
self.square[z]=nil
end
self.curr=vec2(math.random(self.dim),math.random(self.dim))
self.c=self.curr.x*self.dim+self.curr.y-self.dim
self.square[self.c]=vec4(1,1,1,1)
table.insert(self.locations,self.curr)
self.prevCurr=vec2(0,0)
while #self.locations>0 do
self:nextSquare()
if self.prev then
self.curr=self.locations[#self.locations]
table.remove(self.locations,#self.locations)
self.prevCurr=self.curr
else
if self.prevCurr.x>0 then
table.insert(self.locations,self.prevCurr)
self.prevCurr=vec2(0,0)
end
table.insert(self.locations,self.curr)
end
end
self.change=false
end
function Mazeclass:nextSquare()
self.done=false
self.prev=false
self.side={1,2,3,4}
while not self.done do
self.rs=math.random(#self.side)
self.r=self.side[self.rs]
table.remove(self.side,self.rs)
self.nxt=self.curr+self.tab[self.r]
if self.nxt.x>=1 and self.nxt.x<=self.dim and self.nxt.y>=1 and self.nxt.y<=self.dim then
self.c=self.curr.x*self.dim+self.curr.y-self.dim
self.n=self.nxt.x*self.dim+self.nxt.y-self.dim
if self.square[self.n]==nil then
self.square[self.n]=vec4(1,1,1,1)
self.square[self.c][self.r]=0
self.square[self.n][self.tab1[self.r]]=0
self.curr=self.nxt
self.done=true
return
end
end
if #self.side==0 then
self.prev=true
self.done=true
end
end
end
function Mazeclass:change()
self.change=true
end
function Mazeclass:convertToBlockmaze()
local blockmap = {}
local v = 0
for i=1,self.dim do
if i == 1 then
blockmap[(i*2)-1],blockmap[i*2],blockmap[(i*2)+1] = {},{},{}
for z=1,self.dim*2+1 do
blockmap[(i*2)-1][z],blockmap[i*2][z],blockmap[(i*2)+1][z] = 1,1,1
end
else
blockmap[i*2],blockmap[(i*2)+1] = {},{}
for z=1,self.dim*2+1 do
blockmap[i*2][z],blockmap[(i*2)+1][z] = 1,1
end
end
for j=1, self.dim do
v = v + 1
-- current square
blockmap[i*2][j*2]=0
local x,y,z,w = self.square[v].x,self.square[v].y,self.square[v].z,self.square[v].w
-- top line
if x ~= 1 then blockmap[(i*2)+1][j*2] = 0 end
-- right line
if y ~= 1 then blockmap[i*2][(j*2)+1] = 0 end
-- bottom line
if z ~= 1 then blockmap[(i*2)-1][j*2] = 0 end
-- left line
if w ~= 1 then blockmap[i*2][(j*2)-1] = 0 end
end
end
self.bm = blockmap
return blockmap
end
function setup()
maze = Mazeclass(30)
cow = loadstring("10")
if cow == 10 then close() else print(cow) end
map = maze:convertToBlockmaze()
end
function draw()
lineCapMode(PROJECT)
background(40, 40, 50)
--maze:draw()
for i=1,#map do
for j=1,#map[i] do
fill(255-(map[i][j])*255)
WIDTH = math.min(WIDTH,HEIGHT)
rect((j-1)*WIDTH/#map[i],(i-1)*WIDTH/#map[i],WIDTH/#map[i],WIDTH/#map[i])
end
end
end
function touched(t)
if t.tapCount == 3 and t.state==ENDED then
maze:setup1(math.random(100))
end
end
The error in question is in e convert to blockmap function. I post it here in hopes that it is but a simple mathematics error.