通过LINQ进行扑克手牌评估

时间:2017-02-04 16:04:26

标签: vb.net linq poker

我正在制作扑克游戏 - 德州扑克(桌上有5张牌,我自己有2张牌)。

我已经创建了同花顺,直线和同花顺功能,而且我仍然坚持评估手是否有:
1.四种 2.三种之一 3.满堂红 4.两对
5.一对

我相信我可以为上述所有内容编写一个函数,它将返回一个相应的字符串。

我创建了一个包含卡片列表的列表(7张卡片) Class Card的属性cardNumber为Integer类型(Ace = 1,Two = 2,Three = 3等)

这是我的功能:

Public Shared Function ofAKind(hand As List(Of Card)) As String
    Dim result As String = ""
    Dim counter As Integer
    Dim IntegerList As New List(Of Integer)
    'creating a list of integers that are representing faces of cards
    Do
        IntegerList.Add(hand.Item(counter).cardNumber)
        counter += 1
    Loop Until counter = hand.Count

    Dim groupedIntegers = From Int In IntegerList
                          Group By Int
                              Into grouping = Group, Count()

    'and here is my problem: how can I make such a grouping? below is just pseudocode.
    'When using a debugger, I see that it groups them well. It is just that I do not know
    'how to use LINQ to extract that grouping into the below if statement and get a corresponding string. 


    'if grouping = 4 Then
    'result = "Four of a kind"
    'if grouping = 3 andAlso grouping = 2 Then
    'result = "Full House"
    'if grouping = 2 andAlso grouping = 2 Then
    'result = "Two Pairs"
    'if grouping = 2 Then
    'result = "Pair"


    Return result
End Function

2 个答案:

答案 0 :(得分:0)

我明白了。我相信它可以以更清洁的方式完成,但它对我有用。在我的编程发现的这个阶段 - 这是实现的下一个里程碑。感谢Plutonix。欣赏它。

公共功能ofAK(IntegerList As List(Of Integer))As String         昏暗的结果As String =“YES”         Dim groupedIntegerList As New List(Of Integer)

    Dim groupedIntegers = From Int In IntegerList
                          Group By Int
                          Into LOL = Group, Count()

    'creating another list (I am sure there is a cleaner way, but I don't know it yet)
    For Each e In groupedIntegers
        groupedIntegerList.Add(e.Count)
    Next

    If groupedIntegerList.Contains(3) And groupedIntegerList.Contains(2) Then
        result = "Fullhouse!"
    ElseIf groupedIntegerList.Contains(4) Then
        result = "Four of a kind!"
    ElseIf groupedIntegerList.Contains(3) Then
        result = "Three of a kind"
    ElseIf groupedIntegerList.Contains(2) Then
        result = "Pair!"
    End If

    'ugly way to search for two pairs (but it works)

    If result = "Pair!" Then
        Dim searchingForTwoPairs = From int In groupedIntegerList
                                   Where int > 1
                                   Group By int
                                       Into LOL2 = Group, Count()

        Dim twoPairsList As New List(Of Integer)
        For Each e In searchingForTwoPairs
            twoPairsList.Add(e.Count)
        Next

        If twoPairsList.Contains(2) Or twoPairsList.Contains(3) Then
            result = "Two pairs!"
        End If
    End If

    Return result
End Function

答案 1 :(得分:0)

由于缺乏评论。

可能String.Concat所有卡片值一起(每个卡片之间有空格)并使用匹配代码为“\ d”的Regex.Matches(...)来匹配数字

然后Array.ForEach(...)为具有内联If[...]的Groups()计算每个组中的匹配项,并测试它是否具有特定的匹配组合。

这可能有点乏味,而且很长的在线Linq,但只是一个想法:p