I have decided to create Bulls and Cows game for my project. I have been checking for it's correctness. But it behaves bad in some cases. Like when I have already guessed the word it doesn't respond right. This happened rarely but I can conclude that it still is not right.I get words from a notepad file randomly.

Can anyone tell me why it behaves erratic.

some initializations
dim str as string
str = TextBox1.Text
str2 = str.ToCharArray()
Dim str3(100) As Char
Dim str4(100) As Char

Public Sub checkbulls(ByVal guess1 As String)
        Try
            'Throw (New MyException("please select a choice"))
            guess = guess1
            Dim i As Integer
            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
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Sub checkcows()
        Try
            Dim i As Integer
            Dim j As Integer

            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)

            cows = 0
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

Sorry, I don't know that game :$

You have an initialization:

str = TextBox1.Text
str2 = str.ToCharArray()

What does it do? I mean what's in TextBox1.Text?

Line 7: guess = guess1 Why?
Line 4: Public Sub checkbulls(ByVal guess1 As String) What's in the guess1 string?

Hi thank you for replying

Bulls and Cows is a guessing game. guess1 as parameter takes the word to be guessed. Textbox is on the form and allows you to enter your try to guess the word.

the game: if the computer has selected its guess as "work"..if the user puts "abcd" bulls=0 cows=0 as it doesn't contain w or o or r or k. if the word guessed by the user has a letter in the computer guess at the same position as that of user guess then bulls=1. if the letter is there but not at the same position as computer guess cows=1.

eg: "work" user guess is "omac" bulls=0 cows=1 as o is there in work but not in second position of "omac".if user puts "week" bulls=2 cows=0 as w and k are in the same position as "work"

I hope it helps

i have solved the problem. Now another problem. I am using a notepad file(wordlist1.txt) to get words for guessing. But the problem is that i have to type the words in single line separated by comma.I want to use the file with words typed on new lines.

my code is

Dim i As Integer
                Dim randomclass As New Random
                Dim num As Integer = randomclass.Next(15)
                Dim fs As New FileStream("c:\wordlist1.txt", FileMode.Open, FileAccess.Read)
                'declaring a FileStream to open the file named file.doc with access mode of reading
                Dim d As New StreamReader(fs)
                'creating a new StreamReader and passing the filestream object fs as argument
                d.BaseStream.Seek(0, SeekOrigin.Begin)
                'Seek method is used to move the cursor to different positions in a file, in this code, to 
                'the beginning
                While d.Peek() > -1
                    'peek method of StreamReader object tells how much more data is left in the file
                    str5 = d.ReadLine()

                    'displaying text from doc file in the RichTextBox
                    str6 = str5.Split(",") 'str6 is an array of strings, str5 is a string variable

VB.NET has a powerful My namespace, use that instead.

I put the code in an event handler but you can put it in a separate procedure or directly in your code. I declared file name and word array as global variables.

Private Words() As String
Private WordFileName As String = "C:\Temp\words.txt"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim FileText As String ' The whole content of the word file
    Dim Separator(1) As String

    ' Check that file exists
    If My.Computer.FileSystem.FileExists(WordFileName) Then
        ' Read the file
        FileText = My.Computer.FileSystem.ReadAllText(WordFileName)
        ' Get lines by splitting file content
        Separator(0) = Environment.NewLine ' Separator "character" is NewLine string
        ' Split and remove empty strings
        Words = FileText.Split(Separator, System.StringSplitOptions.RemoveEmptyEntries)
    Else
        ' Error, couldn't find the word file
    End If

End Sub

And thanks for explaining the game!

HTH

This article has been dead for over six months. Start a new discussion instead.