Option Explicit
Global TheDeck() As Integer 'allow for up to 8 decks
Global DeckTot As Integer 'keep track of number of cards in deck
Global TimesToShuffle As Integer 'number of times to shuffle deck
Public Function InitDeck() As Integer
Dim i As Integer
Dim j As Integer
If DeckTot = 0 Then Exit Function
'place the cards in order in the deck
For i = 0 To DeckTot - 1
TheDeck(i) = i
Next i
'if value of card is higher than 51 then
'subtract 52 to start a new deck
For i = 0 To DeckTot
For j = 1 To DeckTot / 52
If (TheDeck(i) > 51) Then
TheDeck(i) = TheDeck(i) - 52
End If
Next j
Next i
'return values
InitDeck = TheDeck(DeckTot)
End Function
Public Sub doInitialize(nDecks As Long)
Dim intNumDecks As Integer
'initialize number of decks (change this value as needed)
intNumDecks = CInt(nDecks)
'if programmer is trying to use too many
'decks let him know now and require an input of 1 to 8
If intNumDecks > 8 Then
MsgBox "You are trying to use too many decks", vbOKOnly, "Error"
End
End If
'calculate total number of cards
DeckTot = intNumDecks * 52
End Sub
Public Function CardShuff(intNumCardsDeck As Integer, intTimesToShuffle As Integer) As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim intHolder As Integer
'randomize
Randomize Timer
'shuffle cards (intTimesToShuffle) times
'by picking a two random cards and having
'them trade places in the deck.
'loop it through each card in the deck.
For k = 1 To intTimesToShuffle
For i = 0 To intNumCardsDeck - 1
j = Int(Rnd * intNumCardsDeck)
intHolder = TheDeck(i) 'these lines
TheDeck(i) = TheDeck(j) 'swap the cards
TheDeck(j) = intHolder 'places
Next i
Next k
'return values
CardShuff = TheDeck(DeckTot)
End Function
Public Sub Shuffle()
Dim nDecks As Long
'nDecks = InputBox("Enter number of decks (1 to 8)", "Number of Decks")
'nDecks = CLng(UserForm1.TextBox1.Text)
nDecks = 2
doInitialize nDecks
ReDim TheDeck(52 * nDecks)
DeckTot = CInt(52 * nDecks)
InitDeck
'TimesToShuffle = InputBox("Input number of times to shuffle", "Number of Shuffles")
'TimesToShuffle = CInt(UserForm1.TextBox2.Text)
TimesToShuffle = 3
TheDeck(DeckTot) = CardShuff(DeckTot, TimesToShuffle)
Dim n As Integer
Dim rank As Integer
Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("A1:A416").ClearContents
For n = 0 To DeckTot - 1
If TheDeck(n) < 36 Then
Worksheets("Sheet1").Cells(n + 1, 1).Value = TheDeck(n) \ 4 + 1
Else
Worksheets("Sheet1").Cells(n + 1, 1).Value = 10
End If
Next n
Stop
'Let n=(cards-1), where cards=52*decks
'If cards=1, reference top card
'If cards=2, reference second card
'If cards=n, reference nth card
'At this point TheDeck(n) holds these cards
'If TheDeck(n)=(0 to 3) the card is an ace
'If TheDeck(n)=(4 to 7) the card is an two
'If TheDeck(n)=(8 to 11) the card is an three
'If TheDeck(n)=(12 to 15) the card is an four
'If TheDeck(n)=(16 to 19) the card is an five
'If TheDeck(n)=(20 to 23) the card is an six
'If TheDeck(n)=(24 to 27) the card is an seven
'If TheDeck(n)=(28 to 31) the card is an eight
'If TheDeck(n)=(32 to 35) the card is an nine
'If TheDeck(n)=(36 to 51) the card is an ten
End Sub