- Home
·
- Games
·
- Plays Hangman, draws hangman, keeps track off number of guesses, MsgBox for win or loss
Plays Hangman, draws hangman, keeps track off number of guesses, MsgBox for win or loss
Plays Hangman, draws hangman, keeps track off number of guesses, MsgBox for win or loss
Rate Plays Hangman, draws hangman, keeps track off number of guesses, MsgBox for win or loss
(2(2 Vote))
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
Plays Hangman, draws hangman, keeps track off number of guesses, MsgBox for win or loss Comments
No comments yet — be the first to post one!
Post a Comment