Howdy, Stranger!

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

Help needed to de-bug card game

edited November 28 in Code Sharing Posts: 83

Card geme : Acey-Ducey, stuck on line 153, repeat is wrong word,
Can anyone help ? Heres the code below :smile:

--Acey-Deucy

function showIntro() 
print --[[
ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER
THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP
YOU HAVE AN OPTION TO BET OR NOT BET DEPENDING
ON WHETHER OR NOT YOU FEEL THE CARD WILL HAVE
A VALUE BETWEEN THE FIRST TWO.
IF YOU DO NOT WANT TO BET, ENTER A $0 BET
]]--
end

-- Initialises the Global BankBalance and Random Number Generator
function initGame()
  bankBalance = 100
    math.randomseed( os.time() )
end

-- shows the card type
function showCard(val)
  local cards={ nil, 2, 3, 4, 5, 6, 7, 8, 9, 10, "JACK", "QUEEN", "KING", "ACE"} 
    return cards[val]
  end

-- Displays the current bank balance (global)
function showBalance() 
  print("You Now Have "..bankBalance.." Dollars.")
end

-- Adjusts the global balance
function adjustBalance( amount )
  bankBalance = bankBalance + amount
    showBalance()
end  

-- Generates a random card
function dealCard()
  local nextCardValue = math.random(2,14)
    return nextCardValue
end

-- Deals cards returns a collection of two
function dealCards()
 print("HERE ARE THE NEXT TWO CARDS:")
   local cardValues = {first=dealCard(),second=dealCard()}
     print( "First Card:" .. showCard(cardValues.first) )
       print( "Second Card:" .. showCard(cardValues.second) )  
return cardValues
end

-- Inputs a bet value
function getBet()
  betOK = false
    local betAmount = 0
  -- Loop to get a valid bet or a zero bet
  repeat
  io.write("What is Your Bet ? :");
    local amount = io.read();
      betAmount = tonumber(amount)
  if betAmount==0 then
    print("Chicken!!")
      betOK = true
  end
  if betAmount <= bankBalance then
      betOK = true
    else
      print("Sorry, but you bet too much.")
      print("You have only " .. bankBalance .. " Dollars to bet.")
  end
   until betOK
    return betAmount
end

-- Deals another card and compares values
function playRound( cardValues )
  local playerCard = dealCard()
    local firstCard = cardValues.first 
      local secondCard = cardValues.second 
  print( "Dealer Card" .. showCard(playerCard) )  
    local minValue = math.min( firstCard, secondCard )
      local maxValue = math.max( firstCard, secondCard )
  if playerCard>=minValue and playerCard <=maxValue then
    print("You Win!!!")
    return true
  end
  print("You Lose!!")
  return false
end


function endGameReplay()
  print ("Sorry, but you blue your wad.") 
    io.write("Try Again (Y/N)")
      response = io.read()
  if repsonse == "Y" then
    return true
  else
    print("O.K., Hope you had fun !")  
    return false
  end
end

-- Main Loop ; Here's when the action happens
showIntro()
done = false      -- when true the game loop ends
initGame()
-- Game Loop
repeat
    local cardValues = dealCards()
    local betAmount = getBet()  
    if betAmount == 0 then
      done=true      -- user said they don't want to play anymore
    else
      if playRound(cardValues) then
        adjustBalance( betAmount )
      else
        adjustBalance( -betAmount )    
        -- if no more money then ask to start again
        if bankBalance <= 0 then
          if endGameReplay()==true then
            -- Start again
            initGame() -- reset variables
          end
          done=true
          end
      end
    end
Tagged:

Comments

  • edited July 21 Posts: 140

    "repeat" isn't a Lua keyword. You can use while(condition) .... end for a loop loop. If condition is false, it will skip running the loop and move on. If condition is always true, it will run until it his a break.

  • dave1707dave1707 Mod
    Posts: 6,397

    @kendog400 Anytime you post code, put 3 ~'s on a line before and after the code so it shows correctly.

  • edited November 28 Posts: 83

    ...

  • edited November 28 Posts: 83

    ...

  • dave1707dave1707 Mod
    Posts: 6,397

    @kendog400 Where did you get the code from. It looks like you copied it from somewhere and you think it's going to work in Codea. There's a lot of things in the above code that isn't going to work.

  • dave1707dave1707 Mod
    Posts: 6,397

    @syntonica repeat is valid. Try this code.

    function setup()
        a=0
    
        repeat
            a=a+1
            print(a)
        until a>15
    
    end
    
  • edited July 22 Posts: 140

    Ack! Sorry about that to the OP. Repeat/until will run at least once while a while may never run.

    I never use repeat/until since I never need it. :blush:

  • Posts: 83

    Yes, I copied it from a site on-line, I would like to program Card Games, so it is part of a study until I get my roll on...

  • Posts: 83

    The card Game was written LUA, but I see now that, it doesent mean it would work in Codea....In any event Thanks for the Help !

  • dave1707dave1707 Mod
    Posts: 6,397

    @kendog400 Do a forum search for cards games. You might come up with some interesting things.

  • dave1707dave1707 Mod
    Posts: 6,397

    @kendog400 If you want to play with card games, maybe this would help some.

    function setup()   
        rectMode(CENTER)
        suit={"♠️","♣️","♥️","♦️"}
        val={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}    
    end
    
    function draw()
        background(40, 40, 50)
        cnt=0
        for x=1,4 do
            for y=1,13 do
                cnt=cnt+1
                card(x*100,y*70,cnt,1)
            end
        end
    end
    
    function card(x,y,v,sc)      -- x,y position    Card value 1 to 52   sc=size of card
        scale(sc)
        local c=(v-1)%13
        local s=(v-1)//13+1
        fill(255)
        stroke(0, 255, 182, 255)
        strokeWidth(3)
        rect(x,y,45,60)
        fill(255,0,0)
        if s<3 then
            fill(0)
        end
        fontSize(25)
        text(val[c+1],x,y)
        fontSize(14)
        text(suit[s],x-10,y+18) 
        text(suit[s],x+10,y-18) 
        scale(1/sc)
    end
    
  • edited November 28 Posts: 83

    It looks like this was designed for a Lua compiler running on windows platform and was not programed for Codea.

  • edited October 27 Posts: 102

    Ok, first off, i looked at the original page you got this code from. You're missing this line until doneat the end of the script. happened to me when i copied it too.

    second, it still won't run - not toally sure why, but i think (just a theory) that Codea won't allow you to run a script if there's a repeat or while loop that hasn't finished running. when you do try, it disables the play button and the extra features on the keynoard as well, so maybe a bug there

    so replace the repeat, until with function draw() if not done then --[[ the code already there ]] else return end end

  • edited October 27 Posts: 102

    ok, so i did that, now im having issues adapting the io read write etc. the real trick is going to be getting io.read to work.

    local amount = io.read();

    See, what i want is for io.read to bring up the keyboard, but the repeat loop keeps going. so... ill need to rewritr everything >:(

    edit: i give up. this is too hard to convert into codea

  • edited November 28 Posts: 83

    I would like to see more youtube type tutorials for codea. Programming isn't for the faint at heart and it takes a lot of brain power.. 3d max, cinema 4d, zbrush, photoshop all have large followings, and a large amount of youtube tutorials. I would like to see codea with this type of enviorment.

  • edited October 27 Posts: 102

    @kendog400 I never liked video tutorials myself. for something like photoshop or krita i can see why it is useful, but when programming i really prefer to have a reference guide in another phone or book while i code. Actually,learning it myself, I still have't gone into the new Craft stuff yet or even figured ot physics - busy learning everything else.

    Hm.. Checklist of what you know? This stuff is lua that I think would be nice to know

    1. What 'Dynamically typed' means. >! Lua is dynamically typed, which means that you can say t={}; and then later say t='string'. Variables aren't locked to their type. !<
    2. Given t={a=false,b=false}, set every false value to true using a for loop
    3. How to use a while condition ... do loop
    4. What conditions evaluate as true, and what are false. >! Any value that isn't explicityly false or nil is true. An empty string or tsble evaluates as true.
    5. A repeat .. until loop >! Will always run at least once, it runs first, then checks if condition is true or false, whereas a while loop checks condition first, and so might never run.
    6. What tail calls are. >! Sorry, you're on your own here.
    7. A recursive function >! basically a function that calls itself. e.g. function f(x) return x<10 and f(x+1) or x end print(f(1));
    8. Short circuit logic. VERY useful thing I didn't know until recently. i.e. >! x=1; x = x==2 and 3 or 0; So this is the same as if x==2 then x = 3; else x = 0; endbut easier to write. if you didn't include the 'or 0' then it would be by default false', because the or part only runs if the 'condition' is untrue
    9. multiple assignment. can you guess what this prints? local a,b,c = 1,2; print(a,b,c) >! 1,2,nil
    10. How to write your own iterator function. You know how for loops call psirs, or ipairs? these sre actual functions and you can write your own
  • edited November 28 Posts: 83

    ...

  • dave1707dave1707 Mod
    Posts: 6,397

    @kendog400 Here’s my above code with a few modifications to shuffle a deck of cards. My suggestion is to look over this code and try to understand what’s happening. That’s an easy way to learn.

    displayMode(FULLSCREEN)
    supportedOrientations(PORTRAIT_ANY)
    
    function setup()   
        rectMode(CENTER)
        suit={"♠️","♣️","♥️","♦️"}
        val={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}  
        deck={}
        shuffle()
    end
    
    function draw()
        background(40, 40, 50)
        fill(255)
        text("Tap screen to shuffle",WIDTH/2,HEIGHT-50)
        cnt=0
        for x=1,4 do
            for y=1,13 do
                cnt=cnt+1
                card(x*100,y*70,deck[cnt],1)
            end
        end
    end
    
    function touched(t)
        if t.state==BEGAN then
            shuffle()
        end
    end
    
    function shuffle()
        local tab={} 
        for z=1,52 do
            table.insert(tab,z)
        end
        deck={}
        for z=1,52 do
            d=math.random(#tab)
            table.insert(deck,tab[d])
            table.remove(tab,d)
        end   
    end
    
    function card(x,y,v,sc)      -- x,y position    Card value 1 to 52   sc=size of card
        scale(sc)
        local c=(v-1)%13
        local s=(v-1)//13+1
        fill(255)
        stroke(0, 255, 182, 255)
        strokeWidth(3)
        rect(x,y,45,60)
        fill(255,0,0)
        if s<3 then
            fill(0)
        end
        fontSize(25)
        text(val[c+1],x,y)
        fontSize(14)
        text(suit[s],x-10,y+18) 
        text(suit[s],x+10,y-18) 
        scale(1/sc)
    end
    
  • Posts: 83

    Thank You for the help..

  • edited November 28 Posts: 83

    ...

  • Posts: 83

    I have here a program again, it dident go through last time. It prints the hand to the screen vertically, i'm trying to figure out i can get this printed horizontally, then use the "\n" to print the suit on the next line, after that i would use the vec2's to put a rectangle under it to make it look like a card.

    Can anyone take a peek to help me find the spot where i could take control and make the print out horizontal(straight across) ?

    I've been trying to figure this out, buy changing every spot, but I havent figured it out as of yet.

    This looks like a good game here, and a good starting point.

    -- Five Card Poker
    supportedOrientations(LANDSCAPE_ANY)
    displayMode(FULLSCREEN)
    
    function setup()
        font("ArialRoundedMTBold")
        fontSize(40)
        str=""  -- matches
        hand={} -- table of current hand
        suit={"♥️","♦️","♠️","♣️"}
        value={" 2"," 3"," 4"," 5"," 6"," 7"," 8",
                " 9","10","J","Q","K","A"}  
        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 with new 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 the deck (Unshuffled); 1-thru-52
        end      
        for z=1,52 do
            local r=math.random(1,#d1)    -- get a random card from table (d1)
            table.insert(d2,d1[r])                -- insert it into shuffled deck (d2)
            table.remove(d1,r)                    -- remove it from unshuffled d1
        end
        return d2                                      -- return shuffled deck
    end
    
  • dave1707dave1707 Mod
    Posts: 6,397

    Change the function show with this.

    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
                    fill(255)
                    stroke(247, 192, 22, 255)
                    strokeWidth(4)
                    rect(240+cnt*80,HEIGHT-310,75,110)
                    fill(255,0,0)
                    local card=string.format("%5s",value[x])
                    text(card,225+cnt*80,HEIGHT-250)
                    local card=string.format("%5s",suit[y])
                    text(card,257+cnt*80,HEIGHT-300)
                end
            end
        end
        popStyle()
        text(str,WIDTH/2,HEIGHT-600)
    end
    
  • Posts: 83

    Thanks a Million !!!

  • Posts: 83

    This shuffle program is good, but it prints the 4 cards to the screen horizontally, I've trying to figure out i can get more than 4 cards (mabey 13) printed accross (horizontally), and change this to landscape mode. If i change it the way it is, it still prints only 4 cards accross. I've been hacking through this with no luck. I duplicate the program, and try to change every vairable, still i cant seem to find out how this done. Take a peek, am i missing the right spot ? It seems like it would be in the draw mode..
    ~~~
    --Card Shuffle
    displayMode(FULLSCREEN)
    supportedOrientations(PORTRAIT_ANY)

    function setup()
    rectMode(CENTER)
    suit={"♥️","♦️","♠️","♣️}
    val={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}
    deck={} --table or deck of cards
    shuffle() --shuffle function
    end

    function draw() --It seems like the print accross would be here somewhere
    background(40, 40, 50)
    fill(255) --white font color
    text("Tap screen to shuffle",WIDTH/2,HEIGHT-50)
    cnt=0
    for x=1,4 do --4 cards accross ??
    for y=1,13 do --13 cards down ??
    cnt=cnt+1
    card(x100,y70,deck[cnt],1)
    end
    end
    end

    function touched(t) --shuffle & display as soon as screen is touched
    if t.state==BEGAN then
    shuffle()
    end
    end

    function shuffle()
    local tab={}
    for z=1,52 do --z (the amount of cards)
    table.insert(tab,z)
    end
    deck={} --table is deck of cards
    for z=1,52 do
    d=math.random(#tab) --pluck the random card
    table.insert(deck,tab[d]) --put it into the shuffle deck
    table.remove(tab,d) --remove it from the unshuffled deck
    end
    end

    function card(x,y,v,sc)
    scale(sc) --sc=size of card
    local c=(v-1)%13 --card value
    local s=(v-1)//13+1 --card suit
    fill(255) --white color for the card
    stroke(0, 255, 182, 255)
    strokeWidth(3)
    rect(x,y,45,60) -- x,y position
    fill(255,0,0) --red color for dimonds & hearts
    if s<3 then
    fill(0) --black color for clubs & spades
    end
    fontSize(25)
    text(val[c+1],x,y) --card value
    fontSize(14)
    text(suit[s],x-10,y+18) --card suit
    text(suit[s],x+10,y-18) --card suit upsidde down ?
    scale(1/sc)
    end

  • dave1707dave1707 Mod
    Posts: 6,397

    @kendog400 You only need to swap 2 numbers and reduce the value of a third number then change it from portrait to landscape mode.

  • Posts: 83

    Thanks again !...I'LL be getting back to the drawing board. Next I would like to put in some shuffle sounds and such...I dont know which is the third number to reduce, but I'LL try to figure it out...

  • dave1707dave1707 Mod
    Posts: 6,397

    @kendog400 Once you run the code in landscape mode with the 2 values swapped, you’ll know what number to reduce.

  • dave1707dave1707 Mod
    Posts: 6,397

    @kendog400 You might like this code.

    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)
    
    function setup() 
        rectMode(CENTER)
        cc={x=WIDTH/2,y=50} 
        suit={"♠️","♣️","♥️","♦️"}
        val={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}    
        show=-1
        tab={}
        for y=1,4 do
            for x=1,13 do
                table.insert(tab,vec2(x*60+75,y*90+300))
            end
        end
        done=true
        offset=0 
    end
    
    function draw()
        background(0)
        fill(255)
        text("Tap screen to shuffle",WIDTH/2,HEIGHT-50)
        if show>0 then
            for z=1,show do
                card(tab[z].x,tab[z].y,sDeck[z],1)
            end
        end
        fill(0, 92, 255, 255)
        if offset<=#tab then
            rect(WIDTH/2,50,50,80)
            rect(cc.x,cc.y,50,80)
        end
    end
    
    function touched(t)
        if t.state==BEGAN and done then
            shuffle()
            deal()  
        end
    end
    
    function deal()  
        cc={x=WIDTH/2,y=50} 
        show=show+1
        offset=offset+1 
        done=false
        sound("Game Sounds One:Whoosh 2")
        if offset<=#tab then   
            t1=tween(.2,cc,{x=tab[offset].x,y=tab[offset].y},{loop=tween.loop.once},deal)
        else
            done=true
        end     
    end
    
    function shuffle()
        offset=0 
        show=-1
        sDeck={}
        local t={}
        for z=1,52 do
            t[z]=z
        end
        for z=1,52 do
            v=math.random(#t)
            sDeck[z]=t[v]
            table.remove(t,v)
        end
    end
    
    function card(x,y,v,sc)      -- x,y position    Card value 1 to 52   sc=size of card
        scale(sc)
        local c=(v-1)%13
        local s=(v-1)//13+1
        fill(255)
        stroke(0, 255, 182, 255)
        strokeWidth(3)
        rect(x,y,45,60)
        fill(255,0,0)
        if s<3 then
            fill(0)
        end
        fontSize(25)
        text(val[c+1],x,y)
        fontSize(14)
        text(suit[s],x-10,y+18) 
        text(suit[s],x+10,y-18) 
        scale(1/sc)
    end
    
Sign In or Register to comment.