Solved

help with hangman

Posted on 2006-11-11
4
275 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
T-SQL:  Sigh---Boy, this is fun.... 12 68
What is wrong with the below insert statement. Getting error when executing. 5 53
AutoIncrement column based of FK 11 64
Java List 4 41
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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