dimanche 22 octobre 2017

Shuffling a Deck in Visual Basic?

Here is my source:

Public Class Deck

    Dim deckList As New List(Of Card)

    Sub ShuffleDeck()
        'creates a new deck to copy to, a list of random numbers, and a randomizer object to make random number in a range
        Dim tempDeck As New List(Of Card)
        Dim listOfRandomNumbers As New List(Of Integer)
        Dim randomizer As New Random
        Dim randomNumber As Integer

        'this generates a random number then adds it to the list of random numbers if there is no others already like it in the list of random numbers
        Do While (listOfRandomNumbers.Count() < 52)

            randomNumber = randomizer.Next(0, 51)

            If listOfRandomNumbers.Contains(randomNumber) = False Then
                listOfRandomNumbers.Add(randomNumber)
            End If

        Loop

        Dim index As Integer
        Do While (index < 51)
            'this adds a card to the tempDeck at a random index of the deck list
            tempDeck.Add(deckList(listOfRandomNumbers(index)))
            index = index + 1
        Loop

        'assigning the deck from the ordered one to the shuffled one
        deckList = tempDeck

    End Sub

End Class

I am trying shuffle a Deck of cards, but I'm getting stuck in an infinite loop here when I attempt to make my listOfRandomNumbers contain unique values that aren't repeated.

Do While (listOfRandomNumbers.Count() < 52)

     randomNumber = randomizer.Next(0, 51)

     If listOfRandomNumbers.Contains(randomNumber) = False Then
          listOfRandomNumbers.Add(randomNumber)
     End If

Loop

Any thoughts on this?




Aucun commentaire:

Enregistrer un commentaire