code for playing card shuffle routine
Posted: Wed Dec 23, 2009 2:04 pm
				
				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--
			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