Maze trouble

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.

@Monkeyman32123 Where is the original code you used, that might help to figure out why yours isn’t working.

The original code is all intact and functional (the only major change being that it is now a class), the only issue is a function I tried to add (convertToBlockmaze). It should take the original format and change it to a jumper-compatible map; it doesn’t work. Though if you don’t like reading all of the selfs then the original code is from the discussion “converting a for() loop” http://codea.io/talk/discussion/5895/converting-a-for-loop#Item_11.

It should be noted that the function works until the lines that would actually make it into the proper maze

local x,y,z,w...
if w~=1...end

Commenting out that block makes it do exactly what I would expect it to do without them, so I think it is something in that block

EDIT:
Oddly, if I change ‘local x,y,z,w’ to ‘local y,x,w,z’ it produces the correct image but flipped along the line y=x; and I don’t know how to make it produce the correct image

@Monkeyman32123 I made these changes to your code and ran it in portrait mode. Does the block maze that you create supposed to be the same maze that is drawn above it.

function Mazeclass:draw()

        sprite(self.img,WIDTH/2,HEIGHT/2+200)      -- add 200 to height

function draw()

    maze:draw()                          -- uncommented this line 

It is supposed to be the same maze, yes

@Monkeyman32123 Is this what you’re after.


supportedOrientations(LANDSCAPE_ANY)

function setup()
    parameter.integer("change",2,50,15)
    setup1()
end

function setup1()
    tab={vec2(0,1),vec2(1,0),vec2(0,-1),vec2(-1,0)}
    tab1={3,4,1,2}
    locations={}
    square={}
    dim=change
    size=WIDTH/(dim+5)
    for z=1,dim*dim do
        square[z]=nil
    end
    curr=vec2(math.random(dim),math.random(dim))
    c=curr.x*dim+curr.y-dim
    square[c]=vec4(1,1,1,1)
    table.insert(locations,curr)
    prevCurr=vec2(0,0)
    show=true
    while #locations>0 do
        nextSquare() 
        if prev then
            curr=locations[#locations]
            table.remove(locations,#locations)
            prevCurr=curr
        else
            if prevCurr.x>0 then
                table.insert(locations,prevCurr)
                prevCurr=vec2(0,0)
            end
            table.insert(locations,curr)
        end
    end
    square[1][4]=0
    square[dim*dim][2]=0
    block={}
    for x=1,2*dim+1 do
        block[x]={}
        for y=1,2*dim+1 do
            block[x][y]=0
        end
    end
    v=0
    for x=1,dim do
        for y=1,dim do
            v=v+1
            if square[v].x==1 then -- top line
                block[x*2-1][y*2+1]=1
                block[x*2][y*2+1]=1
                block[x*2+1][y*2+1]=1
            end
            if square[v].y==1 then -- right line
                block[x*2+1][y*2+1]=1
                block[x*2+1][y*2]=1
                block[x*2+1][y*2-1]=1
            end
            if square[v].z==1 then -- bottom line
                block[x*2-1][y*2-1]=1
                block[x*2][y*2-1]=1
                block[x*2+1][y*2-1]=1
            end
            if square[v].w==1 then -- left line
                block[x*2-1][y*2+1]=1
                block[x*2-1][y*2]=1
                block[x*2-1][y*2-1]=1
            end
        end
    end
end

function draw()
    if not show then 
        return
    end
    background(40, 40, 50)
    stroke(255)
    strokeWidth(1)  
    if #locations==0 then
        text("Tap screen for a new maze",WIDTH/2,HEIGHT-20)
        stroke(255)
        strokeWidth(2)
        for x=1,2*dim+1 do
            for y=1,2*dim+1 do
                if block[x][y]==1 then
                    rect(20+x*size/2,30+y*size/2,size/2,size/2)
                end
            end
        end
    end
end

function nextSquare()
    done=false
    prev=false
    side={1,2,3,4}
    while not done do
        rs=math.random(#side)
        r=side[rs]
        table.remove(side,rs)
        nxt=curr+tab[r]
        if nxt.x>=1 and nxt.x<=dim and nxt.y>=1 and nxt.y<=dim then
            c=curr.x*dim+curr.y-dim
            n=nxt.x*dim+nxt.y-dim
            if square[n]==nil then
                square[n]=vec4(1,1,1,1)
                square[c][r]=0
                square[n][tab1[r]]=0
                curr=nxt
                done=true
                return
            end              
        end
        if #side==0 then
            prev=true
            done=true
        end
    end 
end

function touched(t)
    if t.state==BEGAN then
        setup1()
    end
end

That’s Exactly what I was searching for, thank you! It seems everyone is a fan of the liney type, but that can’t be used with jumper