Solved

help with hangman

Posted on 2006-11-11
4
271 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 250 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

856 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