by James Nass (2 Submissions)
Category: Games
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 17th February 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Plays Hangman, draws hangman, keeps track off number of guesses, MsgBox for win or loss
Private Sub cmdPlayGame_Click()
Const strSentinel As String = "!"
Dim strSecretWord As String, intSecretWordLength As Integer
Dim intNumberOfGuesses As Integer
Dim strGuess As String, strWordGuessedSoFar As String
Dim intLetterPos As Integer
strSecretWord = "computer"
intSecretWordLength = Len(strSecretWord)
strWordGuessedSoFar = String(intSecretWordLength, "-")
lblWord.Caption = strWordGuessedSoFar
intNumberOfGuesses = 0
strGuess = InputBox("Guess a letter (! to guess word)", "Hangman")
Do While strGuess <> strSentinel
intNumberOfGuesses = intNumberOfGuesses + 1
For intLetterPos = 1 To intSecretWordLength
If StrComp(strGuess, Mid(strSecretWord, intLetterPos, 1), vbTextCompare) = 0 Then
Mid(strWordGuessedSoFar, intLetterPos, 1) = strGuess
End If
Next intLetterPos
lblWord.Caption = strWordGuessedSoFar
strGuess = InputBox("Guess a letter (! to guess word)", "Hangman")
If intNumberOfGuesses = 1 Then shpHead.Visible = True
If intNumberOfGuesses = 2 Then linBody.Visible = True
If intNumberOfGuesses = 3 Then linLeftArm.Visible = True
If intNumberOfGuesses = 4 Then linRightArm.Visible = True
If intNumberOfGuesses = 5 Then linLeftLeg.Visible = True
If intNumberOfGuesses = 6 Then linRightLeg.Visible = True
If intNumberOfGuesses = 7 Then lineRope.Visible = True
If intNumberOfGuesses = 8 Then lineGallow.Visible = True
If intNumberOfGuesses = 9 Then MsgBox "You Lose"
Loop
If strGuess = strSentinel Then
strGuess = InputBox("Guess the word")
End If
If StrComp(strGuess, strSecretWord, vbTextCompare) = 0 Then
MsgBox "You win! It took you " & intNumberOfGuesses & " guesses."
Else
MsgBox "You lose. After " & intNumberOfGuesses & " guesses you got it wrong."
End If
lblWord.Caption = strSecretWord
End Sub
Private Sub cmdDone_Click()
Unload Me
End Sub
No comments have been posted about Plays Hangman, draws hangman, keeps track off number of guesses, MsgBox for win or loss. Why not be the first to post a comment about Plays Hangman, draws hangman, keeps track off number of guesses, MsgBox for win or loss.