Solved

Help with vb.net hangman

Posted on 2006-10-29
11
1,011 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
  • 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 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…

762 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

18 Experts available now in Live!

Get 1:1 Help Now