我正在写一个扑克游戏,它会处理不同的牌,然后将处理后的牌添加到列表中。它是一张卡片列表,每张卡片都有一个值,cardno,它存储了这个号码,我需要做的是检查列表是否说直(4,5,6,7,8个俱乐部)或满屋( 5个球杆,5个钻石,7个心形,7个铲子,7个钻石)等等。
继承我的卡片代码:
Public Class card
Public suite As String
Public cardlet As String
Public cardno As Integer
End Class
然后我将其添加到列表中
dim dealtcards as new list(Of card)
所以我需要检查发牌卡是否包含一对,两对,三种等等。
答案 0 :(得分:0)
你应该做的是创造不同的'功能',看看手是否包含某种组合。您可以使用此功能在数组中搜索值:
Public Shared Function FindValueFromArray(ByVal Values As Object(), ByVal valueToSearch As Object) As Boolean
Dim retVal As Boolean = False
Dim myArray As Array = DirectCast(Values, Array)
If Array.IndexOf(myArray, valueToSearch) <> -1 Then
retVal = True
End If
Return retVal
End Function
来自here。
答案 1 :(得分:0)
听起来你需要另一个班级...... Public Class Hand
。
它可以在内部包含一系列卡片,但重点是它提供的公共界面。操纵卡片列表的方法(添加到手,从手中删除)和检查手的“状态”的方法(可以返回已知状态的强类型枚举)。也许与此不同(我的VB 非常生锈,所以这主要是伪代码):
Public Class Hand
Private cards As IList(Of card)
' A method to add a card to the hand, which should check if the hand can hold another card or not
' A method to remove a card from the hand, perhaps?
' Other methods representing actions that can be performed, such as folding the hand
Public Function GetHandStatus As HandStatus
If HandIsFlush() Then
Return HandStatus.Flush
Else If HandIsStraight() Then
Return HandStatus.Straight
End IF
End Function
Private Function HandIsFlush As Boolean
' loop through cards, return true if they are all the same suit
End Function
Private Function HandIsStraight As Boolean
' iterate through sorted-by-value cards, return false if more than 1 value separates any two
End Function
End Class
这样做是将此应用程序的业务逻辑的每个元素减少到一个函数来处理该逻辑。如果类开始变大,你可以进一步重构它,可能会将HandStatus
从枚举变为抽象类,子类表示状态,并将逻辑移到那里。
答案 2 :(得分:0)
我要做的是创建和lambda的字典,其中键是你拥有的手的类型,而lambda是用于确定手牌时间的实际值。
Dim fullHouse = Function(cards as List(of Card)
//logic to check
return True or False
End Function
Dim rules as New Dictionary(of String, func(of List(Of Card,Boolean))
rules.Add("FullHouse",fullHouse)
Dim hand as List(of Card)
DIm typeOfHand _
rules.
Keys.
Select(Function(k)
If rules.Item(k)(hand) = True then Return k
Return string.Empty).
Where(Function(r) Not String.IsEmptyOrNull(r)).
FirstOrDefault()
Dim score = hand.Select(h) h.CardNo).Sum()
当然,您可以使用更多OOP扩展此示例,但我认为使用函数方法对lambdas和Linq的使用将使您更容易理解和维护代码。