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 はオブジェクトであるため、2 枚のカードが同一である場合、それらは同じオブジェクトを指します。同じカードのインデックスを書き留めてから、それらを捨て札に加え、プレイヤーの手札から取り除きます。

編集:コードの更新に基づいて、これが私がすることです。構文は確認していませんが、正しいと思います。そうでない場合は、お知らせください。修正します。

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