Card Game revised (one sprite to download)

Here’s an updated version of the code I wrote earlier. I removed it to save space here. In this version, I added an option using a parameter slider that allows left only moves or the default left and right 1 or 3 position moves. I also added code to enter your name or initials for a high score, in this game, a low score. It will show the lowest 10 scores with name/initials. When the game ends (you can’t make any more moves), tap the Save score button. I leave it up to you to determine when you can’t move anymore. Tap the New game button to enter a new name/initials or continue with the current name. I’ve managed to get a low score of 1, the lowest, but most of the time I’m well above 10. See my farther above post for more instructions.

displayMode(FULLSCREEN)

function setup()
    --clearProjectData()
    showKeyboard()
    parameter.boolean("justLeft",false)
    parameter.text("initials","")
    rectMode(CENTER)
    gameSetup()
end

function draw()
    background(229, 189, 97)
    if getInitials then
        initialMessage()
    elseif showScoreFlag then
        scoreDraw()
    else       
        gameDraw()
    end
end

function touched(t)
    if t.state==BEGAN then
        dealCards(t)
        selectCards(t)
        scoreButtonPressed(t)
        newGameButtonPressed(t)
    end    
end

function keyboard(k)
    if k==BACKSPACE then
        initials=string.sub(initials,1,#initials-1)
    elseif k==RETURN then
        if initials~="" then
            hideKeyboard()
            getInitials=false
        end
    else
        initials=initials..string.upper(k)
    end
end

function initialMessage()
    pushStyle()
    fill(255)
    fontSize(30)
    text("Enter name or initials, then press return.",WIDTH/2,HEIGHT*.9)
    text("Or press return for same person.",WIDTH/2,HEIGHT*.9-50)
    text(initials,WIDTH/2,HEIGHT*.9-150)
    popStyle()
end

function showScoreButton()
    fill(255)
    rect(WIDTH/2,60,180,60)
    fill(0)
    text("Save score",WIDTH/2,60) 
end

function scoreButtonPressed(t)
    if t.x>WIDTH/2-90 and t.x<WIDTH/2+90 and
            t.y>60-30 and t.y<60+30 then
        scoreSave()
        showScoreFlag=true
    end    
end

function scoreSave()
    local str=string.format("%03d",scoreTotal)
    saveProjectData(str,initials)
end

function scoreDraw()
    pushStyle()
    textMode(CORNER)
    fontSize(30)
    font("Courier-Bold")
    local z=listProjectData()
    table.sort(z)
    for a,b in pairs(z) do
        local v=readProjectData(b)
        if a>10 then
            saveProjectData(b,nil)
        else
            str=string.format("%2d)   %3d...%s",a,b,v)
            text(str,WIDTH*.35,HEIGHT-a*50)
        end
    end 
    popStyle()  
    newGameButton()
end

function gameSetup()
    play={}
    pos=0
    cardsTotal=52
    select=0
    scoreTotal=364
    shuffleCards()
    scoreTab={}
    showScoreFlag=false  
    getInitials=true  
end

function gameDraw()
    fill(255)
    fontSize(30)
    text("Player  "..initials,WIDTH/2,HEIGHT-200)
    text("Cards remaining  "..cardsTotal,WIDTH/2,HEIGHT-100)
    text(string.format("Score  %d",scoreTotal),WIDTH/2,HEIGHT-150)
    if justLeft then
        text("Move Just Left, 1 or 3",WIDTH/2,HEIGHT-50)
    else
        text("Move Left or Right, 1 or 3",WIDTH/2,HEIGHT-50)
    end
    rect(WIDTH/2,HEIGHT/2-200,120,80)
    fill(0)
    text("Deal",WIDTH/2,HEIGHT/2-200) 
    if select>0 then
        fill(0, 142, 255)
        rect(select*65,HEIGHT/2,60,90)
    end
    showScoreButton()
    showCards() 
end

function dealCards(t)
    if t.x>WIDTH/2-60 and t.x<WIDTH/2+60 and
            t.y>HEIGHT/2-240 and t.y<HEIGHT/2-160 then
        if #play<16 and cardsTotal>0 then
            pos=pos+1
            local z=shuffled[pos]-1
            local s=z//13+1
            local v=z%13+1
            table.insert(play,vec2(s,v))
            cardsTotal=cardsTotal-1
            card1=vec2(0,0)
            card2=vec2(0,0)
            select=0
        end
    end   
end

function selectCards(t)
    local diff
    if t.y>HEIGHT/2-40 and t.y<HEIGHT/2+40 then
        for z=1,#play do
            if t.x>z*65-25 and t.x<z*65+25 then
                if card1.x==0 then
                    card1=vec3(play[z].x,play[z].y,z)
                    select=z
                else
                    card2=vec3(play[z].x,play[z].y,z)  
                    if justLeft then                      
                        diff=card1.z-card2.z
                    else
                        diff=math.abs(card1.z-card2.z)
                    end
                    if diff==1 or diff==3 then
                        if card2.x==card1.x or card2.y==card1.y then
                            play[card2.z]=play[card1.z]
                            scoreTotal=scoreTotal-card2.y
                            table.remove(play,card1.z)                               
                        end
                    end
                    card1,card2=vec3(0,0,0),vec3(0,0,0)
                    select=0
                end
            end
        end
    end
end

function newGameButton()
    fill(255)
    rect(WIDTH-100,60,180,60)
    fill(0)
    text("New game",WIDTH-100,60) 
end

function newGameButtonPressed(t)
    if t.x>WIDTH-100-90 and t.x<WIDTH-100+90 and
            t.y>60-30 and t.y<60+30 then
        showKeyboard()
        gameSetup()    
    end   
end


function showCards()
    fontSize(20)
    for a,b in pairs(play) do
        local s=b.x
        local v=b.y
        fill(255)
        rect(a*65,HEIGHT/2,50,80)
        fill(0)
        if s==1 or s==3 then
            fill(255,0,0)
        end
        text(value[v],a*65-13,HEIGHT/2+25)
        text(value[v],a*65+13,HEIGHT/2-25)
        text(suit[s],a*65,HEIGHT/2)
    end
end

function shuffleCards()
    value={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}
    suit={"??","??","??","??"} 
    shuffled={} 
    local temp={}
    for z=1,52 do
        table.insert(temp,z)        
    end
    for z=1,52 do
        r=math.random(#temp)
        table.insert(shuffled,temp[r])
        table.remove(temp,r) 
    end
end