2013-05-15 54 views
0

所以我的设计是有26个图片框(代码只显示到10 ..你得到的想法)向用户显示他们的“手”。现在,我该如何检查这些图像盒的双打,然后将这些双打移除到一个清除的堆?所以我们假设有2个插孔和3个5插孔,那么2个插孔会被移除,只有2个插孔被移除。我无法弄清楚如何做到这一点。我让他们建立和下面如下在我的代码命名为:卡匹配游戏..如何比较卡?

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 

回答

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 
+0

你可以让你的循环更高效(并删除'如果')如果你从'i + 1'(和'i'到'Cards.Count-2')循环'j'而不是......你不确定你从'string'或'int'改变为'enum'这个问题有显着的不同,但它绝对是更好的代码! –

+0

@dan - 约定 - 修改了linq查询可能效率稍高 –

+0

@MattWilko感谢您的帮助,我通过替换卡类错误 Option Strict On禁止隐式转换从“字符串”到“DeckOfCardsTest.Card.CardValue”。“和“Error Option Strict On禁止从'String'到'DeckOfCardsTest.Card.CardSuit'的隐式转换。” (0) deck(count)=新卡片(面孔(计数Mod 13),套装(count 13)) 下一页下一页 – user2382959