【发布时间】: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 张牌增加了寻找顺子和确定同花顺的复杂性