Codea Project

Hello everyone,

You probably don’t remember me but I bought codea with no programming experience and tried to make a fruit machine game (which I did but with a ton of help from you guys!)

Anyway, I’ve not even loaded Codea in well over a month I think, but all of a sudden I have an idea for a project and would like your honest opinions.

I’m not really a programmer and I never will be, I dabble a tiny bit and that’s it. The game I want to do now is Video Poker. Yes, I know what you are saying, there are loads of them on the App Store but after paying for 4 of them and trying a dozen free ones, I cannot find one that matches what I want in such a game.

I’m not really sure if this is an easy task or a hard one, or even if another ipad programming app would be more suited to the game as codea is more for action games?

Anyway, I’d just like your opinion on the task ahead and my chances of success. And no, I don’t really want to devote weeks and weeks to doing it. Please don’t be biased in your opinion, if another app would be better for programming a poker game, please say so.

Thank you all.

You should take a look at this:
http://coolcodea.wordpress.com/2013/04/22/39-working-with-playing-cards/
Where ignatz proofs that you can make card games with codea, and it might help you with your project.

@Tyson whatever game you want to do, if you want to publish it on the app store, it will pby takes several weeks of your time.

@Jmv38 it’s only for my personal use, I don’t have an apple computer and will never buy one so publishing is not an option.

@Tyson, if you are patient and enjoy the process of getting it to work just as you want it, then go right ahead. Many of the people in this forum are not professionals (myself included), and we struggle just like you do. So I think you will be able to do it.

@G_nex @ignatz thanks for the link to Ignatz cards code, I’ve studied it over the last day or so and although I did learn a huge amount about programming and codea making my fruit machine, I think this is just going to be over my head. His code is incredibly complex for me to understand. (I don’t mean to complain about him or his code, I’m sure it’s ideal. It’s just way above my head)

I think I’ll save myself lots of frustration by abandoning this project. Thanks for the link anyway :slight_smile:

I’ve been very bored recently and was tinkering with codea and getting it to randomly pull cards from a pack of 52. I originally tried to be lazy and modify Ignatz code in the link above, to just pull 5 cards out and couldn’t do it. So I have my own very rough code that gives 5 cards but I’m not sure how to stop it pulling the same card out twice?

I’ve tried lots of if statements but it’s not working that good, is there a simple way to stop generating the same random number when using ‘card=math.random(1,52)’?

@Tyson Without seeing what you’ve done, my suggestion is to create a table. When you pick a random number, see if that table entry is false. If it is, set it to true and pick you’re next card. If it’s true, then that card was picked already, so pick another. Keep doing that until all the cards are picked.

@dave1707 I’m not really up on tables as I find them confusing but I will try to understand them and do what you suggest.

@Tyson Here’s a shuffle program I had. Maybe this can help a little. It prints 10 hands of 5 cards.


function setup()
    suit={"Diamonds","Hearts","Spades","Clubs"}
    value={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"}
    
    Shuffled=shuffleCards(5)    -- shuffle cards x number of times
    
    -- print 10 hands of cards from the shuffled deck
    card=0
    for x=1,10 do
        for y=1,5 do
            card=card+1
            s=math.ceil(Shuffled[card]/13)   -- get suit value
            v=Shuffled[card]%13+1    -- get card value
            print(value[v].." of "..suit[s])    -- print card
        end
        print()
    end
end

function shuffleCards(x)
    local d1, d2, s, y, z = {}, {}
    for z=1,52 do
        d2[z]=z     -- fill table with numbers 1 to 52
    end      
    for y=1,x do    -- shuffle x number of times
        d1,d2=d2,{}
        for z=1,52 do
            s=math.random(1,#d1)    -- get a random number from table d1
            table.insert(d2,d1[s])    -- insert it in table d2
            table.remove(d1,s)    -- remove it from table d1
        end
    end
    return d2    -- return shuffled deck
end

Thank you!

@Tyson Will this help you any.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    suit={"Diamonds","Hearts","Spades","Clubs"}
    value={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"}
    Shuffled=shuffleCards(5)
    r=0
end

function draw()
    background(230, 228, 219, 255)
    fill(45, 127, 37, 255)
    if r==5 then
        text("double tap to shuffle",WIDTH/2,440)
    else
        text("tap screen to deal",WIDTH/2,440)  
    end
    card=0
    fill(0, 5, 255, 255)
    text("Player 1",WIDTH/2,HEIGHT-20)
    text("Player 2",WIDTH-100,500)
    text("Player 3",WIDTH/2,200)
    text("Player 4",100,500)
    for round=1,r do
        for player=1,4 do
            card=card+1
            s=math.ceil(Shuffled[card]/13)   -- get suit value
            v=Shuffled[card]%13+1    -- get card value
            str=value[v].." of "..suit[s]   -- print card
            fill(0)
            if s<3 then
                fill(255,0,0)
            end
            if player==1 then
                text(str,WIDTH/2,HEIGHT-20-20*round)
            end  
            if player==2 then
                text(str,WIDTH-100,500-20*round)
            end  
            if player==3 then
                text(str,WIDTH/2,200-20*round)
            end  
            if player==4 then
                text(str,100,500-20*round)
            end            
        end       
    end
    fill(0)
    rect(0,0,WIDTH,50)
end

function touched(t)
    if t.state==BEGAN then
        if t.tapCount==2 and r==5 then
            Shuffled=shuffleCards(5)
            r=0
            return
        end
        r=r+1
        if r>5 then
            r=5
        end
    end
end

function shuffleCards(x)
    local d1, d2, s, y, z = {}, {}
    for z=1,52 do
        d2[z]=z     -- fill table with numbers 1 to 52
    end      
    for y=1,x do    -- shuffle x number of times
        d1,d2=d2,{}
        for z=1,52 do
            s=math.random(1,#d1)    -- get a random number from table d1
            table.insert(d2,d1[s])    -- insert it in table d2
            table.remove(d1,s)    -- remove it from table d1
        end
    end
    return d2    -- return shuffled deck
end

That’s cool, thanks I can use some of that :slight_smile:

I’ve been wondering how many 'if" statements it would take to slow down codea?
Because it’s probably going to take several hundred of them to check for all the winning card combinations as the cards can be in any order. Would so many if statements cause an issue?

There are ways to not use as many if statements and you could check only a few combinations every frame

@Coder I’m not sure I understand how that would work.

My game would be single player video poker, you hit deal and get 5 cards then you get a chance to keep some and draw other cards and then it has to check several hundreds of combinations for the wins before starting a new hand.

I suggest you pick another game or this’ll take you forever to code as you need thousands of if statement checks.

a quick google search shows theres 2,860 possible pair variants, 858 two pair variants, 858 three of a kind, 156 full house etc

theres gonna be over 2 million possible combinations of the 5 cards you draw.
Do you really wanna code 2 MILLION IF STATEMENTS? lol, no me neither :stuck_out_tongue:

Really o_O

Okay, thanks for that. :frowning:

Applying a brute force check against a library of all the possible combinations is obviously not the way to go!

A few pointers:

  1. order your 5 cards low to high to start with - this is going to reduce the combinations and will make it easier to check the sets and runs. (K, 3, 7, Q, J is the same hand as 3, 7, L, Q, K)

  2. assign values to J=11, Q=12, K=13 as this will make checking runs easier.

  3. to check to see if you have a run: if (card2_value - card1_value) +(card3_value - card2_value)+(card4_value - card3_value)+(card5_value - card4_value)==4 then you have a run (a run counts up in steps of 1 and there are 4 steps between 5 cards). Also store the card5_value to check later for a royal flush

  4. to check if you have a flush: if (card1_suit==card2_suit) and (card3_suit==card4_suit) and (card4_suit==card5_suit) and (card1_suit==card3_suit) then you have a flush

  5. if you have 3 and 4 then you have a straight flush

  6. if you have 5 and the top card is an King/ace (I’m not sure if ace is high and you may need to factor this into your card definition values) then its a royal flush

  7. Check for 4 of a kind - two rules:
    if (card1_value==card2_value) and (card2_value==card3_value) and (card3_value==card4_value) then 4 of a kind

if (card2_value==card3_value) and (card3_value==card4_value) and (card4_value==card5_value) then 4 of a kind

And similar types of statements for 3 of a kind, full house and 2 pair

Don’t be worried about having quite a few checks in your draw loop - these checks are done very very quickly

@West That’s really impressive!! But it’s so far over my head it’s in the clouds. Yeah, I’m not very smart. But it does sound really great, I couldn’t do it though as I simply don’t grasp it. My fault not yours but thanks for your help.

@Tyson Here’s a single hand of 5 card poker. It goes thru one deck (5 cards, 10 hands) before it reshuffles the deck. Tap the screen for a new hand. It also shows you the result of each hand which could be “High Card”, “1 Pair”, “2 Pair”, “3 of a Kind”, “Straight”, “Flush”, “Full House”, “4 of a Kind”, “Straight Flush”, “Royal Flush”. If you look at the function check(), you’ll see that it doesn’t require 2 million “if” statements as mentioned above, just 19 and a table to determine the combination of the hand. I suggest that you learn about tables. They’re very important and can save you tons of code. This probable doesn’t do exactly what you want, but I figured you would want the check() function to determine the combination of the hand and this code shows it’s use. Look over the code and if you have any questions let me know. I’ll go into more detail on any section you request.


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={" 2"," 3"," 4"," 5"," 6"," 7"," 8",
            " 9","10","Jack","Queen","King","Ace"}  
    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])
                text(card,370,HEIGHT-250-cnt*40)
            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