Member Avatar for Griff0527

I would like to start this post off with, I am not looking for someone to do my homework for me. If I was looking for someone to "do it for me", I would go to one of the sites where you can pay someone to do your work for you. I am trying to learn VB and C#. This is a VB.Net assignment. However, I am looking for guidance or someone to point out my errors (and there are SEVERAL of them from the odd results my game is producing. I am going to paste the assignment requrirements, then my code, and finally my list of error/issues I am having. Please assist me if you can. This is due tonight and I am going to continue working on it.

Assignemnt: Create a 5 card poker game where each card once used, cannot be used again in the same hand. When all 5 cards are shown, allow the user to discard and redraw any card(s) up to all 5. Only allowed to change cards once per hand. Inform the user of the result in a dialogbox (MsgBox). After clearing the result, allow the user to draw a new hand or exit the program.

Public Class PokerGame
    Dim Cards(4) As String
    Dim countRank(12) As Integer
    Dim countSuit(3) As Integer
    Dim card As String = ""
    Dim suit As String = ""

    Function DrawCard(ByVal index As Integer) As String
        Dim num As String
        'Dim card As String = ""

        Cards(0) = ""
        Cards(1) = ""
        Cards(2) = ""
        Cards(3) = ""
        Cards(4) = ""
        Randomize()
        'rank cards by precidence
        num = (Int(Rnd() * 13)).ToString
        countRank(CInt(num)) = countRank(CInt(num)) + 1
        If num = "12" Then
            card = "King of"
        ElseIf num = "11 of" Then
            card = "Queen of"
        ElseIf num = "10" Then
            card = "Ten of"
        ElseIf num = "9" Then
            card = "Nine of"
        ElseIf num = "8" Then
            card = "Eight of"
        ElseIf num = "7" Then
            card = "Seven of"
        ElseIf num = "6" Then
            card = "Six of"
        ElseIf num = "5" Then
            card = "Five of"
        ElseIf num = "4" Then
            card = "Four of"
        ElseIf num = "3" Then
            card = "Three of"
        ElseIf num = "2" Then
            card = "Two of"
        ElseIf num = "1" Then
            card = "Ace of"
        End If

        'find suit
        num = (Int(Rnd() * 4)).ToString
        countSuit(CInt(num)) = countSuit(CInt(num)) + 1
        If num = "1" Then
            suit = " Spades"
        ElseIf num = "2" Then
            suit = " Hearts"
        ElseIf num = "3" Then
            suit = " Diamonds"
        ElseIf num = "4" Then
            suit = " Clubs"
        End If

        card = card & suit

        Return card
    End Function
    Private Sub btn_Draw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Draw.Click
        countRank = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        countSuit = {0, 0, 0, 0}
        card1.Text = DrawCard(0)
        card2.Text = DrawCard(1)
        card3.Text = DrawCard(2)
        card4.Text = DrawCard(3)
        card5.Text = DrawCard(4)

        'check for duplicate card
        If card1.Text = card2.Text Or
            card1.Text = card3.Text Or
            card1.Text = card4.Text Or
            card1.Text = card5.Text Then
            DrawCard(0)
            card1.Text = DrawCard(0)
        End If
        If card2.Text = card3.Text Or
            card2.Text = card4.Text Or
            card2.Text = card5.Text Then
            DrawCard(1)
            card2.Text = DrawCard(1)
        End If
        If card3.Text = card4.Text Or
            card3.Text = card5.Text Then
            DrawCard(2)
            card3.Text = DrawCard(2)
        End If
        If card4.Text = card5.Text Then
            DrawCard(3)
            card4.Text = DrawCard(3)
        End If

        btn_Draw.Enabled = False
        btn_Discard.Enabled = True


    End Sub

    Private Sub PokerGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        card1.Text = "Welcome"
        card2.Text = "To"
        card3.Text = "My"
        card4.Text = "Poker"
        card5.Text = "Game"
        btn_Discard.Enabled = False
    End Sub

    Private Sub btn_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Clear.Click
        card1.Text = ""
        card2.Text = ""
        card3.Text = ""
        card4.Text = ""
        card5.Text = ""
        Cards(0) = ""
        Cards(1) = ""
        Cards(2) = ""
        Cards(3) = ""
        Cards(4) = ""
        btn_Discard.Enabled = False
        btn_Draw.Enabled = True
        If CheckBox1.CheckState = 0 Then
            CheckBox1.CheckState = 1
        End If
        If CheckBox2.CheckState = 0 Then
            CheckBox2.CheckState = 1
        End If
        If CheckBox3.CheckState = 0 Then
            CheckBox3.CheckState = 1
        End If
        If CheckBox4.CheckState = 0 Then
            CheckBox4.CheckState = 1
        End If
        If CheckBox5.CheckState = 0 Then
            CheckBox5.CheckState = 1
        End If
    End Sub

    Private Sub btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Exit.Click
        End
    End Sub

    Private Sub btn_Discard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Discard.Click
        If CheckBox1.CheckState = 0 Then
            card1.Text = DrawCard(0)
        End If
        If CheckBox2.CheckState = 0 Then
            card2.Text = DrawCard(1)
        End If
        If CheckBox3.CheckState = 0 Then
            card3.Text = DrawCard(2)
        End If
        If CheckBox4.CheckState = 0 Then
            card4.Text = DrawCard(3)
        End If
        If CheckBox5.CheckState = 0 Then
            card5.Text = DrawCard(4)
        End If

        'display resultrmation
        Dim result As String = ""
        Array.Sort(countRank)
        Array.Reverse(countRank)

        For i = 0 To 4
            If countRank(i) = 4 Then
                result = "you have 4 of a kind!" & vbLf
            ElseIf countRank(i) = 3 Then
                result = "You have 3 of a kind!" & vbLf
            ElseIf countRank(i) = 2 Then
                For j = 0 To 3
                    If countRank(j) = 3 Then
                        result = "You have a Full House!" & vbLf
                    ElseIf countRank(j) = 2 Then
                        result = "You have Two Pair!" & vbLf
                    Else
                        result = "You have a Pair!" & vbLf
                    End If
                Next
            End If
        Next
        For i = 0 To countRank.Length - 1
            If countRank(i) = countRank(i) + 1 Then
                For j = 0 To countSuit.Length - 1
                    If countSuit(j) = 5 Then
                        result = "You have a Straight Flush " & vbLf
                    End If
                Next
            End If
        Next

        For i = 0 To countSuit.Length - 1
            If countSuit(i) = 5 Then
                result = "You have a flush " & card & vbLf
            End If
        Next

        For i = 0 To countSuit.Length - 1
            If countSuit(i) = 5 Then
                result = "You have a Straight" & vbLf
            End If
        Next


        If result = "" Then
            result = "I'm sorry, but you only have a high card hand."
        End If

        btn_Discard.Enabled = False

        MsgBox(result)
    End Sub

End Class

Here are my issues:
1)Randomly, a card will show up with mulitple suits.
2)My logic for determining the hand always says either "a pair" or "high card hand", never picks up 3 of a kind or greater.
3) ** Recently fixed ** I think... was unable to stop the game from dealing the same card twice in one hand. This may still be broken in my duplication logic.

If you can help me find my errors or point out changes I should make, please let me know. Likewise, if I should .zip the roject and attach it, please let me know that as well. I have been scratching my head and reading/researching "Visual Basic 2010 - Step by Step" for four days now, and have finally decided to ask for assistance.

Thank you.

P.S. I have another version which is FAR less complete where I tried using a different logic stucture and array stucture, but I ran into more problems even earlier in the coding process and went back to my original code.

Member Avatar for Griff0527

Follow on question:
In the If - Else statements in the beginning (which work), can I write this as a Select Case structure? I know how to use select case in situations such as:
Select Case listbox1.SelectedIndex
Since this uses the selected positions of 0, 1, 2, 3, etc. But can you use a Select Case to check against the integer value of a variable; in this case "card"?

Member Avatar for Griff0527

I already turned this assignment in "broken", but if anyone sees this post and can help me understand my errors, I'd appreciate it. I'd like to actually learn Visual Basic, not just trudge along well enough to pass the class, but instead well enough to make use of the language.
Thank you,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.