Math.random + table help

@dave1707 I figured it out but I feel like there is a simpler method.

displayMode(FULLSCREEN)    

function setup()
    spriteMode(CENTER)
    ellipseMode(CENTER)

    et=ElapsedTime
    count=30

    tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
    tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
    tab6={vec2(800,250),vec2(800,400),vec2(800,550)} 
    tab7={vec2(800,400),vec2(800,550)}
    tab8={vec2(800,550)}
    tab3={vec3(800,108,.5),vec3(800,258,.5),vec3(800,408,.5),vec3(800,558,.5)}  -- position of sprite and scaler value
    tab4={
            asset.builtin.Planet_Cute.Character_Princess_Girl,
            asset.builtin.Planet_Cute.Character_Boy,
            asset.builtin.Planet_Cute.Character_Cat_Girl,
            asset.builtin.Planet_Cute.Character_Horn_Girl
        }
    imageSize = spriteSize(tab4[1])
    tab5={}
    for z=1,#tab4 do
        a=math.random(#tab4)
        table.insert(tab5,tab4[a])
        table.remove(tab4,a)
    end
    scaler = .50
end

function draw()
    background(98)
    strokeWidth(5)
    fill(128)
    for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)
    end
    for a,b in pairs(tab2) do
        ellipse(b.x, b.y, 50)
    end
       fill(255)
    if ElapsedTime-et>1 then
        count = count - 1
        et=ElapsedTime
    end

   if count<=28 then
        for a,b in pairs(tab3) do
    sprite(tab5[a],b.x,b.y,imageSize*b.z)
            end
        end
    pushStyle()
    fill(128)
    if count==28 then 
    for a,b in pairs(tab6) do
        ellipse(b.x, b.y, 50)
    end
        end
    
     if count==27 then 
    for a,b in pairs(tab7) do
        ellipse(b.x, b.y, 50)
    end
        end
    
       if count==26 then 
    for a,b in pairs(tab8) do
        ellipse(b.x, b.y, 50)
    end
        end
   popStyle()

     text(count,WIDTH*.10,HEIGHT*.10)
end

function touched(touch)    
    if touch.state==BEGAN then
        pos=0
        for a,b in pairs(tab3) do
            if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                        touch.y<b.y+40 then
                b.z=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x==d.x and b.y==d.y then
                        d.z=0
                    end
                end
            end
        end

    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab3[pos].x=tab3[pos].x+touch.deltaX
        tab3[pos].y=tab3[pos].y+touch.deltaY
    end

    if touch.state==ENDED then  
        for a,b in pairs(tab1) do
            if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                    tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                    b.z==0 then
                tab3[pos].x=b.x
                tab3[pos].y=b.y
                b.z=1
                return
            end            
        end
        tab3[pos].x=tab2[pos].x
        tab3[pos].y=tab2[pos].y
        tab3[pos].z=.5
    end
end
displayMode(FULLSCREEN)    

function setup()
    spriteMode(CENTER)
    ellipseMode(CENTER)

    tab1={vec3(100,290,0),vec3(250,290,0),vec3(400,290,0)}    -- position of large circles and occupied
    tab2={vec2(800,100),vec2(800,250),vec2(800,400),vec2(800,550)}  -- position of small circles
    tab3={vec3(800,100,.5),vec3(800,250,.5),vec3(800,400,.5),vec3(800,550,.5)}  -- position of sprite and scaler value
    tab4={
            asset.builtin.Planet_Cute.Character_Princess_Girl,
            asset.builtin.Planet_Cute.Character_Boy,
            asset.builtin.Planet_Cute.Character_Cat_Girl,
            asset.builtin.Planet_Cute.Character_Horn_Girl
        }
    imageSize = spriteSize(tab4[1])
    tab5={}
    for z=1,#tab4 do
        a=math.random(#tab4)
        table.insert(tab5,tab4[a])
        table.remove(tab4,a)
    end
    scaler = .50
    cnt=0
    loop=0
end

function draw()
    background(98)
    strokeWidth(5)
    fill(128)
    for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)
    end
    for a,b in pairs(tab2) do
        ellipse(b.x, b.y, 50)
    end
    delay()
    for z=1,cnt do
        sprite(tab5[z],tab3[z].x,tab3[z].y,imageSize*tab3[z].z)        
    end
end

function delay()
    loop=loop+1
    if loop>60 and cnt<#tab5 then
        cnt=cnt+1
        loop=0
    end            
end

function touched(touch)    
    if touch.state==BEGAN then
        pos=0
        for a,b in pairs(tab3) do
            if touch.x>b.x-40 and touch.x<b.x+40 and touch.y>b.y-40 and 
                        touch.y<b.y+40 then
                b.z=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x==d.x and b.y==d.y then
                        d.z=0
                    end
                end
            end
        end

    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab3[pos].x=tab3[pos].x+touch.deltaX
        tab3[pos].y=tab3[pos].y+touch.deltaY
    end

    if touch.state==ENDED then  
        for a,b in pairs(tab1) do
            if tab3[pos].x>b.x-55 and tab3[pos].x<b.x+55 and 
                    tab3[pos].y>b.y-55 and tab3[pos].y<b.y+55 and
                    b.z==0 then
                tab3[pos].x=b.x
                tab3[pos].y=b.y
                b.z=1
                return
            end            
        end
        tab3[pos].x=tab2[pos].x
        tab3[pos].y=tab2[pos].y
        tab3[pos].z=.5
    end
end

@dave1707 A delay loop function, nice! Thanks for the knowledge.

@Jarc There’s a problem in that delay function. Once all the sprites are shown, loop will add 1 to itself as long as the program is running. Even though that’s not a problem, here’s a better one. With the new one, once all the sprites are shown, the if statement is false and doesn’t execute any of the code inside it anymore.

function delay()
    if cnt<#tab5 then
        loop=loop+1
        if loop>60 then 
            cnt=cnt+1
            loop=0
        end  
    end          
end

@dave1707 I have some questions.

I want to add more sprites(3 for example) but keep the same number of small circle slots so once after scoring the first 3(round 1), it will repeat same beginning process but load the rest of the unused sprites so round 2 can began.

Also this is for visual effect but how do I get the sprites to score 1 by 1, left to right each round. Meaning how do I get the value scores to show up as text near the sprite for about 1 sec and simultaneously while the value text are being displayed on each sprite(1 by 1, left to right) I want it to update a onscreen scoreboard text that starts at 00 when game begins.

The two bottom ellipses I added are for round 2.

function setup()
    
    displayMode(FULLSCREEN)
    t={1,2,3}   -- number for each entry in tab4
    -- position of large circles and occupied
    tab1={vec3(320,290,0),vec3(445,290,0),vec3(570,290,0)} --1st round ellipse
    tab7={vec3(320,110,0),vec3(445,110,0)} --2nd round ellipse 
    -- position of small circles
    tab2={vec2(835,335),vec2(835,265),vec2(835,195),}  
    -- name, value, original x,y position, current x,y position, size
    tab4={  {n=asset.builtin.Planet_Cute.Character_Princess_Girl,
                v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Boy,
                v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Cat_Girl,
                v=12,x1=0,y1=0,x2=0,y2=0,s=.5}}
    
    imageSize = spriteSize(tab4[1].n)
    for z=1,#t do
        a=math.random(#t)
        tab4[z].x1=tab2[t[a]].x
        tab4[z].y1=tab2[t[a]].y
        tab4[z].x2=tab2[t[a]].x
        tab4[z].y2=tab2[t[a]].y
        table.remove(t,a)
    end
    cnt=0
    loop=0   
end

function draw()   
    background(40, 40, 50) 

    pushStyle()
    fontSize(30)
    fill(40,40,50) 
    popStyle()  
  
    if cc==3 then
        noFill()
        fontSize(30)
        --ellipse(400,500,100)
        sprite(asset.builtin.Cargo_Bot.Play_Button, 400,500)
        if showScore then
            fill(255,0,0)
            text(score,400,500)
        end
        
        if cc==3 and showScore==true then
               for z=1,cnt do   
    end 
            
        end
    end

    delay()
    strokeWidth(5)
    fill(128)
    for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)  
    end 

    for a,b in pairs(tab2) do
        ellipse(b.x, b.y, 50)
    end
    
    for a,b in pairs(tab7) do 
        ellipse(b.x, b.y, 100)  
    end 
    
    for z=1,cnt do
        pushMatrix()
        zLevel(.3)
        sprite(tab4[z].n,tab4[z].x2,tab4[z].y2,imageSize*tab4[z].s)  
        popMatrix()    
    end  
end

function delay()
    if cnt<#tab4 then
        loop=loop+1
        if loop>60 then 
            cnt=cnt+1
            loop=0
        end  
    end          
end

function touched(touch)    
    if touch.state==BEGAN then
        if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then
            showScore=true
        end
        pos=0
        for a,b in pairs(tab4) do
            if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and 
                        touch.y<b.y2+40 then
                b.s=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x2==d.x and b.y2==d.y then
                        d.z=0
                    end
                end
            end
        end
    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab4[pos].x2=tab4[pos].x2+touch.deltaX
        tab4[pos].y2=tab4[pos].y2+touch.deltaY
    end

    if touch.state==ENDED then  
        score=0
        cc=0
        showScore=false
        for a,b in pairs(tab1) do
            if tab4[pos].x2>b.x-55 and tab4[pos].x2<b.x+55 and 
                    tab4[pos].y2>b.y-55 and tab4[pos].y2<b.y+55 and
                    b.z==0 then
                tab4[pos].x2=b.x
                tab4[pos].y2=b.y
                b.z=1
                for c,d in pairs(tab4) do
                    if d.x1~=d.x2 and d.y1~=d.y2 then
                        score=score+d.v
                        cc=cc+1
                    end
                end            
                return
            end            
        end
        tab4[pos].x2=tab4[pos].x1
        tab4[pos].y2=tab4[pos].y1
        tab4[pos].s=.5
    end
end

@Jarc You can create a table that has all your sprites in it. On the first round, you pull 3 random sprites out. On the second round, pull out the rest of them in random order. As for displaying the scores, you just need to have a variable that you set. As the count goes down, you display the value. If it’s between a certain range you display it in one spot, the next range in another spot and the third spot for the remaining range.

@dave1707 How do I tell the code to stop after 3.

  for z=1,#t do
        a=math.random(#t)
        tab4[z].x1=tab2[t[a]].x
        tab4[z].y1=tab2[t[a]].y
        tab4[z].x2=tab2[t[a]].x
        tab4[z].y2=tab2[t[a]].y
        table.remove(t,a)
    end

@Jarc How many programs are you working on.

Since you’re adding more sprites, you need to change the way you randomly pick the sprites. The above routine isn’t going to work anymore. You still only pick 3 sprites from the full table. When a sprite is selected, remove it from the table. If you’re always picking 3 sprites at a time, the table should contain multiples of 3. Each time you pick a sprite, assign a small circle to it. I believe I mentioned somewhere that it’s easier to write code when you know in advance what you want to do, so you’re not always rewriting the code because you change something you already coded for.

@Jarc Heres an example of picking a random name from a table of 9 and assigning it 1 of 3 random small circles. This shows 3 rounds.

function setup()    
    names={"name1","name2","name3","name4","name5",
              "name6","name7","name8","name9"}
    circles={"circ1","circ2","circ3"}
    print("round 1========")
    pick()  
    
    print("round 2========")
    pick() 
    
    print("round 3========")
    pick()  
      
end

function pick()
    show={}
    temp={}
    for z=1,3 do    -- temp table of small circles positions
        table.insert(temp,circles[z])        
    end
    for z=1,3 do    -- pick random name and random circle
        a=math.random(#names)
        b=math.random(#temp)
        table.insert(show,{name=names[a],pos=temp[b]})  -- update show table
        table.remove(names,a)
        table.remove(temp,b)       
    end
    for a,b in pairs(show) do
        print(b.name,b.pos)
    end
end

@dave1707 Hey, I added a coroutine to this. Basically every round after 1 has a time limit or touch event that starts the next. For some reason I can’t get round 3’s time limit to work no matter what I try. Can u take a look?


function setup()
    
    co3 = coroutine.create(CoRoutineTest)
    parameter.watch("coroutine.status(co3)")
    
    co = coroutine.create(function()
            for i = 1, 3, 1 do
            print(i)
            print("round ========".. i)
            pick() 
            print(coroutine.status(co))
                if i == 1 then coroutine.yield()
            coroutine.resume(co3)
            end  
                if i == 2 then coroutine.yield()
                coroutine.yield(co3)
            end 
        end   
        if i == 3 then
        end
    end)
    
    print(coroutine.status(co))
       
    names={"name1","name2","name3","name4","name5",
              "name6","name7","name8","name9"}
    circles={"circ1","circ2","circ3"}
    
    coroutine.resume(co)   
    
end

function pick()
    show={}
    temp={}
    for z=1,3 do    -- temp table of small circles positions
        table.insert(temp,circles[z])        
    end
    for z=1,3 do    -- pick random name and random circle
        a=math.random(#names)
        b=math.random(#temp)
        table.insert(show,{name=names[a],pos=temp[b]})  -- update show table
        table.remove(names,a)
        table.remove(temp,b)       
    end
    for a,b in pairs(show) do
        print(b.name,b.pos)
    end
end

function touched(t)
    if t.state==BEGAN then
        coroutine.resume(co)
    end
end

function CoRoutineTest()
    print("time test started")
    local stime = os.clock()
    while os.clock() < stime + 5 do
        coroutine.yield(os.clock())
    end
    print("time test ended")
    coroutine.resume(co)
    return os.clock()
end


function draw()
    background(40,40,50)
    local th = coroutine.status(co3)
    if th ~= "dead" then
        local _,t =  coroutine.resume(co3)
        text("running at " .. t,WIDTH/2,HEIGHT/2)
    else
        text("ended",WIDTH/2,HEIGHT/2)
    end
end

@Jarc I don’t know why you’re using a coroutine for a timer. You can just use a simple timer. As far as as I know, a coroutine is mostly used when you have a routine that takes a long time to complete. You yield that routine frequently to allow the other code to run while the coroutine takes however long it needs to run.

@dave1707 I feel like a coroutine would give me more control over each round especially when I later add more code to it. Not sure how a simple timer could give me as much control and be less code.

@Jarc I don’t know what you want to do with the code you show, but here’s some of you code with a simple timer.

Code removed. Updated code below.

@dave1707 Thanks! This is a little of a backtrack but I was working on the visual for the game. Combining the two codes you helped me with. The problem I was having is the rest of the sprites loading in the far left corner. I fixed it by doing this

function delay()
    if cnt<#names-3 then

but not sure if that was the best way to fix it

Here’s the full code without the fix.


function setup()    
    names={"name1","name2","name3","name4","name5",
              "name6","name7","name8","name9"}
    circles={"circ1","circ2","circ3"}


    t={1,2,3}   -- number for each entry in tab4

    tab1={vec3(320,290,0),vec3(445,290,0),vec3(570,290,0)}
    tab2={vec2(835,335),vec2(835,265),vec2(835,195),}  
    tab4={  {n=asset.builtin.Planet_Cute.Character_Princess_Girl,
                v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Boy,
                v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Cat_Girl,
                v=12,x1=0,y1=0,x2=0,y2=0,s=.5}, 
            {n=asset.builtin.Planet_Cute.Gem_Green,
                v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Gem_Orange,
                v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Gem_Blue,
                v=12,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Star,
                v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Tree_Short,
                v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Heart,
                v=12,x1=0,y1=0,x2=0,y2=0,s=.5},}

    table.move(tab4,1,9,1,names)
    table.move(tab2,1,9,1,circles)
    print("round 1========")
    pick() 

    imageSize = spriteSize(names[1].n)
        
    for z=1,#t do
        a=math.random(#t)
        names[z].x1=circles[t[a]].x
        names[z].y1=circles[t[a]].y
        names[z].x2=circles[t[a]].x
        names[z].y2=circles[t[a]].y
        table.remove(t,a)        
    end
    cnt=0
    loop=0
end

function pick()
    show={}
    temp={}
    for z=1,#t do    -- temp table of small circles positions
        table.insert(temp,circles[z])        
    end
    for z=1,#t do    -- pick random name and random circle
        a=math.random(#names)
        b=math.random(#temp)
        table.insert(show,{name=names[a],pos=temp[b]})  -- update show table
        table.remove(names,a)
        table.remove(temp,b)  
        end
    for a,b in pairs(show) do
        print(b.name,b.pos)
    end
end

function draw()
    background(0)
    pushStyle()
    fontSize(30)
    fill(40,40,50) 
    popStyle()  

    fontSize(30)

    if cc==3 then
        noFill()
        ellipse(400,500,100)
        if showScore then
            fill(255,0,0)
            text(score,400,500)
        end
    end

    delay()
    strokeWidth(5)
    fill(128)
     for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)  
    end 

    for a,b in pairs(circles) do
        ellipse(b.x, b.y, 50)
    end

        for z=1,cnt do
        pushMatrix()
        zLevel(.3) 
        sprite(names[z].n,names[z].x2,names[z].y2,imageSize*names[z].s) 
        popMatrix()    
    end  
end


function delay()
    if cnt<#names then
        loop=loop+1
        if loop>60 then 
            cnt=cnt+1
            loop=0
        end         
    end
end

function touched(touch)    
    if touch.state==BEGAN then
        if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then
            showScore=true
        end
        pos=0
        for a,b in pairs(tab4) do
            if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and 
                        touch.y<b.y2+40 then
                b.s=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x2==d.x and b.y2==d.y then
                        d.z=0
                        end
                end
            end
        end
    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab4[pos].x2=tab4[pos].x2+touch.deltaX
        tab4[pos].y2=tab4[pos].y2+touch.deltaY
    end

    if touch.state==ENDED then  
        score=0
        cc=0
        showScore=false
        for a,b in pairs(tab1) do
            if tab4[pos].x2>b.x-55 and tab4[pos].x2<b.x+55 and 
                    tab4[pos].y2>b.y-55 and tab4[pos].y2<b.y+55 and
                    b.z==0 then
                tab4[pos].x2=b.x
                tab4[pos].y2=b.y
                b.z=1
                for c,d in pairs(tab4) do
                    if d.x1~=d.x2 and d.y1~=d.y2 then
                        score=score+d.v
                        cc=cc+1
                    end
                end            
                return
            end            
        end
        tab4[pos].x2=tab4[pos].x1
        tab4[pos].y2=tab4[pos].y1
        tab4[pos].s=.5
    end
end

@dave1707 basically that but can you add if the timer goes out the next round starts.

Here’s my version with coroutines but I’ll use yours. It does the same as yours but if the timer goes out the next round starts. (Btw the os clock in this doesn’t reset unless you close Codea :expressionless: )


function setup()

    r1 = coroutine.create(round1timer)
    parameter.watch("coroutine.status(r1)")
    
    r2 = coroutine.create(round2timer)
    parameter.watch("coroutine.status(r2)")

    co = coroutine.create(function()
            for i = 1, 3, 1 do
            print(i)
            print("round ========".. i)
            pick() 
            print(coroutine.status(co))
                if i == 1 then coroutine.yield()
            coroutine.resume(r1)
            end  
                if i == 2 then coroutine.yield()
                coroutine.yield(r1)
            end 
        end   
        if i == 3 then
        end
    end)

    print(coroutine.status(co))

    names={"name1","name2","name3","name4","name5",
              "name6","name7","name8","name9"}
    circles={"circ1","circ2","circ3"}

    coroutine.resume(co)   

end

function pick()
    show={}
    temp={}
    for z=1,3 do    -- temp table of small circles positions
        table.insert(temp,circles[z])        
    end
    for z=1,3 do    -- pick random name and random circle
        a=math.random(#names)
        b=math.random(#temp)
        table.insert(show,{name=names[a],pos=temp[b]})  -- update show table
        table.remove(names,a)
        table.remove(temp,b)       
    end
    for a,b in pairs(show) do
        print(b.name,b.pos)
    end
end

function touched(t)
    if t.state==BEGAN then
        coroutine.resume(co)
    round1 = false
    end
end

function round1timer()
    print("round 1 timer started")
    local stime = os.clock()
    while os.clock() < stime + 5 do
        coroutine.yield(os.clock())
    end
    print("round 1 timer ended")
    coroutine.resume(co)
    return os.clock()
end

function round2timer()
    print("round 2 timer started")
    local stime = os.clock()
    while os.clock() < stime + 5 do
        coroutine.yield(os.clock())
    end
    coroutine.resume(co)
    print("round 2 timer ended")
    coroutine.resume(co)
    return os.clock()
end

function draw()
    background(40,40,50)
    local th = coroutine.status(r1)
    if round1 == false then
        th = "dead"
    end
    if th ~= "dead" then
        local _,t =  coroutine.resume(r1)
        text("running at " .. t,WIDTH/2,HEIGHT/2)
    else
        text("round 1 ended",WIDTH/2,HEIGHT/2)
    end
    
    --local 
    th2 = coroutine.status(r2)
    if th == "dead" then
        local _,t =  coroutine.resume(r2)
        text("running at " .. t,WIDTH/2+300,HEIGHT/2)
    else
        text("round 2 ended",WIDTH/2+300,HEIGHT/2)
    end
end

@Jarc Heres the updated code.

displayMode(STANDARD)

function setup()
    fill(255)
    round=0
    names={"name1","name2","name3","name4","name5",
              "name6","name7","name8","name9"}
    circles={"circ1","circ2","circ3"}    
    s=require("socket")
end

function pick()
    st=s:gettime()
    show={}
    temp={}
    for z=1,3 do    -- temp table of small circles positions
        table.insert(temp,circles[z])        
    end
    for z=1,3 do    -- pick random name and random circle
        a=math.random(#names)
        b=math.random(#temp)
        table.insert(show,{name=names[a],pos=temp[b]})  -- update show table
        table.remove(names,a)
        table.remove(temp,b)       
    end
    round=round+1
    print("Round -----------  "..round)
    for a,b in pairs(show) do
        print(b.name,b.pos)
    end
end

function touched(t)
    if t.state==BEGAN and #names>0 and not done then
        pick()
    end
end

function draw()
    background(40,40,50)
    text("Tap screen to start.",WIDTH/2,HEIGHT-50)
    timer(10)
end

function timer(max)
    if st==nil then
        return
    end
    if #names>0 then
        curr=s:gettime()-st
        text(string.format("Time remaining  %4.1f",max-curr),WIDTH/2,HEIGHT/2)
        if curr>max then
            pick()
        end
    end    
end

@Jarc Sorry, but I don’t know what you’re trying to do. I don’t see any difference between adding your fix or leaving it out.

(Code updated)
@dave1707 Sorry, here’s a better example with timer included.

How do I

  1. make 3 sprites load every round from table, no duplicates. (Round 1 works)
    2)Get the delay function to work every round (I had to delete to get this code to work)
    3)Stop timer and next button from producing rounds after round 3
    4)Keep the value score and update it every round (each round it adds)
  2. make the sprites in the big circles stay every round.
function setup()  
    fill(255)
    round=0  
    names={"name1","name2","name3","name4","name5",
              "name6","name7","name8","name9"}
    circles={"circ1","circ2","circ3"}
    s=require("socket")

    buttonSize=vec2(500,600)

    t={1,2,3}   -- number for each entry in tab4

    tab1={vec3(320,290,0),vec3(445,290,0),vec3(570,290,0),vec3(380,190,0),vec3(510,190,0),vec3(445,90,0)}
    tab2={vec2(835,335),vec2(835,265),vec2(835,195),} 
    tab4={  {n=asset.builtin.Planet_Cute.Character_Princess_Girl,
                v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Boy,
                v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Cat_Girl,
                v=12,x1=0,y1=0,x2=0,y2=0,s=.5}, 
            {n=asset.builtin.Planet_Cute.Gem_Green,
                v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Gem_Orange,
                v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Gem_Blue,
                v=12,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Star,
                v=23,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Tree_Short,
                v=28,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Heart,
                v=12,x1=0,y1=0,x2=0,y2=0,s=.5},}

    table.move(tab4,1,9,1,names)
    table.move(tab2,1,9,1,circles)
    print("round 1========")
    pick() 


    imageSize = spriteSize(names[1].n)

    for z=1,#t do
        a=math.random(#t)
        names[z].x1=circles[t[a]].x
        names[z].y1=circles[t[a]].y
        names[z].x2=circles[t[a]].x
        names[z].y2=circles[t[a]].y
        table.remove(t,a)        
    end
    cnt=0
    loop=0
end

function pick()
    st=s:gettime()
    show={}
    temp={}
    for z=1,3 do    -- temp table of small circles positions
        table.insert(temp,circles[z])        
    end
    for z=1,3 do    -- pick random name and random circle
        a=math.random(#names)
        b=math.random(#temp)
        table.insert(show,{name=names[a],pos=temp[b]})  -- update show table
        table.remove(names,a)
        table.remove(temp,b)  
        end
    round=round+1
    print("Round -----------  "..round)
    for a,b in pairs(show) do
        print(b.name,b.pos)
    end
end

function draw()
    background(0)
    
    text("round 1", 200, 300)
    text("round 2", 250, 200)
    text("round 3", 320, 100)

      text("Tap next to start.",WIDTH/2,HEIGHT-50)
    timer(10)

    pushStyle()
    fontSize(30)
    fill(40,40,50) 
    popStyle()  

    fontSize(30)

    if cc==3 then
        noFill()
        ellipse(400,500,100)
        if showScore then
            fill(255,0,0)
            text(score,400,500)
        end
    end

    sprite(asset.builtin.Cargo_Bot.Next_Button,buttonSize.x,buttonSize.y)

    strokeWidth(5)
    fill(128)
     for a,b in pairs(tab1) do
        ellipse(b.x, b.y, 100)  
    end 

    for a,b in pairs(circles) do
        ellipse(b.x, b.y, 50)
    end

        for z=1,3 do
        pushMatrix()
        zLevel(.3) 
        sprite(names[z].n,names[z].x2,names[z].y2,imageSize*names[z].s) 
        popMatrix()    
    end  
end

function touched(touch)   

             if touch.state==BEGAN and
touch.x>buttonSize.x-75 and touch.x<buttonSize.x+75 and touch.y>buttonSize.y-50 and touch.y<buttonSize.y+50 and #names>0 and not done then
        pick()
    end

    if touch.state==BEGAN then
        if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then
            showScore=true
        end
        pos=0
        for a,b in pairs(tab4) do
            if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and 
                        touch.y<b.y2+40 then
                b.s=1   -- scaler
                pos=a
                for c,d in pairs(tab1) do
                    if b.x2==d.x and b.y2==d.y then
                        d.z=0
                        end
                end
            end
        end
    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        tab4[pos].x2=tab4[pos].x2+touch.deltaX
        tab4[pos].y2=tab4[pos].y2+touch.deltaY
    end

    if touch.state==ENDED then  
        score=0
        cc=0
        showScore=false
        for a,b in pairs(tab1) do
            if tab4[pos].x2>b.x-55 and tab4[pos].x2<b.x+55 and 
                    tab4[pos].y2>b.y-55 and tab4[pos].y2<b.y+55 and
                    b.z==0 then
                tab4[pos].x2=b.x
                tab4[pos].y2=b.y
                b.z=1
                for c,d in pairs(tab4) do
                    if d.x1~=d.x2 and d.y1~=d.y2 then
                        score=score+d.v
                        cc=cc+1
                    end
                end            
                return
            end            
        end
        tab4[pos].x2=tab4[pos].x1
        tab4[pos].y2=tab4[pos].y1
        tab4[pos].s=.5
    end
end

function timer(max)
    if st==nil then
        return
    end
    if #names>0 then
        curr=s:gettime()-st
        text(string.format("Time remaining  %4.1f",max-curr),WIDTH/2,HEIGHT/2)
        if curr>max then
            pick()
        end
    end   
end 

@Jarc Your code seems to change each time you post it. You’re last code had 3 large circles. This code has six large circles. A lot of what you want fixed was working at some point in previous code. You seem to be going in circles as you change things. I don’t know if you’re trying to add more things on the fly or if this was all planned up front.

@dave1707 It was a lot to ask of you, sorry. I figured most of it out but I have one question. I have the code set to when I load 4 sprites into the first row slots and press ready the score appears. My question is how can I get just the sprites in that 1st row to move into another table(when I press ready). I know about table.move but I’m not sure how to only pick those sprites.

function setup()
    
    score=0
    round=0 
    s=require("socket")
    
    readybuttonSize=vec2(500,500)

    buttonSize=vec2(500,600)
     
    slot = { vec2(835,335), vec2(835,265), vec2(835,195), vec2(900,335), vec2(900,265), vec2(900, 195) }
    
    gameslot = { vec3(320,290,0), vec3(445,290,0), vec3(570,290,0), vec3(695,290,0), vec3(380,180,0), vec3(505,180,0), vec3(630,180,0) }
    
    deck = {{n=asset.builtin.Planet_Cute.Character_Princess_Girl,
                v=5,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Boy,
                v=6,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Character_Cat_Girl,
                v=8,x1=0,y1=0,x2=0,y2=0,s=.5}, 
            {n=asset.builtin.Planet_Cute.Gem_Green,
                v=9,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Gem_Orange,
                v=10,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Gem_Blue,
                v=2,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Star,
                v=3,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Tree_Short,
                v=4,x1=0,y1=0,x2=0,y2=0,s=.5}, 
            {n=asset.builtin.Planet_Cute.Heart,
                v=5,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Chest_Closed,
                v=1,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Enemy_Bug,
                v=7,x1=0,y1=0,x2=0,y2=0,s=.5},
            {n=asset.builtin.Planet_Cute.Ramp_South,
                v=3,x1=0,y1=0,x2=0,y2=0,s=.5}}
            
            pick()
    
            imageSize = spriteSize(deck[1].n)
    
end

function pick()
    
    st=s:gettime()
    show={}
    temp={}
    
    for z=1,6 do    -- temp table of small circles positions
        table.insert(temp,slot[z])        
    end
    
    for z=1,6 do    -- pick random name and random circle
        a=math.random(#deck)
        b=math.random(#temp)
        table.insert(show,{name=deck[a],pos=temp[b]})  -- update show table
        table.remove(deck,a)
        table.remove(temp,b)  
    end
    
    round=round+1
    print("Round -----------  "..round)
    
    for k,v in pairs(show) do
        print(v.name,v.pos)   
    end

     for z=1,6 do
        show[z].x1=show[z].pos.x
        show[z].y1=show[z].pos.y
        show[z].x2=show[z].pos.x
        show[z].y2=show[z].pos.y
        show[z].v=show[z].name.v
        show[z].s=show[z].name.s
        show[z].n=show[z].name.n
    end
end

function draw()   
    background(0)     
    
    text("round 1", 200, 300)
    text("round 2", 250, 200)
    
    text("Tap next to start.",WIDTH/2,HEIGHT-50)
    timer(30)
    pushStyle()
    fontSize(30)
    fill(40,40,50) 
    popStyle()  
    fontSize(30)
    if cc==4 then 
        rectMode(CENTER)
        pushStyle()
        noFill()
        rect(readybuttonSize.x,readybuttonSize.y,100)
        popStyle()
        text("Ready",500,500)
        if sc==true then
            scoring()
        end
    end
    
    sprite(asset.builtin.Cargo_Bot.Next_Button,buttonSize.x,buttonSize.y)   
      
    fill(128)
    
    strokeWidth(5)

    for k,v in pairs(slot) do
        ellipse(v.x, v.y, 50)  
    end 

    for k,v in pairs(gameslot) do
        ellipse(v.x, v.y, 100)
    end
    
    for z=1,6 do
        pushMatrix()
        zLevel(.3) 
        sprite(show[z].n,show[z].x2,show[z].y2,imageSize*show[z].s)
        popMatrix()    
    end  
end

function touched(touch)    
    
        if touch.state==BEGAN and
touch.x>readybuttonSize.x-75 and touch.x<readybuttonSize.x+75 and touch.y>readybuttonSize.y-50 and touch.y<readybuttonSize.y+50 and cc==4 then
        sc=true
        end
    
        if touch.state==BEGAN and
touch.x>buttonSize.x-75 and touch.x<buttonSize.x+75 and touch.y>buttonSize.y-50 and touch.y<buttonSize.y+50         and #deck>0 and not done then
        pick()
        end
    
    if touch.state==BEGAN then
        if touch.x>400-50 and touch.x<400+50 and touch.y>500-50 and touch.y<500+50 then
            showScore=true
        end
        pos=0
        for a,b in pairs(show) do
     
            if touch.x>b.x2-40 and touch.x<b.x2+40 and touch.y>b.y2-40 and 
                        touch.y<b.y2+40 then
                b.s=1   -- scaler
                pos=a
                for c,d in pairs(gameslot) do
                    if b.x2==d.x and b.y2==d.y then
                        d.z=0
                    end
                end
            end
        end
    end

    if pos==0 then 
        return
    end

    if touch.state==MOVING then
        show[pos].x2=show[pos].x2+touch.deltaX
        show[pos].y2=show[pos].y2+touch.deltaY
    end

    if touch.state==ENDED then  
        --score=0
        cc=0
        showScore=false
        for a,b in pairs(gameslot) do
            if show[pos].x2>b.x-55 and show[pos].x2<b.x+55 and 
                    show[pos].y2>b.y-55 and show[pos].y2<b.y+55 and
                    b.z==0 then
                show[pos].x2=b.x
                show[pos].y2=b.y
                b.z=1
                for c,d in pairs(show) do
                    if d.x1~=d.x2 and d.y1~=d.y2 then
                        score=score+d.v
                        cc=cc+1
                    end
                end            
                return
            end            
        end
        show[pos].x2=show[pos].x1
        show[pos].y2=show[pos].y1
        show[pos].s=.5
    end
end
    
function timer(max)
    if st==nil then
        return
    end
    if #deck>0 then
        curr=s:gettime()-st
        text(string.format("Time remaining  %4.1f",max-curr),WIDTH/2,HEIGHT/2)
        if curr>max then
            pick()
        end
    end
end

function scoring()
    text(score,150,500)
end