卡匹配游戏..如何比较卡?

时间:2013-05-15 13:17:52

标签: .net vb.net comparison-operators playing-cards

所以我的设计是有26个图片框(代码只显示到10 ......你明白了)向用户显示他们的“手”。现在我如何检查这些图像框的双打,然后删除那些双打到清除堆?所以,假设有2个Jacks和3个5s,那么2个Jacks被移除,只有2个5个被移除。我无法弄清楚如何做到这一点。我在下面的代码中设置并命名如下:

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

这是我的代码,这只适用于玩家一,虽然也会有一台电脑播放器。

Deckofcards

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

1 个答案:

答案 0 :(得分: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