hi

the problem is when i use my own word eg here "jose" the results are correct but when i use random string it doesnt work the same

here's my code

Imports System.Text

Public Class Form2
    Dim guess As String = " "
    Dim bulls As Integer = 0
    Dim cows As Integer = 0
    Dim str As String
    Dim str2 As String
    Dim i As Integer
    Dim j As Integer
    Dim count As String
    Dim str3(100) As Char
    Dim str4(100) As Char


    Private Function RandomString(ByVal size As Integer, ByVal lowerCase As Boolean) As String
        Dim builder As New StringBuilder()
        Dim random As New Random()
        Dim ch As Char
        Dim i As Integer
        For i = 0 To size - 1
            ch = Convert.ToChar(Convert.ToInt32((26 * random.NextDouble() + 65)))
            builder.Append(ch)
        Next i
        If lowerCase Then
            Return builder.ToString().ToLower()
        End If
        Return builder.ToString()
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        str = TextBox1.Text
        check(str)
        ListBox1.Items.Add(str)
        str2 = str.ToCharArray()
        guess = RandomString(4, True)
        'guess = "jose"

        checkbulls()

        checkcows()

        ListBox2.Text = " "
        ListBox3.Text = " "

    End Sub
    Public Sub check(ByVal str1 As String)
        str = str1
        If (str.Length() <> 4) Then
            MsgBox("OOps enter only 4 letters")
            str = "     "

        End If

    End Sub
    Public Sub checkbulls()



        For i = 0 To 3


            str3(i) = str2(i)
            str4(i) = guess.Chars(i)


            If (str3(i) = str4(i)) Then
                bulls = bulls + 1
                str3(i) = "#"
                str4(i) = "!"
            End If
        Next

        ListBox2.Items.Add(bulls)
        won()
        bulls = 0
    End Sub
    Public Sub checkcows()
        For i = 0 To 3
            For j = 0 To 3
                If (str3(i) = str4(j)) Then
                    cows = cows + 1
                    str3(i) = "#"
                    str4(j) = "!"

                End If
            Next
        Next
        ListBox3.Items.Add(cows)
        won()
        cows = 0
    End Sub
    Public Sub won()
        If (bulls = 4) Then
            MsgBox("congrats")
            If (count = 0) Then
                MsgBox("you won in first attempt")
                Me.Close()

            Else

                MsgBox("you won in " + count + " attempts")
                Me.Close()
            End If

        Else
            count = count + 1
        End If

    End Sub




    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MsgBox("the word was " + guess + " try again")
        Me.Close()

    End Sub

End Class

please help

i guess every time i click button a new string is generated
how do i avoid that? may be a file?

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.