Solved

Help with vb.net hangman

Posted on 2006-10-29
11
1,048 Views
Last Modified: 2008-01-16
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
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
  • 7
  • 4
11 Comments
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 500 total points
ID: 17831132
You've got all the logic you need, you've just got some other stuff too.

When looking through strWord (using that outer For i = 0 To strWord.Length - 1  loop, if you encounter the matching character ( If strWord.Substring(i, 1) = btn.Text Then ), then you already know where it goes in txtWord.Text -- it goes at position (i).  An extra loop to look through the characters of the word on the screen doesn't make sense.

So look through the master copy of the word, see if a character matches, and if it does then the word you display to the user should look like:
1.  All the stuff in txtWord.Text up to position (i), since nothing in that area has changed.
2.  Instead of the character at position (i), which is an underscore still, the letter the user clicked (which you already know 'cause you just went looking for it!)
3.  All the stuff in txtWord.Text after position (i), since none of that has changed either.

(Sure, it may change later on for some other value of (i), but so what.)

Different combinations of .Substring() and VB's string concatentation operator ( & ) should be all you need to do the replacement.
0
 

Author Comment

by:sethRnda
ID: 17831336
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.
0
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 500 total points
ID: 17831432
The code you have changing txtWord right now is:

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

So you've got "something" (strWord.ToCharArray(i, 1)) followed by "a bunch of underscores" -- sure, you'll only ever show words as a letter followed by a bunch of underscores.

First, it's probably a little more direct to get the correct letter from btn.Text the way you did in the If statement (just 'cause it's shorter, really)

That'd still be txtWord.Text = btn.Text & underscore(strWord.Length - 1, "")
... which is still the correct letter followed by a bunch of underscores.

You want "the beginning part of txtWord.Text" followed by "the letter" followed by "the ending part of txtWord.Text"
How 'bout
txtWord.Text = txtWord.Text.Substring(0, i - 1) & btn.Text & txtWord.Text.Substring(i + 1)


We don't care what's in strWord at all any more; you've already determined that the letter at (i) matches, so now just worry about what you're showing in the TextBox.


As to disabling the control, isn't that what 'btn' is for?


Incidentally, to be perfectly correct it should really be:
Dim btn As Button = CType(sender, Button)

VB .NET tends not to be picky about type coersion, but I always write it out anyway.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sethRnda
ID: 17831482
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)

0
 

Author Comment

by:sethRnda
ID: 17831617
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
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17831667
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?
0
 

Author Comment

by:sethRnda
ID: 17831716
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.
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17831726
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.  (-:
0
 

Author Comment

by:sethRnda
ID: 17831731
Man this program is a pain in the arse................
0
 

Author Comment

by:sethRnda
ID: 17831743
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?
0
 

Author Comment

by:sethRnda
ID: 17837067
Now the program is adding underscores to the end of the word...............: (
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example 2 56
IntelliJ and Eclipse Neon 2 installations 4 65
CSV file copy field 1 to field 2 2 62
JVM error from eclipse 1 25
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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.
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 …
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…

732 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