2

I realized a flaw on my coding for picking cards from my structure group arrays this morning for a tabletop card game I'm converting to computer. Currently the code is set up to randomly pick an array of a specific card, but if quantity of cards in the group is < 1, then the group is skipped. For my deck this isn't being truly random because each card has its own quantity. Some cards have 10 cards, others 6, most 4. How can I code it to where it looks at all these quantity numbers as a whole and picks a random card out of these numbers.

If GameSize >= 3 Then
    For StartHands = 10 To 14
        Number = (DeckGroup(Rnd.Next(0, DeckGroup.Count)).ID)
        'Cardslots Player3
        If CardTypeArray(StartHands) = "" Then
            If DeckGroup(Number).QuantityInteger > 0 Then
                DeckGroup(Number).QuantityInteger -= 1
                Player1HandGroup(Number).QuantityInteger3 += 1
                CardTypeArray(StartHands) = Player1HandGroup(Number).CardType
                Me.NumberArray(StartHands) = Number
            Else
                'Recall Procedure if Generated Random Number is not allowed 
                'due to QuantityInteger <= 0
                Call StartButton_Click(sender, e)
            End If
        End If
    Next StartHands
End If

when playing a card, and drawing a new card, I use this coding scheme, to prevent stackoverflow, but is virtual the same.

Dim temp As IEnumerable(Of LunchMoneyGame.LunchMoneyMainForm.Group) = From r In DeckGroup Where r.QuantityInteger > 0 Select r

If temp IsNot Nothing AndAlso temp.count > 0 Then
    Number = (temp(Rnd.Next(0, temp.Count)).ID)

    DeckGroup(Number).QuantityInteger -= 1
    'Select the Player depending value of T
    Select Case T
        Case 0
            Player1HandGroup(Number).QuantityInteger += 1
        Case 1
            Player1HandGroup(Number).QuantityInteger2 += 1
        Case 2
            Player1HandGroup(Number).QuantityInteger3 += 1
        Case 3
            Player1HandGroup(Number).QuantityInteger4 += 1
        Case 4
            Player1HandGroup(Number).QuantityInteger5 += 1
    End Select

Edit: Improved question: How can I weight the probably of drawing a card based on how many of a particular card are left in the decks quantity integer for each card?

Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
Gakko no Ato
  • 105
  • 15
  • Deck sizes are limited enough to allow filling a list with the individual cards. Then it is trivial, just use a [Fisher-Yates shuffle](http://stackoverflow.com/a/8108702/17034). – Hans Passant Mar 30 '13 at 17:22
  • My deck has 106 cards, most cards with their quantity of a given card. – Gakko no Ato Mar 30 '13 at 17:54

1 Answers1

0

The idea of my solution is to generate a random number over the total number of availables cards in all deck groups. Finally we find the deck group this number targets within a loop:

Dim totalNumerOfCards As Integer = DeckGroup.Sum(Function(d) d.QuantityInteger)
Dim Number As Integer = Rnd.Next(totalNumerOfCards)
Dim numCards As Integer = 0
Dim groupIndex As Integer = 0
Dim cardIndex As Integer = 0
Dim i As Integer = 0
While i < DeckGroup.Length AndAlso numCards <= Number
    groupIndex = i
    cardIndex = Number - numCards
    numCards += DeckGroup(i).QuantityInteger
    i += 1
End While
Console.WriteLine("Your card is in DeckGroup({0}), card index {1}",
                  groupIndex, cardIndex);
Alex Essilfie
  • 12,339
  • 9
  • 70
  • 108
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • in my procedure for pick a card to put in a players hand my number variable now becomes 106 each time instead of the desired random number and gives me index out of range error. – Gakko no Ato Apr 19 '13 at 06:46
  • Make sure that you are intitializing your Random object only once at the beginning of the game, instead of each time you are using it, otherwise you might get the same random number at each call. Also only specify a seed value for debugging purposes. This will generate a reproducable sequence. In the "real" game don't specify one in order to get a different game each time. The Debugger is your best friend. Use it to track the exception! – Olivier Jacot-Descombes Apr 19 '13 at 13:59