Page 1 of 1

code for playing card shuffle routine

Posted: Wed Dec 23, 2009 2:04 pm
by el_stupido
I swear I posted this already but my internet connnection on my main computer is so slow cause of the stone room it is in (aka wireless purgatory) maybe it didn't go thru...

Hey guys,

here's my second attempt at a playing card shuffling routine. My first attempt, the computer would sometimes place two of the same card in the deck. I think I've solved it with this version. But you are welcome to play and improve. If you do can you share?! :)

Here's the code. Uses arrays. Which I have to say are super awesome. Feedback welcome. Happy to share.

El Stupido

--CODE FOLLOWS--

Code: Select all

on shuffle_deck_ii
   -- first up we build an array of cards in order
   -- At this stage we don't need to assign house names, or whatever, we just need to create the deck of cards. House name attributes can be dealt with in the shuffle.
   put 1 into cardcount
   repeat with i = 1 to 4
      repeat with k = 1 to 13
         put i into item 1 of line cardcount of thedeck
         put k into item 2 of line cardcount of thedeck
         put false into item 3 of line cardcount of thedeck
         add 1 to cardcount 
      end repeat
   end repeat
   
   -- okkay so that's made a variable that's 52 lines deep with card attributes (i = house, k = card number) and a flag that can be changed to true when we've shuffled the card...

   put empty into checkcard
   repeat with i = 1 to 52
      -- choose a random card from thedeck, loop until I find a card that hasn't been shuffled
      put 0 into flag
      
      repeat until flag = 1
         put the random of 52 into shuff
         put line shuff of thedeck into thecard
         if item 3 of thecard <> true then
            put true into item 3 of line shuff of thedeck
            put 1 into flag
         end if
      end repeat
      
      -- NOW we classsify the cards etc, and then add to an array...
      
      put item 1 of thecard into card_house
      
      if card_house = 1 then
         put "Hearts" into card_house_name
      else if card_house = 2 then
         put "Diamonds" into card_house_name
      else if card_house = 3 then
         put "Clubs" into card_house_name
      else 
         put "Spades" into card_house_name
      end if
      
      put item 2 of thecard into card_number
      
      put card_number into item 1 of thiscard
      put card_house into item 2 of thiscard
      put card_house_name into item 3 of thiscard
      put 0 into flag
      
      
      
      put thiscard into deck[i]
      put thiscard into line i of cards_played_already
   end repeat
   
end shuffle_deck_ii

Re: code for playing card shuffle routine

Posted: Wed Dec 23, 2009 6:11 pm
by sturgis
You could do something like this.

Code: Select all

on mouseUp
   set the randomSeed to the seconds -- changed seed on each use so that the same sequences don't keep coming up.
   put empty into theShuffledCards -- actually not needed after I think about it. each line will always be replaced in this var on every run. 
-- the list of cards will never change.  Well unless you're doing varied card games at which point a string can be setup for each deck type   
put "aH,aD,aS,aC,2H,2D,2S,2C,3H,3D,3S,3C,4H,4D,4S,4C,5H,5D,5S,5C,6H,6D,6S,6C,7H,7D,7S,7C,8H,8D,8S,8C,9H,9D,9S,9C,10H,10D,10S,10C,JH,JD,JS,JC,QH,QD,QS,QC,KH,KD,KS,KC" into theCards
   repeat with i = 1 to 52 -- same as the question above.  52 can be changed to a var that contains the passed number of cards based on deck type
      put random (the number of items in theCards) into tCard --grab a random item from our card string
      put item tCard of theCards into line i of theShuffledCards -- put it into our shuffled cards var.  could do this as items or lines, or build an array.
      delete item tCard of theCards -- remove it from the string so we don't have to do any checks to see if we already pulled a card
   end repeat
end mouseUp

Re: code for playing card shuffle routine

Posted: Tue Dec 29, 2009 9:58 am
by el_stupido
I see what you did there. Clever