简单的老女仆游戏,不是那么简单吗?

时间:2013-05-19 15:53:34

标签: vb.net

公平警告,我将发布我的整个播放器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.    

1 个答案:

答案 0 :(得分:1)

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

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

如果是这种情况,您的Deck对象将需要另一个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