0

所以我的设计是有 26 个图片框(代码只显示到 10.. 你明白了)向用户展示他们的“手”。现在我如何检查那些图像框是否有双打,然后将这些双打移到清除的一堆?假设有 2 个 Jacks 和 3 个 Fives,那么 2 个 Jacks 被移除,只有 2 个 Fives 被移除。我不知道该怎么做。我在下面的代码中对它们进行了设置和命名,如下所示:

Dim faces() As String = {"Ace", "Two", "Three", "Four", "Five",
         "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}

这是我的代码,这实际上只适用于玩家一,尽管也会有电脑玩家。

卡片组

Public Class DeckOfCards
   Private Const NUMBER_OF_CARDS As Integer = 52 ' number of cards
   Private deck(NUMBER_OF_CARDS - 1) As Card ' array of Card objects
   Private currentCard As Integer ' index of next Card to be dealt
   Private Shared randomNumbers As New Random() ' random number generator

   ' constructor fills deck of Cards
   Public Sub New()
      Dim faces() As String = {"Ace", "Two", "Three", "Four", "Five",
         "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}
      Dim suits() As String = {"Hearts", "Diamonds", "Clubs", "Spades"}
        currentCard = 0 ' set currentCard so first Card dealt is deck(0)


      ' populate deck array with Card objects
      For count = 0 To deck.GetUpperBound(0)
         deck(count) = New Card(faces(count Mod 13), suits(count \ 13))
      Next
   End Sub ' New

   ' shuffle deck of Cards with simple one-pass algorithm
   Public Sub Shuffle()
      ' after shuffling, dealing should start at deck(0) again
      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 currentCard <= deck.GetUpperBound(0) Then
         Dim lastCard 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 ' DealCard


End Class
4

1 回答 1

3

我认为你正在以错误的方式解决这个问题。即您的设计是错误的,因为您将值分配为字符串,这使得比较它们更加困难

我建议你有一个Card看起来像这样的课程:

Public Class Card

    Public Enum CardValue
        Ace = 1
        Two = 2
        'etc
        Jack = 11
        Queen = 12
        King = 13
    End Enum

    Public Enum CardSuit
        Clubs 
        Spades
        Hearts
        Diamonds 
    End Enum

    Public Property Value As CardValue
    Public Property Suit as CardSuit

    Public sub New(value as CardValue, suit as CardSuit)
        Me.Value = value
        Me.Suit = suit
    End Sub

End Class

每一场比赛都会简单地掌握List(Of Card)在他们手中。比较哪些是相同的很简单:

Dim Cards as List(Of Card) 'Players hand

If Cards.Select(Function(x) x.Value).Distinct.Count < Cards.Count Then
    'there are some duplicates in the list
    Dim duplicates = Cards.GroupBy(Function(x) x.Value).Where(Function(g) g.Count > 1).Select(Function(g) g.Key).ToList
    For Each i In duplicates
        Debug.WriteLine("Card value " + i.ToString + " is a match")
    Next
End If

您可以按如下方式创建和填充您的牌组:

Dim deck(51) As Card
Dim cardPosition As Integer = 0

'loop through each suit and each value in that suit setting one of the deck to that    
For Each suit As Card.CardSuit In [Enum].GetValues(GetType(Card.CardSuit))
    For Each value As Card.CardValue In [Enum].GetValues(GetType(Card.CardValue))
        deck(cardPosition) = New Card(value, suit)
        cardPosition += 1
    Next
Next
于 2013-05-15T13:31:02.900 回答