Codea Project

@Tyson, Code stores 52 cards in the Document folder.

-- Card set 52 to Documents Folder
-- Use this function to perform your initial setup
function setup()
    x=0
    imgC={}
    imgS={}
    imgH={}
    imgD={}
    for i=1,13 do
    imgC[i]=http.request("https://dl.dropboxusercontent.com/u/19857700/C"..i..".png",
    function(data,status,headers)imgC[i]=data end)    
    imgS[i]=http.request("https://dl.dropboxusercontent.com/u/19857700/S"..i..".png",
    function(data,status,headers)imgS[i]=data end)    
    imgH[i]=http.request("https://dl.dropboxusercontent.com/u/19857700/H"..i..".png",
    function(data,status,headers)imgH[i]=data end)    
    imgD[i]=http.request("https://dl.dropboxusercontent.com/u/19857700/D"..i..".png",
    function(data,status,headers)imgD[i]=data end)    
    end
end
-- This function gets called once every frame
function draw()
    
 background(0)
    for i = 1,13 do
        x=x+50
        if imgC[i] ~= nil then sprite(imgC[i],x,500) end
        if imgC[i] ~= nil then saveImage("Documents:C"..i,imgC[i]) end
        if imgS[i] ~= nil then sprite(imgS[i],x,400) end
        if imgS[i] ~= nil then saveImage("Documents:S"..i,imgS[i]) end
        if imgH[i] ~= nil then sprite(imgH[i],x,300) end
        if imgH[i] ~= nil then saveImage("Documents:H"..i,imgH[i]) end
        if imgD[i] ~= nil then sprite(imgD[i],x,200) end
        if imgD[i] ~= nil then saveImage("Documents:D"..i,imgD[i]) end
        end
        x=0 
end

@Tyson, This code is from @dave1707 and has been modified by me with the
display of cards.

--Cardset 52 example
supportedOrientations(LANDSCAPE_ANY)
--displayMode(FULLSCREEN)
font("Courier-Bold")
fontSize(30)
function setup()
    str=""  -- matches
    hand={} -- table of current hand
    suit={"Diamonds","Hearts","Spades","Clubs"}
    value={"1","2","3","4","5","6","7","8",
            "9","10","11","12","13"}  
    setup1() 
end
function setup1()
    shuf="New deck"
    sc=shuffleCards()   -- shuffle cards
    r=0 -- card offset
    setTable({sc[r+1],sc[r+2],sc[r+3],sc[r+4],sc[r+5]})
    check() -- check current hand
end
function draw()
    background(40,40,50)
    fill(255)
    text(shuf,WIDTH/2,HEIGHT-50)
    text("Tap screen for next hand",WIDTH/2,HEIGHT-100)
    show()  -- show the current hand
end
function nextHand()
    r=r+5
    if r>49 then    -- new deck
        setup1()
    else
        setTable({sc[r+1],sc[r+2],sc[r+3],sc[r+4],sc[r+5]})
        check()
    end  
end
function touched(t)
    shuf=""
    if t.state==ENDED then
        nextHand()
    end
end
function setTable(cards)
    -- set table values to 0
    for x=1,13 do
        hand[x]={}
        for y=1,4 do        
            hand[x][y]=0
        end
    end
    -- update table with cards
    for x=1,5 do
        local s=math.floor((cards[x]-1)/13)+1
        local v=(cards[x]-1)%13+1
        hand[v][s]=1
    end
end
function show()
    pushStyle()
    textMode(CORNER)
    local cnt=0
    for x=13,1,-1 do
        for y=1,4 do
            if hand[x][y]==1 then
                cnt=cnt+1
                local card=string.format("%5s of %s",value[x],suit[y])
                lineSize=0
                local v=string.sub(suit[y],1,1)
                local img="Documents:"..v..value[x]
                text(card,370,HEIGHT-250-cnt*50)
                sprite(img,300,HEIGHT-150-cnt*80,100,150)
                print(card," ",img," ",v)
            end
        end
    end
    popStyle()
    text(str,WIDTH/2,HEIGHT-600)
end
function check()    -- determine ranking of current hand
    -- clear flags
    local same2,same3,same4=0,0,0
    local flush,straight,royal=0,0,0
    local v2,v3,v4
    -- check for pairs, 3 of a kind, 4 of a kind, full house
    for x=1,13 do
        v2=hand[x][1]+hand[x][2]+hand[x][3]+hand[x][4]
        v3=hand[x][1]+hand[x][2]+hand[x][3]+hand[x][4]
        v4=hand[x][1]+hand[x][2]+hand[x][3]+hand[x][4]
        if v2==2 then
            same2=same2+1
        end
        if v3==3 then
            same3=1
        end
        if v4==4 then
            same4=1
        end
    end
    -- check for flush
    local s1,s2,s3,s4=0,0,0,0
    for x=1,13 do
        s1=s1+hand[x][1]
        s2=s2+hand[x][2]
        s3=s3+hand[x][3]
        s4=s4+hand[x][4]
    end
    if s1==5 or s2==5 or s3==5 or s4==5 then
        flush=1
    end
    -- check for straight
    local sum=0
    local x=13    -- start with Ace for low straight
    if hand[x][1]==1 or hand[x][2]==1 or hand[x][3]==1 or hand[x][4]==1 then
        sum=sum+1
    end
    for x=1,13 do
        if hand[x][1]==1 or hand[x][2]==1 or hand[x][3]==1 or hand[x][4]==1 then
            if sum==0 and x==9 then
                royal=1 -- possible royal 10,J,Q,K,A
            end
            sum=sum+1
            if sum==5 and x==4 then
                straight=1  -- Ace,2,3,4,5
            end
        else
            if sum<5 then   -- out of sequence
                sum=0
            end
            royal=0 -- clear royal
        end
    end
    if sum==5 then
        straight=1
    end
    -- update str with results of hand
    str="High card"
    if same2==1 then
        str="1 Pair"
    end
    if same2==2 then
        str="2 Pair"
    end
    if same3==1 then
        str="3 of a kind"
    end
    if straight==1 then
        str="Straight"
    end
    if flush==1 then
        str="Flush"
    end
    if same2==1 and same3==1 then
        str="Full House"
    end
    if same4==1 then
        str="4 of a kind"
    end
    if flush==1 and straight==1 then
        str="Straight Flush"
    end    
    if flush==1 and royal==1 then
        str="Royal Flush"
    end
end
function shuffleCards()
    local d1, d2 = {}, {}
    for z=1,52 do
        d1[z]=z     -- fill table with numbers 1 to 52
    end      
    for z=1,52 do
        local r=math.random(1,#d1)    -- get a random number from table d1
        table.insert(d2,d1[r])    -- insert it in table d2
        table.remove(d1,r)    -- remove it from table d1
    end
    return d2    -- return shuffled deck
end

@matox @dave1707

That’s really awesome!! Wow :slight_smile:
(Sorry I was slow in replying, I’ve been away)

I’ve been looking at your code and there are parts that I simply can’t comprehend even taking it a line at a time, but that’s my dumbness :slight_smile:

Anyway… I’m wondering, how hard would it be to add jokers as wildcards to the pack? I’ve been toying with it but obviously because I don’t totally understand the code, it’s hard or impossible to change without it being kind of hit and miss ;). or would the code have to be totally rewritten?

Anyway just wanted to say thank you to you guys for you hard work and great help.

@Tyson There would be a lot of changes, but the code wouldn’t have to be totally rewritten. Just about every function would need to be updated. Probably the hardest change would be to the function “check” to determine winning hands.

@dave1707. Thanks for that, I thought it might need massive changes. :slight_smile: