
scare crow
Member

Jan 9, 2006, 12:39 PM
Post #1 of 1
(173 views)
Shortcut
|
|
shuffle virtual deck
|
Can't Post
|
|
not made by me While I was creating my black jack game I wanted to make the user feel like they were using an actual deck of cards. To make this illusion even more accurate I decided that I would have the computer keep track of which cards had been used and after all cards had been used I wanted the deck to be shuffled. Therefore I had to figure out a easy way to shuffle the deck. That is how I came up with the following method: First I made an image control array called imgCards that went from 1 to 52. In this array I filled slot (1) with the ace of clubs, slot (2) with the two of clubs, and so on I continued this process until I had all 52 cards put into their corresponding slots. This would make the array look like this: ------Key----------------------------------------------- |H= Hearts C= Clubs D= Diamonds S= Spades| |J= Jack Q= Queen K= King | -------------------------------------------------------- SLOT CARD SLOT CARD SLOT CARD SLOT CARD ---- ---- ---- ---- ---- ---- ---- ---- 1 1C 14 1D 27 1H 40 1S 2 2C 15 2D 28 2H 41 2S 3 3C 16 3D 29 3H 42 3S 4 4C 17 4D 30 4H 43 4S 5 5C 18 5D 31 5H 44 5S 6 6C 19 6D 32 6H 45 6S 7 7C 20 7D 33 7H 46 7S 8 8C 21 8D 34 8H 47 8S 9 9C 22 9D 35 9H 48 9S 10 10C 23 10D 36 10H 49 10S 11 JC 24 JD 37 JH 50 JS 12 QC 25 QD 38 QH 51 QS 13 KC 26 KD 39 KH 52 KS Now that I had my virtual deck I had to figure out a procedure to shuffle it. After trying many different ways, I concluded that the best way to do this task was to make numeric array called arrDeck that went from 1 to 52. Then I would choose a random slot in the arrDeck and put the first card in my imgCards array into it. Then take my second card in the imgCards array and put it into a different random slot in the arrDeck array. I would do this until every slot in the arrDeck array was filled. I know it sounds confusing but it really isn't just look at the code below: I already had this declared in my declarations section: Dim arrDeck(1 To 52) As Integer This is in my procedure: Public Sub ShuffleDeck() Randomize 'Makes it so the starting seed is different Dim CardCount As Integer 'This will be my loop counter Dim CardVal As Integer 'This will get assigned the random value CardCount = 1 'Resets card count to 1 Do While CardCount < 53 'This sets every slot in arrDeck to -1 arrDeck(CardCount) = -1 'so that later I can tell if the slot is empty CardCount = CardCount + 1 'or not Loop CardCount = 1 'Resets card count to 1 Do While CardCount < 53 'Loop until every slot is full 10 CardVal = Int((52 * Rnd) + 1) 'Set CardVal = to a random number If arrDeck(CardVal) = -1 Then 'Take random slot and see if there is a card 'in it yet. If there isn't it will be = to -1 'if there is it will be equal to a different 'value. arrDeck(CardVal) = CardCount'There isn't so put current card in that slot CardCount = CardCount + 1 'Increment my card count Else: GoTo 10 'There is so start again from the beginning End If Loop 'Do it all again if < 53 End Sub I hope that makes it easier to understand. all credit goes to plya at runescapetrades.com
|