Link to home
Start Free TrialLog in
Avatar of sethRnda
sethRnda

asked on

Help with vb.net hangman

Hey everybody,

I have to develop hangman with vb.net. I'm able to do the easy stuff like display underscores for each letter for the word to guess. The problem I'm having is how to replace the underscores with correct values when a button is clicked. I'm starting to think I need to use a control array, but I have no idea how to establish one. Here's the code I have so far if anyone cares to look....My btn_click sub procedure is a mess and it's the main problem with the program.

Public Class Form1
    Inherits System.Windows.Forms.Form


    Dim strWord As String


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        strWord = InputBox("Please enter a word for the user to guess.")
        txtWord.Text = underscore(strWord.Length, "")
       





    End Sub

Private Function underscore(ByVal wordlength As Integer, ByVal undscore As String) As String

        For wordlength = 0 To wordlength - 1
            underscore += "_ "
        Next

        Return underscore


    End Function

 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 = sender
        Dim indx As Integer
        Dim i As Integer
        Dim j As Integer





        If Not btn.Text = "" Then
            indx = GroupBox1.Controls.IndexOf(btn)
            For i = 0 To strWord.Length - 1
                For j = 0 To txtWord.Text.Length - 1
                    If strWord.Substring(i, 1) = btn.Text Then
                        strWord.Replace("_"c, btn.Text)

                        txtWord.Text = strWord.ToCharArray(i, 1) + underscore(strWord.Length - 1, "")







                    End If
                Next j

            Next i





        End If

    End Sub

End Class
ASKER CERTIFIED SOLUTION
Avatar of VoteyDisciple
VoteyDisciple

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sethRnda
sethRnda

ASKER

Ok....I understand why the second for loop is useless. I guess the problem I'm having is with the substring method. Right now if I type in 'abc' as the word if I click buttonA I'll get this in txtWord 'a_ _'.....then if I click buttonB txtWord looks like this...'b_ _' ...b replaces a: (        

I don't know why I'm having such a problem with this. I picked up ADO real easy....but this is different. I also need to figure out a way to disable the button after it's clicked. That's why I was thinking I needed a control array.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This was the key line of code you gave me.....txtWord.Text = txtWord.Text.Substring(0, i - 1) & btn.Text & txtWord.Text.Substring(i + 1)


i had to get rid of the -1 in the txtWord.text.substring(0, i) and I think I have to get rid of the + 1 in the last substring call, but the letters are replacing the underscores now. Now I just have to figure out why this is working: ) Thanks....I might have a few more questions for you later if you don't mind. This is what that line looks like now....

 txtWord.Text = txtWord.Text.Substring(0, i) & btn.Text '& txtWord.Text.Substring(i)

I can disable the buttons that contained correct letters, but not the buttons that contain wrong values...

 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
                    strWord.Replace("_"c, btn.Text)

                    txtWord.Text = txtWord.Text.Substring(0, i) & btn.Text & txtWord.Text.Substring(i, 1)


                    If btn.Visible = True Then
                        btn.Visible = False
                    Else
                        If Not btn.Text = strWord.Substring(i, 1) And btn.Visible = True Then



                            btn.Visible = False



                        End If

                    End If
There are a few problems here.

1.  Inside an If statement that reads  "strWord.Substring(i, 1) = btn.Text" you have annother If statement that reads "Not strWord.Substring(i, 1) = btn.Text)"

Erm... that's contradictory.

2.  In the Else block for If btn.Visible = True you check to see if btn.Visible = True.  Again, that can't ever happen.  If btn.Visible were true you wouldn't have fallen into that Else block.

3.  You cannot possibly tell that I guessed wrong until you've checked EVERY letter in the word.  If you look at the first letter, see that it doesn't match my guess, and then just flat out say I was wrong, you're forgetting about all the OTHER letters in the word: maybe I got one of those right, and it just wasn't the first!



More directly than that, though... under what circumstances might I click a button and still have it stay on the screen?
More directly than that, though... under what circumstances might I click a button and still have it stay on the screen?


i guess if you click a button that doesn't contain a correct value it could stay of the screen...I just need a way to disable it or I need a way to inform the user that value that button contains has already been clicked.
Well that gets harder then; you'll need a way to keep track of whether any letters matched, so that after you're done with the loop you can decide whether to disable the button or hide it.

It looked to me like you were trying to get the button to disappear no matter what, in which case my suggestion would be: just have the button disappear no matter what.  (-:
Man this program is a pain in the arse................
I thought the for loop incremented the substring index...so if the word is something like 'battle' both the t's should show up because i is incrementing...right?
Now the program is adding underscores to the end of the word...............: (