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
你可以让你的循环更高效(并删除'如果')如果你从'i + 1'(和'i'到'Cards.Count-2')循环'j'而不是......你不确定你从'string'或'int'改变为'enum'这个问题有显着的不同,但它绝对是更好的代码! –
@dan - 约定 - 修改了linq查询可能效率稍高 –
@MattWilko感谢您的帮助,我通过替换卡类错误 Option Strict On禁止隐式转换从“字符串”到“DeckOfCardsTest.Card.CardValue”。“和“Error Option Strict On禁止从'String'到'DeckOfCardsTest.Card.CardSuit'的隐式转换。” (0) deck(count)=新卡片(面孔(计数Mod 13),套装(count 13)) 下一页下一页 – user2382959