How to Shuffle A Card Deck

local deckOfCards ={ "assets/images/cards/s1.png","assets/images/cards/s2.png","assets/images/cards/s3.png","assets/images/cards/s4.png","assets/images/cards/s5.png","assets/images/cards/s6.png","assets/images/cards/s7.png","assets/images/cards/s8.png","assets/images/cards/s9.png","assets/images/cards/s10.png","assets/images/cards/s11.png","assets/images/cards/s12.png","assets/images/cards/s13.png", "assets/images/cards/h1.png","assets/images/cards/h2.png","assets/images/cards/h3.png","assets/images/cards/h4.png","assets/images/cards/h5.png","assets/images/cards/h6.png","assets/images/cards/h7.png","assets/images/cards/h8.png","assets/images/cards/h9.png","assets/images/cards/h10.png","assets/images/cards/h11.png","assets/images/cards/h12.png","assets/images/cards/h13.png", "assets/images/cards/c1.png","assets/images/cards/c2.png","assets/images/cards/c3.png","assets/images/cards/c4.png","assets/images/cards/c5.png","assets/images/cards/c6.png","assets/images/cards/c7.png","assets/images/cards/c8.png","assets/images/cards/c9.png","assets/images/cards/c10.png","assets/images/cards/c11.png","assets/images/cards/c12.png","assets/images/cards/c13.png", "assets/images/cards/d1.png","assets/images/cards/d2.png","assets/images/cards/d3.png","assets/images/cards/d4.png","assets/images/cards/d5.png","assets/images/cards/d6.png","assets/images/cards/d7.png","assets/images/cards/d8.png","assets/images/cards/d9.png","assets/images/cards/d10.png","assets/images/cards/d11.png","assets/images/cards/d12.png","assets/images/cards/d13.png" } local function shuffleCardsDeck() local yAxis =math.random(display.contentCenterY-500,display.contentCenterY+500) for i = 1 , #cardsImages do local cardShuffle=math.random(1,#cardsImages) print (" ".."Card Displayed in Position".." "..i.." ".."is".." "..cardShuffle) i = display.newImageRect( cardsImages[math.random(1,#cardsImages)], 140, 180 ) i.x=display.contentCenterX i.y=yAxis transition.to(i,{time=500,y=display.contentCenterY+20,x=display.contentCenterX,width=140 ,height=180}) if i == nil then i[#i+1] = cardShuffle print (" ".."Card Displayed in Position".." "..i.." ".."is".." "..cardShuffle) shuffleCardsDeck() else end -- shuffleCardsDeck() -- cards[i] = display.newImageRect( cardsImages[math.random(1,#cardsImages)], 140, 180 ) -- cards[i].x=display.contentCenterX -- cards[i].y=yAxis -- transition.to(cards[i],{time=500,y=display.contentCenterY+20,x=display.contentCenterX,width=140 ,height=180}) --print(cardsImages) -- print("Total Cards In Deck".." = "..i.." "..cardsImages[i]) end -- cards[52]:addEventListener( "tap", splitCardsDeck,1) end timer.performWithDelay(1000,shuffleCardsDeck,1)

Hi Everyone

   I am working on cards game and want exact code of shuffling of card deck without repetition of cards using for loop(especialy when loop repeats until 52 , no card should be repeated)…Please see my code and  guide properly…

I would create a temporary table of card indexes, then randomly pick one of the entries, move to a “shuffled” table, and remove from the temp table:

-- create temp deck local tempDeck = {} local shuffledDeck = {} for i = 1, 52 do tempDeck[i] = i end -- shuffle for i = 1, #deckOfCards do shuffledDeck[#shuffledDeck + 1] = deckOfCards[math.random(1, #tempDeck)] table.remove(tempDeck, i] end

Note, I didn’t test this code, but it should give you a starting point.

–John

[lua]

local function shuffle(t)

    local rand = math.random

    for i = #t, 2, -1 do

        local j = rand(i)

        t[i], t[j] = t[j], t[i] – function to shuffle a table (used for shuffling clues)

    end

end

shuffle(cards)

[/lua]

I would create a temporary table of card indexes, then randomly pick one of the entries, move to a “shuffled” table, and remove from the temp table:

-- create temp deck local tempDeck = {} local shuffledDeck = {} for i = 1, 52 do tempDeck[i] = i end -- shuffle for i = 1, #deckOfCards do shuffledDeck[#shuffledDeck + 1] = deckOfCards[math.random(1, #tempDeck)] table.remove(tempDeck, i] end

Note, I didn’t test this code, but it should give you a starting point.

–John

[lua]

local function shuffle(t)

    local rand = math.random

    for i = #t, 2, -1 do

        local j = rand(i)

        t[i], t[j] = t[j], t[i] – function to shuffle a table (used for shuffling clues)

    end

end

shuffle(cards)

[/lua]