Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

15 puzzle -- SetConext() demo

edited January 2012 in Code Sharing Posts: 622

The intial image is split into 16 parts (images), one of which is thrown out to make the puzzle.


--15 puzzle
function setup()
    pp = {} -- puzzle peices
    for i = 1,15 do
        pp[i] = {}
        pp[i].i = image(187,187)
    end
    done = 0
    dots = {}
    for i = 1,100 do
        x = math.random(648)+50
        y = math.random(648)+50
        r = math.random(256)
        g = math.random(256)
        b = math.random(256)
        a = math.random(256)
        dots[i] = {x=x,y=y,r=r,g=g,b=b,a=a}
    end
    print("touch near the empty square")
end

function draw()
    background(0, 0, 0)
    stroke(0,0,0,0)
    strokeWidth(0)
    if done < 2 then --draw the peices into the table of image(s)
        drawps()
        setContext()
        done = done + 1
    end
    spriteMode(CORNER)
    for i = 1, 15 do
        sprite(pp[i].i, pp[i].x, pp[i].y)
    end
end

function touched(touch)
    if touch.state == BEGAN then
        for i = 1, 15 do --find out what is touched
            v1 = vec2(touch.x,touch.y)
            if v1:dist(vec2(pp[i].x + 93, pp[i].y + 93)) < 93 then --this is moving
                local empty_up = true
                local empty_down = true
                local empty_left = true
                local empty_right = true
                if pp[i].fx == 1 then empty_left = false end
                if pp[i].fx == 4 then empty_right = false end
                if pp[i].fy == 1 then empty_down = false end
                if pp[i].fy == 4 then empty_up = false end 
                for j = 1, 15 do
                    if pp[i].fx == pp[j].fx - 1
                    and pp[i].fy == pp[j].fy then
                        empty_right = false
                    end
                    if pp[i].fx == pp[j].fx + 1
                    and pp[i].fy == pp[j].fy then
                        empty_left = false
                    end
                    if pp[i].fy == pp[j].fy - 1
                    and pp[i].fx == pp[j].fx then
                        empty_up = false
                    end
                    if pp[i].fy == pp[j].fy + 1
                    and pp[i].fx == pp[j].fx then
                        empty_down = false
                    end
                end
                if empty_right then pp[i].fx = pp[i].fx + 1 end
                if empty_left then pp[i].fx = pp[i].fx - 1 end    
                if empty_up then pp[i].fy = pp[i].fy + 1 end
                if empty_down then pp[i].fy = pp[i].fy - 1 end    
                --print(empty_right,empty_left,empty_up,empty_down)
            end
            pp[i].x = (pp[i].fx - 1) * 187
            pp[i].y = (pp[i].fy - 1) * 187
        end
    end
end                     

function drawps()
    xx = 0
    yy = 0
    drawp(1,0,-187*3)
    drawp(2,-187,0)
    drawp(3,-187,0)
    drawp(4,-187,0)
    drawp(5,187*3,187)
    drawp(6,-187,0)
    drawp(7,-187,0)
    drawp(8,-187,0)
    drawp(9,187*3,187)
    drawp(10,-187,0)
    drawp(11,-187,0)
    drawp(12,-187,0)
    drawp(13,187*3,187)
    drawp(14,-187,0)
    drawp(15,-187,0)
end

function drawp(i,x,y)
    xx = xx - x
    yy = yy - y    
    pp[i].x = xx
    pp[i].y = yy
    pp[i].fx = (xx/187) + 1    
    pp[i].fy = (yy/187) + 1
    pp[i].tx = nil
    pp[i].ty = nil
    setContext(pp[i].i)
    translate(x,y)
    drawdots()
end

function drawdots()
    for i = 1, 100 do
        fill(dots[i].r,dots[i].g,dots[i].b,255)
        ellipse(dots[i].x,dots[i].y,100,100)
    end       
end

    

Comments

  • Posts: 273

    "Well, that's a devilishly difficult demo!" he said after he shuffled the pieces -- with too much bravado -- and then tried to put it back together again.

Sign In or Register to comment.