2

今天早上,我意识到我在为我正在转换为计算机的桌面纸牌游戏中从结构组数组中挑选卡片的编码存在缺陷。当前代码设置为随机选择特定卡片的数组,但如果组中的卡片数量<1,则跳过该组。对于我的牌组来说,这并不是真正的随机,因为每张牌都有自己的数量。有些卡有 10 张卡,其他 6 张,大多数是 4 张卡。我如何将它编码到它查看所有这些数量的整体数字并从这些数字中选择一张随机卡的位置。

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

在打牌和抽新牌时,我使用这种编码方案,以防止堆栈溢出,但虚拟相同。

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?

4

1 回答 1

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);
于 2013-03-30T17:21:27.730 回答