Public Sub PlayHangman()
Dim bPlaying As Boolean
Dim sInput As String
Dim lTries As Long
Dim vaWords As Variant
Dim sTheWord As String
Dim aDispWord() As String
Dim i As Long
Const sTERMCHAR As String = "-"
Const lMAXTRIES As Long = 6
bPlaying = True
'I'll pick one of these words at random
vaWords = Split("jazz zigzag colour favourite doughnut rabbit")
sTheWord = vaWords(Int(Rnd * (UBound(vaWords) - LBound(vaWords) + 1) + LBound(vaWords)))
'Fill the displayed word w "emtpy space"
ReDim aDispWord(1 To Len(sTheWord))
For i = LBound(aDispWord) To UBound(aDispWord)
aDispWord(i) = "_"
Next i
'Start the game
Do While bPlaying
Debug.Print "Welcome to hangman"
'This is the guessing loop
Do
'Print the hangman and whatever was guessed
PrintHangman lTries
Debug.Print Join(aDispWord, Space(1))
sInput = Left$(InputBox("Enter your guess", "Hangman"), 1)
'Type a dash to get abort early
If sInput = sTERMCHAR Then
bPlaying = False
Exit Do
End If
'Correct guesses get filled in the displayed word and incorrect
'guesses increments lTries
If InStr(1, sTheWord, sInput) > 0 Then
For i = 1 To Len(sTheWord)
If Mid$(sTheWord, i, 1) = sInput Then
aDispWord(i) = sInput
End If
Next i
Else
lTries = lTries + 1
End If
Loop Until lTries >= lMAXTRIES Or Join(aDispWord, vbNullString) = sTheWord
If Join(aDispWord, vbNullString) = sTheWord Then
Debug.Print "You win"
Else
Debug.Print "You lose. The word was " & sTheWord
End If
bPlaying = MsgBox("Play again?", vbYesNo) = vbYes
Loop
End Sub
Public Sub PrintHangman(ByVal lTries As Long)
Dim aMan(1 To 5, 1 To 7) As String
Dim i As Long, j As Long
Dim sPrint As String
aMan(1, 1) = "_": aMan(1, 2) = "_": aMan(1, 3) = "_": aMan(1, 4) = "_": aMan(1, 5) = "_": aMan(1, 6) = "_"
For i = 2 To UBound(aMan)
aMan(i, 1) = "|"
For j = 2 To UBound(aMan, 2)
aMan(i, j) = Space(1)
Next j
Next i
aMan(2, 6) = "|"
If lTries >= 1 Then aMan(3, 6) = "o"
If lTries >= 2 Then aMan(4, 6) = "|"
If lTries >= 3 Then aMan(4, 5) = "/"
If lTries >= 4 Then aMan(4, 7) = "\"
If lTries >= 5 Then aMan(5, 5) = "/"
If lTries >= 6 Then aMan(5, 7) = "\"
For i = LBound(aMan, 1) To UBound(aMan, 1)
For j = LBound(aMan, 2) To UBound(aMan, 2)
sPrint = sPrint & aMan(i, j)
Next j
Debug.Print sPrint
sPrint = vbNullString
Next i
End Sub