0

したがって、私のデザインは、ユーザーに「手」を表示するために、26 個の画像ボックス (コードは 10 個までしか表示されません) を持つことです。これらの画像ボックスでダブルスをチェックし、それらのダブルスをクリアされたパイルに削除するにはどうすればよいですか? たとえば、2 つのジャックと 3 つの 5 がある場合、2 つのジャックが削除され、2 つの 5 のみが削除されます。これを行う方法がわかりません。次のように、以下のコードでそれらを設定して名前を付けています。

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

これが私のコードです。これは実際にはプレーヤー 1 用ですが、コンピューター プレーヤーもあります。

デッキオブカード

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 に答える