Ok, so I am working on the hangman game for a school assignment. I have everything doing what I want except for one issue: No matter how many letters are in my word, the number of dashes is always one more than I need.

For example:
say the word is apple, I want ----- which will fill in thus: a--le, etc. However, I am getting this ------, which fills in thus: -a--le. I cannot figure out why the additional dash is being appended.

I would appreciate a second set of eyes. Probably it will jump out at ya.
Here is my code:

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain
    Dim strWord, strLetter As String
    Dim intLine, intIncorrect As Integer
    Dim blnDashReplaced, blnGameOver As Boolean

    Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
        Me.Close()
    End Sub

    Private Sub GetWord()
        Dim strWordList() As String = Split(My.Computer.FileSystem.ReadAllText("Words.txt"), vbCr)
        Dim randomGenerator As New Random

        intLine = randomGenerator.Next(0, 64144)
        strWord = strWordList(intLine).ToUpper

    End Sub

    Private Sub NewGame()
        Dim intindex As Integer = 0

        lblDisplayWord.Text = ""
        lblIncorrect.Text = ""
        intIncorrect = 0

        'TEMP LABEL USED DURING DEBUGGING, TO BE DELETED WHEN DONE
        lblShowWord.Text = ""

        'clear out pic boxes
        picBottom.Visible = False
        picPost.Visible = False
        picTop.Visible = False
        picRope.Visible = False
        picHead.Visible = False
        picBody.Visible = False
        picRightArm.Visible = False
        picLeftArm.Visible = False
        picRightLeg.Visible = False
        picLeftLeg.Visible = False

        ' get a word from strWordList array and convert to uppercase
        Call GetWord()

        txtLetter.Focus()

        'TEMP LABEL USED DURING DEBUGGING, TO BE DELETED WHEN DONE
        lblShowWord.Text = strWord

        Do While intindex < strWord.Length
            lblDisplayWord.Text &= "-"
            intindex += 1
        Loop

    End Sub

    Private Sub mnuFileNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click
        Call NewGame()
    End Sub

    Private Sub btnGetWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetWord.Click
        Call NewGame()
    End Sub

    Private Sub txtLetter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLetter.TextChanged
        ' get a letter from player and convert to uppercase
        strLetter = (txtLetter.Text).ToUpper
    End Sub

    Private Sub btnCheckLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckLetter.Click

        Do While strLetter <> String.Empty AndAlso blnGameOver = False
            'search the word for a letter
            Dim intindex As Integer = 0
            For intindex = 0 To strWord.Length - 1
                'if letter appears in word then replace dash with letter
                If strWord.Substring(intindex, 1) = strLetter Then
                    lblDisplayWord.Text = lblDisplayWord.Text.Remove(intindex, 1)
                    lblDisplayWord.Text = lblDisplayWord.Text.Insert(intindex, strLetter)
                    blnDashReplaced = True
                End If
            Next intindex

            If blnDashReplaced = True Then
                If lblDisplayWord.Text.Contains("-") = False Then
                    blnGameOver = True
                    MessageBox.Show("You Win!", "Game Over")
                Else
                    blnDashReplaced = False
                End If

            Else
                lblIncorrect.Text &= strLetter
                intIncorrect += 1

                Select Case intIncorrect
                    Case 1
                        picBottom.Visible = True
                    Case 2
                        picPost.Visible = True
                    Case 3
                        picTop.Visible = True
                    Case 4
                        picRope.Visible = True
                    Case 5
                        picHead.Visible = True
                    Case 6
                        picBody.Visible = True
                    Case 7
                        picRightArm.Visible = True
                    Case 8
                        picLeftArm.Visible = True
                    Case 9
                        picRightLeg.Visible = True
                    Case 10
                        picLeftLeg.Visible = True
                        blnGameOver = True
                        MessageBox.Show("Sorry the word is " & strWord & ".", "Game Over")
                End Select

            End If

            strLetter = String.Empty
            txtLetter.Text = String.Empty
            txtLetter.Focus()

        Loop

    End Sub

End Class

Recommended Answers

All 3 Replies

The loop seems fine. Try trimming the word, maybe it's getting an white space from the text file.
And just to be sure reset the value of the text box before inserting the dashes.

This is your problem

Private Sub GetWord()
    Dim strWordList() As String = Split(My.Computer.FileSystem.ReadAllText("Words.txt"), vbCr)
    Dim randomGenerator As New Random
    intLine = randomGenerator.Next(0, 64144)
    strWord = strWordList(intLine).ToUpper
End Sub

You are splitting on VbCr. It should be VbCrLf

Or try:

Private Sub GetWord()
    Dim strWordList() As String = IO.File.ReadAllLines("Words.txt")
    Dim randomGenerator As New Random

    intLine = randomGenerator.Next(0, 64144)
    strWord = strWordList(intLine).ToUpper

End Sub

Also, instead of the loop: lblDisplayWord.Text = New String("-"c, strWord.Length)

Changing it to VbCrLf did the trick! Thanks so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.