Hangman program issues..

eric_r

Active member
Joined
Aug 30, 2006
Messages
29
Programming Experience
Beginner
Im fairly new to VB.NET and I am making the infamous Hangman program.

So far Ive managed to draw a random word from a text file and display it in a label on form load, but what I really want to do is convert all of the characters to an underscore so they can't see it.

I am debating on that, or just letting a reader count the characters in the word and assign a certain amount of underscores to the label instead of converting anything.

Like I said, I am NEW so tips or direction would be much appreciated. If I could get ahold of my professor I would.

Thanks!
 
all strings (including the Text property in labels, textbox's, etc..) have a Length property, this property is the number of characters in the string, in this case the number of letters in the word

you can use that to assign underscores to a label, for example
VB.NET:
'This assumes you have two labels on the form, one named "lblWord" the other named "lblUnderScores"
lblWord.Text = "Hangman"
For Counter As Integer = 1 To lblWord.Text.Length
  lblUnderScores.Text  &= "_"
Next Counter
'After this loop runs, lblUnderScores has 7 underscores in it

does that help?
 
Great, that did help.

Ive got it so there are 2 labels. One shows the actual word and one shows the underscores. I added a space after the "_" so it spaced them properly.

Now I am thinking about how I can get part of the _ _ _ _ label to show the letter when it is correctly guessed. I havent come up with anything yet :(

Appreciate your assistance!
 
You should make another label lblWordSpaced that contains the word with spaces.

add this code in your form_load:

VB.NET:
Dim spacedword As String = ""
For Each character As Char In lblWord.Text
spacedword = spacedword + character + " "
Next
lblWordSpaced.Text = spacedword

So now you have for example:
lblWord.Text = "HANGMAN"
lblWordSpaced.Text = "H A N G M A N"
lblUnderscores.Text = "_ _ _ _ _ _ _"

Now you see that the positions of the spaced word and the underscores are the same.

Now try to get hold of the "A" by using the indexof method on lblWordSpaced.Text.

When you have that index (=3), you can use that to delete the underscore at position 3 in lblUnderscores.text and insert the "A".

Only problem now is that there can be several "A"s in your word, so you'll have to make a loop somehow. remember indexOf returns -1 when it didn't find anything.

I have the solution here in front of me, i just made it, in case you don't find it. Let us know how far you get.
 
Im kind of stuck actually. just letting you know I'm working on it right now but it may take a while to come up with something concrete.
 
take your time, when learning vb it's best to make an attempt first and when you get stuck, post the code and we can help

in this case there are several ways to accomplish this, so take your time
 
Ive had a minor breakthrough. Very minor..

Here's my code

VB.NET:
    Dim Words(39) As String
    Dim sr As IO.StreamReader
    Dim Index, Counter As Integer
    Dim selectedWord As String
    Dim buttonPushed As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sr As IO.StreamReader
        Dim num As String
        Dim spacedword As String = ""
        sr = IO.File.OpenText("C:\Programming\hangman\words_computers.txt")
        Do While sr.Peek <> -1
            sr.ReadLine()
            Counter += 1
        Loop
        sr.Close()
        ReDim Words(Counter - 1)
        sr = IO.File.OpenText("C:\Programming\hangman\words_computers.txt")
        For Index = 0 To (Counter - 1)
            Words(Index) = sr.ReadLine
        Next Index
        GetRandomWord()    
        AssignUnderscores()
        whenIncorrect()
        sr.Close()
        For Each character As Char In lblWord.Text
            spacedword = spacedword + character + " "
        Next
        lblWordSpaced.Text = spacedword
    End Sub
    Public Sub checkLetter()
        'this function will be called for each Letter button.
        Dim letter As String
        letter = txtGuess.Text
        For Index = 0 To (selectedWord.Length - 1)
            If selectedWord.Substring(Index, 1) = letter Then
                ReplaceLetter()
                lblWord.Text = "just testing"
            End If
        Next
    End Sub
    Public Sub ReplaceLetter()

    End Sub
    Public Sub AssignUnderscores()
        'This assumes you have two labels on the form, one named "lblWord" the other named "lblUnderScores"
        lblWord.Text = selectedWord
        For Counter As Integer = 1 To lblWord.Text.Length
            lblUnderscores.Text &= "_ "

        Next Counter
        'After this loop runs, lblUnderScores has underscores in it
    End Sub
    Public Sub whenIncorrect()
       End Sub
    Public Sub GetRandomWord()
        'THIS Function simulates random selection of a word and displays it in the label
        'Eventually it needs to return underscores _ _ _ in the place of the letters
        Dim randomNum As New Random   
        selectedWord = Words(randomNum.Next(0, 40))        'THIS is important code! chooses between 0 and 40. 0 counts, but 40 does not
        lblWord.Text = selectedWord                        'just displays the word. change this because it gives away the answer. should be "_ _ _ _ _"
    End Sub

Its kind of a mess right now because Im very much so testing out several different things.

What I did is make a function that checks selectedWord for the letter.

What I need next is a way to replace the underscore for the correct letter. I was thinking about how the string "_ _ _" is 5 characters. I'll have to create some sort of algorithm that will read this, and completely replace the string including the underscores. I dont think its possible to just insert the letter because its a string. Pondering ideas on how to create a function that is able to add underscores appropriately regardless of the word.
 
Hey, i see you made progress indeed since last time :) .

You are actually very close, all you need to do is:
- declare spacedword on top.
- in checkLetter(): make use of spacedword instead of selectedword
- add 2 lines of code in replaceLetter()

In your function checkLetter:
Make use of spacedword instead of selectedword.
Why? well because the length of spacedword and the underscores is the same.
For example: spacedword contains "T E S T" and underscores contains "_ _ _ _".

If you know where T is in "T E S T", you will know exactly where to add T in "_ _ _ _".

So, in your function, we are looking for "T" in that spacedword.
It will find T at index 0 in "T E S T"

Btw you can actually do remove and insert a character in a string
your replaceLetter function willl be like this:
remove the underscore at index 0 (in this case) in the lblunderscores.text ("_ _ _ _")
and insert "T" at index 0 (in this case) in lblunderscores.text "T _ _ _"

So an extra 2 lines of code in there.

Let us know if you get there! :)
 
Last edited:
Great, you are a huge help! Its not that I lack the concepts really, its that I simply lack the language syntax experience to get it done. I dont want the code to be given to me really but what you are all doing is great. I learn by example and once I see what the code is capable of I can usually twist it to work for me.

A few notes on your suggestions... I did what you said with declaring spacedword but found it necessary to include this at the top of CheckLetter

spacedword = lblWordSpaced.Text

Or else it crashes!

Now I am playing with the last portion of your advice, I will update when I progress!
 
by the way:

remove the underscore at index 0 in the lblunderscores.text ("_ _ _ _")
and insert "T" at index 0 in lblunderscores.text "T _ _ _"

I know what you mean, but the actual code evades me. Hints on what to call or how to do it? Im paging through my textbook right now and its not helping..
 
I can't really hint but give you the syntax, sorry for that ;) so here it is:

VB.NET:
lblUnderScores.Text = lblUnderScores.Text.Remove(index, 1) 'removes the underscore at that index
lblUnderScores.Text = lblUnderScores.Text.Insert(index, letter) 'inserts "T" at that index
 
Update:

It replaces the appropriate underscore with the letter given when I use this code
VB.NET:
    Public Sub ReplaceLetter()
        ' thanks to CygNuS for this code!
        lblUnderscores.Text = lblUnderscores.Text.Remove(Index, 1) 'removes the underscore at that index
        lblUnderscores.Text = lblUnderscores.Text.Insert(Index, letter) 'inserts "T" at that index

    End Sub
but it will only replace up to the 5th or 7th character in the index (ie 3rd or 4th letter in the word) and I think its due to this ...

VB.NET:
    Public Sub checkLetter()
        letter = txtGuess.Text.ToUpper
        spacedword = lblWordSpaced.Text
        For Index = 0 To (selectedWord.Length - 1)
            If spacedword.Substring(Index, 1) = letter Then
                ReplaceLetter()
                lblWord.Text = "it works!" 'disabled and not in view
            End If
            letterFound = letter
        Next
    End Sub
I'm no genius but shouldnt For Index = 0 to (selectedWord.Length - 1) actually be For Index = 0 to (spacedword.Length - 1) ????

edit: the answer is yes.. riddle solved.
 
My next question is probably silly to you guys but I am new to imageLists.

Im using an ImageList to sequentially bump up the image everytime an incorrect letter is given. it kind of works but my first issue is that as soon as the imagelist is loaded, it changes the picBox.Image to max size of (256, 256)

So its smaller than the (376, 378) dimensions that the picBox image actually is. that is just the beginning of the issues. Why can't I set image size to anything higher than 256? It shows height and width dimensions as proper but when it comes down to actually showing it, it shrinks it down to 256x256.

my feeble attempt
VB.NET:
    Public Sub subPoint()
        If ilsHang.Images.Empty <> True Then
            If ilsHang.Images.Count - 1 > currentImage Then
                currentImage += 1
            Else
                currentImage = 0
            End If
        End If
        ilsHang.ImageSize = New Size(376, 378)
        picBox.Image = ilsHang.Images(currentImage)
    End Sub
 
Back
Top