?
Solved

help with hangman

Posted on 2006-11-11
4
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

752 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