1

公平的警告,我将为玩家 1 发布我的整个代码,只是因为我不确定需要什么才能继续。我迫切需要帮助。有 49 张牌,玩家 1 将得到 16 张牌。当我发牌时,有 16 张牌显示给用户。

我的问题是:我如何检查这 16 张牌的双倍,然后取出这些牌并将它们放入弃牌堆?

Public Class DeckOfCardsTest
    Dim playercards As Integer = 16
    Dim playermatches As Integer
    Dim comp1cards As Integer = 16
    Dim comp1matches As Integer
    Dim comp2cards As Integer = 17
    Dim comp2matches As Integer


    Private deck As New DeckOfCards() ' create the deck of cards




Public Class DeckOfCards
    Private Const NUMBER_OF_CARDS As Integer = 49 ' 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 ' DeckOfCards

如何让这个 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

以下是错误:

Error   1   Overload resolution failed because no accessible 'Select' can be called with these arguments: Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of Card, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of Card, Integer, TResult)'.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of Card, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of Card, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': 'Value' is not a member of 'DeckOfCardsTest.Card'. Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of Card, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. 

Error   2   Data type(s) of the type parameter(s) in extension method 'Public Function GroupBy(Of TKey)(keySelector As System.Func(Of Card, TKey)) As System.Collections.Generic.IEnumerable(Of System.Linq.IGrouping(Of TKey, Card))' defined in 'System.Linq.Enumerable' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.

Error   3   'Value' is not a member of 'DeckOfCardsTest.Card'.

Error   4   Option Strict On disallows implicit conversions from 'Object' to 'System.Collections.Generic.IEnumerable(Of DeckOfCardsTest.Card)'. 

Error   5   Option Strict On disallows late binding.    
4

1 回答 1

1

我对老处女不熟悉,所以请原谅我的无知。

如果我正确理解你的问题,你想检查玩家的手牌,移除双打,然后将它们放入弃牌堆?

如果是这种情况,您的 Deck 对象将需要有另一个 Card 对象数组(或最好是列表)来表示丢弃堆。然后,在你发牌后,循环浏览玩家手中的每张牌,并将其与其他每张牌(不包括它自己)进行比较。由于 Card 是一个对象,如果两张卡相同,它们将指向同一个对象。记下相同卡片的索引,然后将它们添加到您的弃牌堆中,然后将它们从玩家手中移除。

编辑:根据您的代码更新,这就是我要做的。我没有检查语法,但我认为它是正确的。如果没有,请告诉我,我会修复它。

Dim Cards As New List(Of Card) 'Players hand
Dim discardPile As New List(Of Card) ' discard pile

For i As Integer = 1 to 16
   Cards.Add(deck.DealCard())
Next i

If Cards.Select(Function(x) x).Distinct.Count < Cards.Count Then
'there are some duplicates in the list
    Dim duplicates As List(Of Card) = Cards.GroupBy(Function(x) x.GwtValue).Where(Function(g) g.Count > 1).SelectMany(Function(g) g.ToArray).ToList

    ' This part adds the duplicates to the discard pile
    discardPile.AddRange(duplicates)
    ' This is the part that would remove the duplicates from the player's hand       
    Cards.RemoveAll(Function(y) duplicates.Contains(y))

End If
于 2013-05-19T16:06:03.913 回答