I'm trying to make a hangman game and i cannot get it to work right. What i want it to do is: prompt for a word to guess. Display # of dashes for the word entered. And have how ever many letters the word is that was entered multiply it by 2 and that is how many guesses you will get. If all guesses are used up, then it will say game over and tell you what the word was. If guessed right it will say good guessing. My code is as follows so far:
Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
'simulates the hangman game
'declares variables
Dim strWord As String 'stores the word to be guessed
Dim strLetter As String 'stores the letter guesses
Dim blnDashReplaced As Boolean 'indicates if a dash was replaced
Dim blnGameOver As Boolean 'indicates if the game is over
Dim intIncorrectGuesses As Integer 'counts the number of incorrect guesses
Dim intIndex As Integer 'keeps track of the indexes
'get a 5-letter word from the first player
strWord = InputBox("Enter a word:", "Word")
Do While strWord.Length > 1
strWord = InputBox("Enter a word:", "Word")
Loop
'convert word to uppercase
strWord = strWord.ToUpper()
'display dashes for word entered ??
Me.lblWord.Text = "-----"
'clear the lblIncorrectGuesses control
Me.lblIncorrectGuesses.Text = ""
'allow the second player to guess a letter
'the game is over when either the word has been guessed or
'the second player makes 10 incorrect guesses
Do While Not blnGameOver
'get a letter from the second player
strLetter = InputBox("Enter a letter:", "Letter", "", 500, 400)
'convert letter to uppercase
strLetter = strWord.ToUpper()
'search the word for the letter
For intIndex = 0 To strWord.Length - 1
If strWord.Substring(intIndex, 1) = strLetter Then
'replace appropriate dash in the lblWord control
Mid(Me.lblWord.Text, intIndex + 1) = strLetter
'indicate that a replacement was made
blnDashReplaced = True
End If
Next intIndex
'determine whether a replacement was made
If blnDashReplaced Then
'if the word does not contain any dashes, then
'the user guessed the word, so the game is over
If Me.lblWord.Text.IndexOf("-") = -1 Then
blnGameOver = True
MessageBox.Show("Great guessing!", "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1)
Else 'reset the blnDashReplaced variable
blnDashReplaced = False
End If
Else 'processed when the word contains at least 1 dash
'update the counter variable, then display the result
intIncorrectGuesses = intIncorrectGuesses + 1
Me.lblIncorrectGuesses.Text = Convert.ToString(intIncorrectGuesses)
'determine whether the user has made 10 incorrect guesses, the game is over
blnGameOver = True
Me.lblWord.Text = "Game Over"
MessageBox.Show("Sorry, the word is " & strWord, "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1)
End If
Loop
End Sub
End Class
Any help/code that anyone can provide would be greatly appreciated. Thanks, Correy.
Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
'simulates the hangman game
'declares variables
Dim strWord As String 'stores the word to be guessed
Dim strLetter As String 'stores the letter guesses
Dim blnDashReplaced As Boolean 'indicates if a dash was replaced
Dim blnGameOver As Boolean 'indicates if the game is over
Dim intIncorrectGuesses As Integer 'counts the number of incorrect guesses
Dim intIndex As Integer 'keeps track of the indexes
'get a 5-letter word from the first player
strWord = InputBox("Enter a word:", "Word")
Do While strWord.Length > 1
strWord = InputBox("Enter a word:", "Word")
Loop
'convert word to uppercase
strWord = strWord.ToUpper()
'display dashes for word entered ??
Me.lblWord.Text = "-----"
'clear the lblIncorrectGuesses control
Me.lblIncorrectGuesses.Text = ""
'allow the second player to guess a letter
'the game is over when either the word has been guessed or
'the second player makes 10 incorrect guesses
Do While Not blnGameOver
'get a letter from the second player
strLetter = InputBox("Enter a letter:", "Letter", "", 500, 400)
'convert letter to uppercase
strLetter = strWord.ToUpper()
'search the word for the letter
For intIndex = 0 To strWord.Length - 1
If strWord.Substring(intIndex, 1) = strLetter Then
'replace appropriate dash in the lblWord control
Mid(Me.lblWord.Text, intIndex + 1) = strLetter
'indicate that a replacement was made
blnDashReplaced = True
End If
Next intIndex
'determine whether a replacement was made
If blnDashReplaced Then
'if the word does not contain any dashes, then
'the user guessed the word, so the game is over
If Me.lblWord.Text.IndexOf("-") = -1 Then
blnGameOver = True
MessageBox.Show("Great guessing!", "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1)
Else 'reset the blnDashReplaced variable
blnDashReplaced = False
End If
Else 'processed when the word contains at least 1 dash
'update the counter variable, then display the result
intIncorrectGuesses = intIncorrectGuesses + 1
Me.lblIncorrectGuesses.Text = Convert.ToString(intIncorrectGuesses)
'determine whether the user has made 10 incorrect guesses, the game is over
blnGameOver = True
Me.lblWord.Text = "Game Over"
MessageBox.Show("Sorry, the word is " & strWord, "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information, _
MessageBoxDefaultButton.Button1)
End If
Loop
End Sub
End Class
Any help/code that anyone can provide would be greatly appreciated. Thanks, Correy.
Last edited: