?
Solved

help with hangman

Posted on 2006-11-11
4
Medium Priority
?
293 Views
Last Modified: 2010-04-16
Hi everyone: )

I'm having a problem when the word a player is trying to guess has two of the same letter. Say the word is 'letter'. When the user guesses 't' my guess variable is only getting incremented by one....any suggestions? Here's the portion of the code I think needs to be changed....


 Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQ.Click, btnP.Click, btnO.Click, btnN.Click, btnM.Click, btnL.Click, btnK.Click, btnJ.Click, btnI.Click, btnH.Click, btnG.Click, btnA.Click, btnB.Click, btnC.Click, btnD.Click, btnE.Click, btnF.Click, btnR.Click, btnS.Click, btnT.Click, btnU.Click, btnV.Click, btnW.Click, btnX.Click, btnY.Click, btnZ.Click

        Dim btn As Button = CType(sender, Button)

        Dim i As Integer
        Dim found As Boolean = False





        If Not btn.Text = "" Then
            ' indx = GroupBox1.Controls.IndexOf(btn)
            For i = 0 To strWord.Length - 1

                If strWord.Substring(i, 1) = btn.Text Then
                    found = True
                    ' strWord.Replace("_"c, btn.Text)

                    txtWord.Text = txtWord.Text.Substring(0, i) & btn.Text & txtWord.Text.Substring(i + 1).Replace("_"c, " "c)

                End If
            Next i
        End If









        If found Then

            btn.Enabled = False
            guess = guess + 1
            If guess = strWord.Length Then

                MessageBox.Show("You win!!!")
                playAgain = InputBox("Would you like to play again? Type 0 for no or 1 for yes.")
                If playAgain = True Then
                    Dim newform As Form1 = New Form1
                    newform.Show()
                Else
                    Me.Close()
                End If



            End If
        End If







        If found = False Then


            btn.Visible = False
            Label2.Image = ImageList1.Images(m)


            m = m + 1



        End If
        Dim indx As Integer
        If m >= 7 Then
            MessageBox.Show("Sorry, but your hung: )")
            again = InputBox("Play again? Type 1 for yes, 0 for no.")
            Dim lossForm As Form1 = New Form1

            If again = True Then
                Dim newform As Form1 = New Form1
                newform.Show()
            Else
                Me.Close()
            End If


            ' Dim newform As Form1 = New Form1
            'newform.Show()

        End If
       
    End Sub
0
Comment
Question by:sethRnda
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17921078
Why not increment guess when you've actually found a match instead of waiting 'til you're all the way out of the loop and all you've got is taht (found) variable  Anything that depends on iterations of that loop needs to be done inside.
0
 

Author Comment

by:sethRnda
ID: 17921090
I'm not sure I understand what you mean : (
0
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 1000 total points
ID: 17921114
You have this loop looking for matching letters:

            For i = 0 To strWord.Length - 1
                If strWord.Substring(i, 1) = btn.Text Then
                    found = True
                    ' strWord.Replace("_"c, btn.Text)

                    txtWord.Text = txtWord.Text.Substring(0, i) & btn.Text & txtWord.Text.Substring(i + 1).Replace("_"c, " "c)
                End If
            Next i


Any and all code that relates to "doing stuff for each matching letter" (including counting it)  must be done within that loop.
0
 

Author Comment

by:sethRnda
ID: 17921185
boy I'm dumb sometimes.....thanks for the help.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Introduction to Processes
Starting up a Project

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question