【问题标题】:Poker Hand evaluation through LINQ通过 LINQ 进行扑克手评估
【发布时间】:2017-02-04 16:04:26
【问题描述】:

我正在创建一个扑克游戏 - 德州扑克(桌上有 5 张牌,我自己有 2 张牌)。

我已经创建了同花、同花和同花功能,但我一直在评估这手牌是否有:
1. 四种
2. 三样
3. 满座
4. 两对
5.一对

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

我创建了一个包含卡片列表的列表(7 张卡片) 类 Card 具有整数类型的属性 cardNumber(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-3 个 linq 查询以一种方法对整手牌进行排名 - 因为您想以扑克手牌顺序进行测试,所以您必须这样做。如果您要比较手牌,则返回一个字符串是不够的。如果 2 手有 1 对,那么对是什么决定了谁击败了谁。尤其是在德州扑克中,您还必须能够打破与高牌的平局(对子 Qs、Ace hi vs Pair Queens 9 hi)。
  • 冥王星。谢谢你的评论。你说的很有道理,我完全同意。我应该从一开始就提到我只是想构建一个简单的游戏版本。为了这个特殊的目的,我知道我的手就足够了,而不是与任何其他手比较。
  • 卡片是如何存储的?卡等级如何,是 1 到 13 吗?
  • 卡片存储在列表中。卡有属性 cardNumber。 Ace = 0, Two = 1, Three = 2... Queen = 11, King = 12。使用 Do Loop 我创建了一个整数列表来保存这些值。使用 LINQ,我浏览了列表并按值分组。
  • 您需要一个实际的卡片类({Rank, Suit, Value}),该值不够,否则您将无法确定冲洗。您可能还希望首先对 5 张牌进行排序,因为 7 张牌增加了寻找顺子和确定同花顺的复杂性

标签: vb.net linq poker


【解决方案1】:

因为无法发表评论。

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

然后 Array.ForEach(...) 用于 Groups() 并带有一个内联 If[...] 来计算每个组中的出现次数并测试它是否具有特定的匹配组合。


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

【讨论】:

    【解决方案2】:

    我想通了。我确信它可以以更清洁的方式完成,但它对我有用。在我的编程发现的这个阶段——这是实现的下一个里程碑。感谢冥王星。欣赏它。

    AKind(IntegerList As List(Of Integer)) As String 的公共函数 暗淡结果为字符串 =“是” 将 groupedIntegerList 调暗为新列表(整数)

        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
    

    【讨论】:

      猜你喜欢
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2017-03-26
      相关资源
      最近更新 更多