Comparing card values from a deck of cards

I am creating a deck of cards like so:

local blackjack = {}

blackjack.suits = {“heart”,“diamond”,“club”,“spade”}

function blackjack.create_deck()

    blackjack.deck={}

      for i=1, 4 do

          for j=1, 13 do

            local temp_card = blackjack.suits[i]…j

            table.insert(blackjack.deck,temp_card)

        end

    end

end

blackjack.create_deck()

I am trying to get a card value for each one so I can add them later.  

blackjack.value=tonumber(blackjack.deck[1])

print(‘card1’…blackjack.value)

But it gives error, most likely because I have the suit string in there.  Is there a way for me to access the card value using this create deck function?

Edit: replaced my first reply as it would be a complicated shuffle.

This may work

local blackjack = {}
blackjack.suits = {“heart”,“diamond”,“club”,“spade”}

print("blackjack.suits[1] == ", blackjack.suits[1])

function blackjack.create_deck()
    blackjack.deck={}

      for i=1, 4 do
          for j=1, 13 do
            local  TempCard = {}
             TempCard.Suit = blackjack.suits[i]
             TempCard.Value = j
             table.insert(blackjack.deck, TempCard)

           

             --[[

             – add your image for the card like this, assuming you label your Card Images: Card1.png, Card2.png etc

             local Index = #blackjack.deck

             blackjack.deck[Index].Image = “Card”…Index…".png"

            --]]
         end
     end
end
blackjack.create_deck()

    print("blackjack.deck[1].Suit == ", blackjack.deck[1].Suit)
    print("blackjack.deck[1].Value == ", blackjack.deck[1].Value)
    
    print("blackjack.deck[15].Suit == ", blackjack.deck[15].Suit)
    print("blackjack.deck[15].Value == ", blackjack.deck[15].Value)

Gotta get a little creative to deal with the Ace with this though.

Hope this helps,

Nail

Nail, 

Thank you, I am trying to print out then shuffle the deck.  What I was able to do previously is below.  Now we changed the name of table yes?  How should I name it now when I pass it into the shuffle function?  Now it is not working correctly.

[lua]for i = 1, #blackjack.deck do 

   print(blackjack.deck[i])

end[/lua]

then to shuffle I found this function in forums

[lua]function blackjack.shuffleTable(tablename)

    t = tablename

    function table.shuffle(t)

        math.randomseed(os.time())

        assert(t, “table.shuffle() expected a table, got nil”)

        local iterations = #t

        local j

        for i = iterations, 2, -1 do

            j = math.random(i)

            t[i], t[j] = t[j], t[i]

        end

    end

        return table.shuffle(t)

end

blackjack.shuffleTable(blackjack.deck)

[/lua]

r305,

No, I don’t believe we changed the table name, I added variables to each card is all, Value and Suit.

I’m not sure how the shuffle function you found works, but here is the function I use and I believe it works.

Try this…

local function shuffle(t)
    local n = #t
    math.randomseed( os.time() )
    while n > 2 do
        local k = math.random(n)
        t[n], t[k] = t[k], t[n]
        n = n - 1
    end
    return t
end

 blackjack.deck = shuffle(blackjack.deck)
 
 print(“after shuffle…”)
 print("blackjack.deck[1].Suit == ", blackjack.deck[1].Suit)
    print("blackjack.deck[1].Value == ", blackjack.deck[1].Value)
    
    print("blackjack.deck[15].Suit == ", blackjack.deck[15].Suit)
    print("blackjack.deck[15].Value == ", blackjack.deck[15].Value)

Hope this helps,

Nail

Edit: replaced my first reply as it would be a complicated shuffle.

This may work

local blackjack = {}
blackjack.suits = {“heart”,“diamond”,“club”,“spade”}

print("blackjack.suits[1] == ", blackjack.suits[1])

function blackjack.create_deck()
    blackjack.deck={}

      for i=1, 4 do
          for j=1, 13 do
            local  TempCard = {}
             TempCard.Suit = blackjack.suits[i]
             TempCard.Value = j
             table.insert(blackjack.deck, TempCard)

           

             --[[

             – add your image for the card like this, assuming you label your Card Images: Card1.png, Card2.png etc

             local Index = #blackjack.deck

             blackjack.deck[Index].Image = “Card”…Index…".png"

            --]]
         end
     end
end
blackjack.create_deck()

    print("blackjack.deck[1].Suit == ", blackjack.deck[1].Suit)
    print("blackjack.deck[1].Value == ", blackjack.deck[1].Value)
    
    print("blackjack.deck[15].Suit == ", blackjack.deck[15].Suit)
    print("blackjack.deck[15].Value == ", blackjack.deck[15].Value)

Gotta get a little creative to deal with the Ace with this though.

Hope this helps,

Nail

Nail, 

Thank you, I am trying to print out then shuffle the deck.  What I was able to do previously is below.  Now we changed the name of table yes?  How should I name it now when I pass it into the shuffle function?  Now it is not working correctly.

[lua]for i = 1, #blackjack.deck do 

   print(blackjack.deck[i])

end[/lua]

then to shuffle I found this function in forums

[lua]function blackjack.shuffleTable(tablename)

    t = tablename

    function table.shuffle(t)

        math.randomseed(os.time())

        assert(t, “table.shuffle() expected a table, got nil”)

        local iterations = #t

        local j

        for i = iterations, 2, -1 do

            j = math.random(i)

            t[i], t[j] = t[j], t[i]

        end

    end

        return table.shuffle(t)

end

blackjack.shuffleTable(blackjack.deck)

[/lua]

r305,

No, I don’t believe we changed the table name, I added variables to each card is all, Value and Suit.

I’m not sure how the shuffle function you found works, but here is the function I use and I believe it works.

Try this…

local function shuffle(t)
    local n = #t
    math.randomseed( os.time() )
    while n > 2 do
        local k = math.random(n)
        t[n], t[k] = t[k], t[n]
        n = n - 1
    end
    return t
end

 blackjack.deck = shuffle(blackjack.deck)
 
 print(“after shuffle…”)
 print("blackjack.deck[1].Suit == ", blackjack.deck[1].Suit)
    print("blackjack.deck[1].Value == ", blackjack.deck[1].Value)
    
    print("blackjack.deck[15].Suit == ", blackjack.deck[15].Suit)
    print("blackjack.deck[15].Value == ", blackjack.deck[15].Value)

Hope this helps,

Nail