搜索组中的连续数字

时间:2012-10-02 20:57:16

标签: vb.net

我正在为自己制作一个扑克游戏,我有其余的代码,但我似乎无法找到如何搜索七张卡片,看看是否有一个直线(5张现场牌和2张牌玩家的手牌,卡是数字(1是ace,2是2,等等.11是杰克,12是女王等)到目前为止我有这个:

Function isStraight(ByVal Player As String)
    Dim h1, h2, h3, h4, h5 As String
    h1 = 0
    h2 = 1
    h3 = 2
    h4 = 3
    h5 = 4
    Dim z1, z2 As String
    If Player = "P1" Then
        z1 = P1Card1
        z2 = P1Card2
    ElseIf Player = "P2" Then
        z1 = P2Card1
        z2 = P2Card2
    End If 
    Dim cntr As Integer = 0
    Do
        cntr = cntr + 1
        h1 = h1 + 1
        h2 = h2 + 1
        h3 = h3 + 1
        h4 = h4 + 1
        h5 = h5 + 1
        If A(FC1, FC2, FC3, FC4, FC5) Or A(FC5, FC1, FC2, FC3, FC4) Or A(FC4, FC5, FC1, FC2, FC3) Or A(FC3, FC4, FC5, FC1, FC2) Then

        End If
    Loop

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果您使用卡的数值,则应使用Integer而不是String作为数据类型。考虑设计您的方法以这种方式工作:

'Accept only cards, use a specific Boolean return type
'IEnumerable(Of Integer) will still allow you to pass arrays to this function
Function isStraight(ByVal River As IEnumerable(Of Integer), ByVal Hand As IEnumerable(Of Integer)) As Boolean

    'Make sure Option Infer is On
    Dim Cards = River.Concat(Hand)

    'Ace can be high or low, so add a high value to the list if you have any aces
    If Cards.Contains(1) Then Cards = Cards.Concat(New Integer() {14}) 

    'It will be MUCH easier to find consecutive cards if they are sorted in order to start with, and we don't care about pairs so limit us to unique number cards
    'The "Function(c) c" here is called a Lambda Expression. This lambda expression tells the OrderBy() method to compare items in the collection for sorting purposes using simple ascending order. 
    Cards = Cards.OrderBy(Function(c) c ).Distinct()

    'If this count gets to five consecutive cards, we have a straight
    Dim StraightCount As Integer 

    'Initialize to a card that can never be consecutive, so first time through the loop will reset the counter
    Dim PreviousCard Integer = -1 

    For Each card As Integer In Cards
       'If the prior card is one less than current card, they are consecutive: add 1 to StraightCount
       'If they are not consecutive, set back to 1 (this is the 1st card in a new potential straight)
       StraightCount = If(card - PreviousCard = 1, StraightCount + 1, 1)

       'If we reach 5, don't bother finishing: Return True immediately
       If StraightCount = 5 Then Return True

       'Set this card as the prior card for the next loop iteration
       PreviousCard = card
    Next card

    'Did not find a straight
    Return False
End Function