0

I'm doing Hangman Game in Visual Basic. I'm looking for typing a letter in a TextBox and clicking a button to check out. If that letter is in String, it will return position but when the word has two matches... How could I do it?

Next code only return the first match, I mean, only position of the first "A".

Dim palabra As String = "PALABRA"

Private Sub BtnComprobar_Click(sender As Object, e As EventArgs) Handles BtnComprobar.Click
    If txtComprobar IsNot "" Then
        Dim letra As String = UCase(txtComprobar.Text)

        If palabra.IndexOf(letra) > -1 Then
            Select Case palabra.IndexOf(letra)
                Case 0
                    Lbl1.Text = letra
                    LblP.ForeColor = Color.Red
                Case 1
                    Lbl2.Text = letra
                    LblA.ForeColor = Color.Red
                Case 2
                    Lbl3.Text = letra
                    LblL.ForeColor = Color.Red
                Case 4
                    Lbl4.Text = letra
                Case 5
                    Lbl5.Text = letra
                    LblB.ForeColor = Color.Red
            End Select
        Else
            errores += 1
            txtErrores.Text = CStr(errores)
        End If
        txtComprobar.Text = ""
    End If
End Sub

Thank for your help

EDIT: Sorry, I didn't say it, I can't use arrays.

Mrquestion
  • 771
  • 1
  • 6
  • 16
  • 1
    `IndexOf()` has [numerous overloads](http://stackoverflow.com/a/19441248/1070452), `IndexOf(char, n) ` will find the first index of char after `n`. – Ňɏssa Pøngjǣrdenlarp Sep 28 '16 at 15:53
  • I did that: `ElseIf palabra.IndexOf(letra, indice) > -1 Then Select Case palabra.IndexOf(letra, indice)` But it isn't finding the last match. – Mrquestion Sep 28 '16 at 16:38

3 Answers3

0

Since you don't look like you know much of the language. I'll make you a sample that might help you.

You could see your problem the other way around, instead of looking if the choosen letter is in the word, look if each character of the word is the choosen letter.

If palabra.IndexOf(letra) > -1 Then
    If palabra(0) = letra Then
        Lbl1.Text = letra
    End If

    If palabra(1) = letra Then
        Lbl2.Text = letra
    End If

    If palabra(2) = letra Then
        Lbl3.Text = letra
    End If

    If palabra(3) = letra Then
        Lbl4.Text = letra
    End If

    If palabra(4) = letra Then
        Lbl5.Text = letra
    End If

    If palabra(5) = letra Then
        Lbl6.Text = letra
    End If
Else
    errores += 1
    txtErrores.Text = CStr(errores)
End If

This would be much easier with arrays of label and loops.

the_lotus
  • 12,668
  • 3
  • 36
  • 53
0

Add this function to your code:

 Public Function GetIndexes(ByVal SearchWithinThis As String, ByVal SearchForThis As String) As List(Of Integer)
        Dim Result As New List(Of Integer)

        Dim i As Integer = SearchWithinThis.IndexOf(SearchForThis)

        While (i <> -1)
            Result.Add(i)
            i = SearchWithinThis.IndexOf(SearchForThis, i + 1)
        End While

        Return Result
    End Function

And call the function in your code:

Dim Indexes as list(of Integer) = GetIndexes(palabra, letra)

Now GetIndexes will find all indexes of the letter you are looking for and put them in the list Indexes.

Cal-cium
  • 654
  • 12
  • 22
-1

Put your string into an array and loop through each letter. Return the position when there is a match but continue looping until you have gone through the whole array.