Solved

help with hangman

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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.
This is about my first experience with programming Arduino.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now