Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' Shuffle deck of Cards with simple one-pass algorithm
- Public Sub Shuffle()
- ' after shuffling, dealing should start at deck(0)
- currentCard = 0 ' reinitialize currentCard
- ' for each Card, pick another random Card and swap them
- For first = 0 To deck.GetUpperBound(0)
- ' select a random number between 0 and 51
- Dim second As Integer = randomNumbers.Next(NUMBER_OF_CARDS)
- ' swap current Card with randomly selected Card
- Dim temp As Card = deck(first) ' store copy of deck(first)
- deck(first) = deck(second) ' move deck(second) to deck(first)
- deck(second) - temp ' move original deck(first) to deck(second)
- Next
- End Sub ' Shuffle
- ' deal one card
- Public Function DealCard() As Card
- ' determine whether Cards remain to be dealt
- If current Card <= deck.GetUpperBound(0) Then
- Dim last Card As Integer = currentCard ' store current card number
- currentCard += 1 ' increment current card number
- Return deck(lastCard)
- Else
- Return Nothing ' no more cards to deal
- End If
- End Function ' Deal Card
- End Class ' DeckOfCards
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement