3 Replies - 333 Views - Last Post: 17 July 2014 - 04:14 AM Rate Topic: -----

#1 MORITSUKI  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-July 14

Need help with my poker hand analyzer!

Posted 15 July 2014 - 12:39 AM

Here is the code I have so far:
Public Class Form1
    'Dim CardString() As String = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}
    'Dim SuitsString() As String = {"S", "H", "C", "D"}

    Const suits As String = "CDHS"
    Const cardvalues As String = "A2345678910JQK"


    Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
        ListBox1.Items.Clear()
        Dim CardInfo() As String = TextBox1.Text.Split(","c) 'splits user-entered string into seperate strings using comma as delineator
        Dim cards(,) As Boolean = convertHands(CardInfo)

        ThreeOfaKind(cards)
        Pairs(cards)
        FourOfaKind(cards)
        LowStraight(cards)
        HighStraight(cards)
        Flush(cards)
    End Sub

    Private Function convertHands(CardInfo() As String) As Boolean(,)

        'declare table array of boolean values to represent all cards in a deck
        Dim result(5, 14) As Boolean

        'set all values in table array to false
        For i As Integer = 0 To 4
            For j As Integer = 0 To 13
                result(i, j) = False
            Next
        Next

        'for each card in the hand set one cell in the table to true
        For Each card As String In CardInfo
            card = card.Trim.ToUpper
            Dim rowIndex As Integer = suits.IndexOf(card.Last) + 1
            Dim columnIndex As Integer = cardvalues.IndexOf(card.First) + 1
            result(rowIndex, columnIndex) = True
        Next

        Return result
    End Function

    Sub FourOfaKind(CardInfo(,) As Boolean)
        For i As Integer = 0 To 13
            Dim counter As Integer = 0
            For j As Integer = 0 To 4
                If CardInfo(j, i) = True Then
                    counter += 1

                End If
            Next
            If counter >= 4 Then
                ListBox1.Items.Clear()
                ListBox1.Items.Add("Four of a Kind")
            End If
        Next
    End Sub

    Sub ThreeOfaKind(CardInfo(,) As Boolean)
        For i As Integer = 0 To 13
            Dim counter As Integer = 0
            For j As Integer = 0 To 4
                If CardInfo(j, i) = True Then
                    counter += 1
                End If
            Next
            If counter >= 3 Then
                ListBox1.Items.Add("Three of a Kind")
            End If
        Next
    End Sub

    Sub Pairs(CardInfo(,) As Boolean)
        For i As Integer = 0 To 13
            Dim counter As Integer = 0
            For j As Integer = 0 To 4
                If CardInfo(j, i) = True Then
                    counter += 1
                End If
            Next
            If counter >= 2 Then
                ListBox1.Items.Add("Pair")
            End If
        Next
    End Sub

    Sub LowStraight(CardInfo(,) As Boolean)
        Dim numberOfCardValuesInArow As Integer = 0
        Dim thereIsAnAce As Boolean
        Dim thereIsAtwo As Boolean

        'iterate over columns
        For i As Integer = 0 To 13

            'check if there is a cell in the column with the value True
            Dim foundCard As Boolean
            For j As Integer = 0 To 4
                If CardInfo(j, i) = True Then
                    foundCard = True
                End If
            Next

            'count consecutive cards
            If foundCard = True Then
                numberOfCardValuesInArow += 1

                'keep track if we find an ace or a two
                If i = 1 Then
                    thereIsAnAce = True
                ElseIf i = 2 Then
                    thereIsAtwo = True
                End If
            Else
                'if a card in the sequence is missing, reset the counter
                numberOfCardValuesInArow = 0
            End If

            'if we have the ace low straight, output message
            If numberOfCardValuesInArow = 5 And thereIsAnAce And thereIsAtwo Then
                ListBox1.Items.Add("Ace low straight")
            End If
        Next
    End Sub

    Sub HighStraight(CardInfo(,) As Boolean)
        Dim numberOfCardValuesInArow As Integer = 0
        Dim thereIsAnAce As Boolean
        Dim thereIsAking As Boolean

        'iterate over columns 'se
        For i As Integer = 2 To 13

            'check if there is a cell in the column with the value True
            Dim foundCard As Boolean = False
            For j As Integer = 1 To 4
                If CardInfo(j, i) = True Then
                    foundCard = True
                End If
            Next

            'count consecutive cards
            If foundCard = True Then
                numberOfCardValuesInArow += 1

                'keep track if we find an ace or a king
                If i = 1 Then 'an
                    thereIsAnAce = True
                ElseIf i = 13 Then
                    thereIsAking = True
                End If
            Else
                'if a card in the sequence is missing, reset the counter
                numberOfCardValuesInArow = 0
            End If
        Next

        If numberOfCardValuesInArow = 4 And thereIsAking Then

            'check if there is an ace 
            For j As Integer = 1 To 4

                'if we have the ace high straight, output message
                If CardInfo(j, 1) = True Then 's code
                    ListBox1.Items.Add("Ace high straight")
                End If
            Next
        End If

    End Sub

    Sub Flush(CardInfo(,) As Boolean)
        For j As Integer = 0 To 4
            Dim counter As Integer = 0
            For i As Integer = 0 To 13
                If CardInfo(j, i) = True Then
                    counter += 1

                End If
            Next
            If counter >= 5 Then
                ListBox1.Items.Clear()
                ListBox1.Items.Add("Flush")
            End If
        Next
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        ' Clears previous input and output and sets cursor to beginning of masked text box

        Textbox1.Text = ""
        ListBox1.Items.Clear()
        Textbox1.Focus()
        Textbox1.Selectionstart = 0
    End Sub





    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class


I am just wondering what's wrong with my LowStraight and HighStraight Sub. No matter which one I input, "AC, 2C, 3S, 4D, 5H" or "10J, JH, QH, KS, AH", as long as there is a A in the input I always get Low Straight, even though the later one suppose to display HighStraight. Can someone please let me know what's wrong?

Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Need help with my poker hand analyzer!

#2 MORITSUKI  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-July 14

Re: Need help with my poker hand analyzer!

Posted 15 July 2014 - 02:43 AM

Also, how can I make it so whenever I have a Four of a Kind input will only return "Four of a kind" but not "Three of a Kind" and "Pairs"?
Was This Post Helpful? 0
  • +
  • -

#3 markjc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-July 14

Re: Need help with my poker hand analyzer!

Posted 16 July 2014 - 08:25 PM

Just a thought, to help with the low straight, high straight issue, you my try implementing the Instr function to determine when in a combined string of chars the Ace is and if the POS is greater than a certain number it would differentiate between the two.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,541
  • Joined: 12-December 12

Re: Need help with my poker hand analyzer!

Posted 17 July 2014 - 04:14 AM

I've only had a brief look but
    If i = 1 Then
        thereIsAnAce = True

you are iterating 0..13 but are looking for an Ace at position 1, which is the second item. I assume you should be looking for it at position 0, the first position. Basically, arrays start at 0.

You are also iterating from 0..4. I'll guess this is the suits, but this means that there would be five suits (0..4).

In summary, I think you need to clarify how arrays and array-indexing works.

Dim arr(10) As Integer

creates 11 elements, indexed from 0 to 10.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1