1

私は自分でポーカー ゲームを作っていて、残りのコードは持っていますが、7 枚のカードを検索してストレート (フィールド カード 5 枚とプレイヤーの手札に 2 枚のカード) があるかどうかを確認する方法がわかりません。 、カードは数字です (1 はエース、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

前もって感謝します!

4

1 に答える 1

0

カードに数値を使用している場合は、データ型に文字列ではなく整数を使用する必要があります。このように機能するようにメソッドを設計することを検討してください。

'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
于 2012-10-02T21:18:29.247 に答える