The Boy (but not this The Boy) is taking a Java class. I was watching him code up a hangman game last night. There’s a lot to like about Java, I think. It’s frustrating working with a new language when you don’t know all the debugging tricks. I couldn’t figure out how to step through code or use the console to inspect variables (like an Immediate Window) in Eclipse. I’m sure it can be done, I just don’t know how.
I decided to replicate his game in VBA. It’s pretty rough. It doesn’t tell you what you’ve already guessed, for one thing. Also, I don’t use the grid because he was using ASCII art in the Eclipse console and I wanted to do the same. Clearly shapes on the grid would be cooler. VBA doesn’t have a “scan” feature to get input, so I had to use InputBox and MsgBox to get user input.
As I was coding it, I was thinking that I should have use test-first development as an exercise, but I didn’t. For what it’s worth:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
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 |
Posting code? Use <pre> tags for VBA and <code> tags for inline.