need help with hangman game
In my hangman game if i have a word with two of the same letters only the first of the two will show up. I have the labels invisible until you guess it right, here is the code:
Private Sub btnguess_Click()
If txtguess.Text = letter1 Then
Label1.Visible = True
ElseIf txtguess.Text = letter2 Then
Label2.Visible = True
ElseIf txtguess.Text = letter3 Then
Label3.Visible = True
ElseIf txtguess.Text = letter4 Then
Label4.Visible = True
ElseIf txtguess.Text = letter5 Then
Label5.Visible = True
ElseIf txtguess.Text = letter6 Then
Label6.Visible = True
ElseIf txtguess.Text = letter7 Then
Label7.Visible = True
ElseIf txtguess.Text = letter8 Then
Label8.Visible = True
Re: need help with hangman game
If you look at your logic, you only check for letter2 if the guess was not letter1, and you only check for letter3 it the guess wasn't letter or letter2, etc. So for a word like "dad" you'll only ever show the first "d". The 'ElseIf' statements only get executed if the preceding test was false.
You need to check every letter every time.
Re: need help with hangman game
Try something like this: need an array of labels and an arrar called letter. Also added a button for this example to load the 'answer' into the labels....
Code:
Option Explicit
Dim letter() As String
Private Sub btnguess_Click()
Dim myLetter As String
myLetter = UCase(Trim(txtGuess.Text))
Dim x As Integer
For x = 1 To 5
If myLetter = letter(x) Then
Label1(x).Visible = True
End If
Next x
End Sub
Private Sub cmdSetUpAnswer_Click()
ReDim letter(6) As String
Dim x As Integer
letter(1) = "B"
letter(2) = "A"
letter(3) = "S"
letter(4) = "I"
letter(5) = "C"
For x = 1 To 5
Label1(x).Caption = letter(x)
Next x
End Sub
Re: need help with hangman game
Sorry, that is an ARRAY called letter
and, you, of course, must click the cmdSetupAnswer button first....but I'm assuming your project ALREADY ahs those labels identified with the letters of an answer...but, I'd use the array....MUCH easier.
Re: need help with hangman game
If you aren't aginst totally rethinking things, something like this could work.
Code:
Option Explicit
Dim strAnswer As String
Dim strPlaceHolder() As String
Private Sub Form_Load()
Dim i As Integer
Dim ub As Integer
strAnswer = "VBForums"
ub = Len(strAnswer) - 1
ReDim strPlaceHolder(ub)
For i = 0 To ub
strPlaceHolder(i) = "_"
Next i
lblAnswer.Caption = Join(strPlaceHolder)
End Sub
Private Sub btnguess_Click_Click()
Dim intPoss As Integer
intPoss = InStr(LCase(strAnswer), LCase(txtGuess.Text))
Do While intPoss > 0
strPlaceHolder(intPoss - 1) = txtGuess.Text
intPoss = InStr(intPoss + 1, strAnswer, txtGuess.Text)
Loop
lblAnswer.Caption = Join(strPlaceHolder)
txtGuess.Text = ""
txtGuess.SetFocus
End Sub