Option Explicit
Const MaxGuesses As Integer = 10
Public StopGameStatus As enumStopGameStatus
Private HangmanBook As Workbook
Private pWordToGuess As String
Property Get WordToGuess() As String
WordToGuess = pWordToGuess
End Property
Property Let WordToGuess(ThisGuessWord As String)
pWordToGuess = ThisGuessWord
Dim WordCount As Integer
WordCount = Len(pWordToGuess)
Range(Cells(1, 1), Cells(1, WordCount)).Name = "Word"
Range(Cells(1, WordCount + 1), Range("A1").End(xlToRight)).EntireColumn.Hidden = True
Range(Cells(8, 1), Cells(8, 1).End(xlDown)).EntireRow.Hidden = True
With Range("Word")
.EntireRow.RowHeight = 40
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlCenter
.Font.Bold = True
.Interior.Color = RGB(240, 240, 240)
End With
Dim c As Range
For Each c In Range("Word").Cells
With c
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlThin
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlThin
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.EntireColumn.ColumnWidth = 15
End With
Next c
Range("B3").Value = "Correct"
Range("B4").Value = "Wrong"
Range("B5").Value = "Left"
Range("A3").Name = "Correct"
Range("A4").Name = "Wrong"
Range("A5").Name = "Left"
Range("C3").Name = "GuessesCorrect"
Range("C4").Name = "GuessesWrong"
Range("C5").Name = "GuessesLeft"
Dim SpacedAlphabet As String
Dim LetterPosition As Integer
SpacedAlphabet = ""
For LetterPosition = 1 To Len(Alphabet)
SpacedAlphabet = SpacedAlphabet & Mid(Alphabet, LetterPosition, 1) & " "
Next LetterPosition
Range("GuessesLeft").Value = SpacedAlphabet
Range("Correct").Value = 0
Range("Wrong").Value = 0
Range("Left").Value = MaxGuesses
End Property
Private Sub Class_Initialize()
Set HangmanBook = Workbooks.Add
StopGameStatus = GameInProgress
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name <> ActiveSheet.Name Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End If
Next ws
ActiveSheet.Name = "Hangman"
End Sub
Private Sub Class_Terminate()
HangmanBook.Close savechanges:=False
End Sub
Private Sub RemoveLetter(WhichLetter As String)
Range("GuessesLeft").Value = Replace(Range("GuessesLeft").Value, " " & WhichLetter, "")
End Sub
Sub PlayRound()
Dim Letter As New clsGuess
Letter.CorrectWord = pWordToGuess
If Not Letter.IfTooManyGoes Then
StopGameStatus = UserKeptGuessingInvalidLetters
Exit Sub
End If
If Letter.IfAlreadyGuessed Then
MsgBox "You've already guessed this letter!"
Exit Sub
End If
RemoveLetter Letter.LetterGuessed
If Letter.IfGuessCorrect Then
Range("GuessesCorrect").Value = Range("GuessesCorrect").Value & " " & Letter.LetterGuessed
Range("Correct").Value = Range("Correct").Value + 1
If IfWordGuessed Then
StopGameStatus = UserWon
Exit Sub
End If
MsgBox "Good guess! Letter " & Letter.LetterGuessed & " was in the word.", vbOKOnly + vbExclamation, "Correct guess"
Else
Range("GuessesWrong").Value = Range("GuessesWrong").Value & " " & Letter.LetterGuessed
Range("Wrong").Value = Range("Wrong").Value + 1
Range("Left") = Range("Left").Value - 1
If Range("Left").Value = 0 Then
StopGameStatus = UserLost
Exit Sub
End If
MsgBox "Sorry: letter " & Letter.LetterGuessed & " is not in the word.", vbOKOnly + vbExclamation, "Wrong guess"
End If
End Sub
Private Function IfWordGuessed() As Boolean
Dim c As Range
For Each c In Range("Word")
If c.Value = "" Then
IfWordGuessed = False
Exit Function
End If
Next c
IfWordGuessed = True
End Function