print command executes multiple times

So im pretty sure im doing something wrong and I can’t figure this out. Any help is appreciated.

I have a card deck

local cardDeck = {"2S","3S","4S","5S","6S","7S","8S","9S","10S","AS","KS","QS","JS","2H","3H","4H","5H","6H","7H","8H","9H","10H","AH","KH","QH","JH","2C","3C","4C","5C","6C","7C","8C","9C","10C","AC","KC","QC","JC","2D","3D","4D","5D","6D","7D","8D","9D","10D","AD","KD","QD","JD","J1","J2"}

and a shuffle function…

math.randomseed( os.time() )
local function shuffleDeck( t )
    local j
    for i = #t, 2, -1 do
        j = math.random( i )
        t[i], t[j] = t[j], t[i]
    end
    return t
end

and a draw cards function

local currentIndex = 1 
function drawCards( num, deck ) 
    local cardsDrawn = {}   
    for i = currentIndex, num do
        cardsDrawn[#cardsDrawn+1] = deck[i]
    end
    currentIndex = currentIndex + num
    return cardsDrawn
end

and a button event

local function pushShuffleButton( event )
    cardDeck = shuffleDeck(cardDeck)
    print("Drawing CPU cards\n")
    cpuHand = drawCards( 27, cardDeck )
    print(table.concat(cpuHand,", "))

    print("Drawing Player 1 cards\n")
    p1Hand = drawCards( 27, cardDeck )
    print(table.concat(p1Hand,", "))
end

To see the values in both cpuHand and p1Hand im using the print command. However, the command executes more than 1 time. Any idea why?

My results look something like this

May 05 10:43:18.768 Copyright © 2009-2018 C o r o n a L a b s I n c .
Version: 3.0.0
Build: 2018.3326
May 05 10:43:18.775 Loading project from: ~/Documents/Corona/AP4
May 05 10:43:18.775 Project sandbox folder: ~/Library/Application Support/Corona Simulator/AP4-4638A0329A9F7CE9346821C4FE7FBC3A
May 05 10:43:18.780 Platform: iPhone / x86_64 / 10.15.4 / Intel® Iris™ Graphics 6100 / 2.1 INTEL-14.5.22 / 2018.3326 / en-CA | CA | en_CA | en
May 05 10:43:27.121 Drawing CPU cards
May 05 10:43:27.122 7S, 5H, 4S, J1, 2H, 6S, JS, 3H, JH, AS, AC, 7C, 4H, 6H, QS, 8H, 3C, AD, AH, 8D, JC, J2, 9C, 5S, 3S, KH, 3D
Drawing Player 1 cards
May 05 10:43:27.122 Drawing CPU cards

                Drawing Player 1 cards

May 05 10:43:27.129 Drawing CPU cards
May 05 10:43:27.129 Drawing Player 1 cards
May 05 10:43:27.147 Drawing CPU cards

                Drawing Player 1 cards

May 05 10:43:27.152 Drawing CPU cards

                Drawing Player 1 cards

May 05 10:43:27.168 Drawing CPU cards
May 05 10:43:27.169 Drawing Player 1 cards

                Drawing CPU cards
                
                Drawing Player 1 cards

May 05 10:43:27.183 Drawing CPU cards

                Drawing Player 1 cards

May 05 10:43:27.184 Drawing CPU cards

                Drawing Player 1 cards

May 05 10:43:27.185
May 05 10:43:27.201 Drawing CPU cards

                Drawing Player 1 cards

May 05 10:43:27.201 Drawing CPU cards

                Drawing Player 1 cards

May 05 10:43:27.218 Drawing CPU cards

                Drawing Player 1 cards

May 05 10:43:27.218 Drawing CPU cards

                Drawing Player 1 cards

Whats happening here?

Touch events have multiple phases. You should specify during which phase of the event you wish to run the code.

For instance,

local function pushShuffleButton( event )
    if event.phase == "began" then
         cardDeck = shuffleDeck(cardDeck)
         print("Drawing CPU cards\n")
         cpuHand = drawCards( 27, cardDeck )
         print(table.concat(cpuHand,", "))

         print("Drawing Player 1 cards\n")
         p1Hand = drawCards( 27, cardDeck )
         print(table.concat(p1Hand,", "))
    end
end

You should check out https://docs.coronalabs.com/api/event/touch/phase.html

OMG thank you! That totally cleared it up.

I do have a followup question.

In this function

local function pushShuffleButton( event )
    cardDeck = shuffleDeck(cardDeck)
    print("Drawing CPU cards\n")
    cpuHand = drawCards( 27, cardDeck )
    print(table.concat(cpuHand,", "))

    print("Drawing Player 1 cards\n")
    p1Hand = drawCards( 27, cardDeck )
    print(table.concat(p1Hand,", "))
end

My first array prints just fine but the log stops after the 3rd print command. Is there a limti? I tried printing p1Hand first instead of cpuHand and the effect is the same.

May 05 12:14:39.510 Copyright © 2009-2018 C o r o n a L a b s I n c .
Version: 3.0.0
Build: 2018.3326
May 05 12:14:39.518 Loading project from: ~/Documents/Corona/AP4
May 05 12:14:39.518 Project sandbox folder: ~/Library/Application Support/Corona Simulator/AP4-4638A0329A9F7CE9346821C4FE7FBC3A
May 05 12:14:39.522 Platform: iPhone / x86_64 / 10.15.4 / Intel® Iris™ Graphics 6100 / 2.1 INTEL-14.5.22 / 2018.3326 / en-CA | CA | en_CA | en
May 05 12:14:40.984 Drawing Player 1 cards
6C, 7C, 5D, KH, JS, AS, 8D, 3H, KD, 10S, AH, 5S, QH, 2C, 10H, 7D, AC, AD, 10D, QC, QD, 9H, 6H, 2H, 4S, JC, 4C
May 05 12:14:40.984 Drawing CPU cards

Edit: I also noticed that after the first block, it actually doesn’t reach those lines.
Example:

		print("Drawing CPU cards\n")
		cpuHand = drawCards( 27, cardDeck )
		print(table.concat(cpuHand,", "))

		print("Drawing Player 1 cards\n")
		playerHand = drawCards( 27, cardDeck )
		print(table.concat(playerHand,", "))

		p1Score = countHand(playerHand)
		p1ScoreText.text = "Your Score: "..p1Score

In this scenario, the p1Score = 0

If I switch up and put the code for Drawing P1 cards above the CPU cards then the p1Score shows 27 (as it should)

Well, firstly, your shuffleDeck function probably doesn’t work like you want it to work. I’ve actually posted a detailed reply to this very issue at https://gist.github.com/Uradamus/10323382#gistcomment-3149506.

Your drawCards function also has a problem with how you draw the cards. Specifically, you are starting the loop from currentIndex and continuing until num. The first time you do this, currentIndex is 1 and your num is 27, but after the loop you then increase currentIndex to 1+27=28. Then the nex time you run the function the loop runs from 28 to 27, i.e. it doesn’t run.

You could fix that by writing: for i = currentIndex, currentIndex+num-1 do, but then you’d run into issues once you run out of cards in deck.

@XeduR you are a lifesaver! I read the git page and it makes so much sense. Thank you for sharing that!

As for drawCards(), my intention is to split the deck 50/50 at this time. Your solution works for this purpose perfectly. Again, many thanks!