Option Explicit On
Option Strict On
Public Class HangmanForm
Private Sub HangmanForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Closes the program
End
End Sub
Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
'declare variables
Dim strWord As String 'stores the word to be guessed
Dim strLetter As String 'stores the letter guesses
Dim blnIsDashReplaced As Boolean 'indicates if a dash was replaced
Dim blnIsGameOver As Boolean 'indicates if the game is over
Dim intIncorrectCounter As Integer 'counts the number of incorrect guesses
Dim intIndex As Integer 'keeps track of the indexes
Dim intAllowedGuesses As Integer
'Hide picture boxes
pctPostBottom.Visible = False
pctPostSide.Visible = False
pctPostTop.Visible = False
pctRope.Visible = False
pctHead.Visible = False
pctBody.Visible = False
pctRightArm.Visible = False
pctLeftArm.Visible = False
pctRightLeg.Visible = False
pctLeftLeg.Visible = False
'Get a word from the first player
strWord = InputBox("Enter A Secret Word:", "Secret Word")
'Convert word to uppercase
strLetter = strWord.ToUpper()
'Count characters in string
Dim intLength As Integer
intLength = strWord.Length
'Varible for dashes to be placed
Dim strDash As String
'Display dashes for word entered
Dim intCount As Integer
For intCount = 0 To intLength - 1
strDash = strDash & "-"
Next intCount
'declare variable for a space
Dim strSpace As String = " "
'declare position variable to check for space
Dim intChkSpace As Integer
'Declare a starting position variable
Dim intStart As Integer = 1
'Set loop counter to 0
intCount = 0
'Searches for a space in the word
For intCount = 0 To intLength - 1
intChkSpace = InStr(intStart, strWord, strSpace, CompareMethod.Text)
If intChkSpace = 0 Then
Exit For 'Exits loop if there is no space
Else
'Put space where it belongs
Mid(lblSecretWord.Text, intChkSpace, 1) = strDash
intStart = intChkSpace + 1
End If
Next intCount
'displays "-" for every letter in the string
lblSecretWord.Text = strDash
'Clear the lblIncorrectGuesses control
lblIncorrectGuesses.Text = ""
'Allow the second player to guess a letter
'The game is over when either the word has been guessed or
'The second player makes too many incorrect guesses
Do Until blnIsGameOver
Loop
'Get a letter from the second player
strLetter = InputBox("Enter A Letter:", "Letter", "", 500, 400)
'Convert letter to uppercase
strLetter = strWord.ToUpper
'Search the word for the letter
For intIndex = 0 To strWord.Length - 1
If strWord.Substring(intIndex, 1) = strLetter Then
'replace appropriate dash in the lblWord control
Mid(lblSecretWord.Text, intIndex + 1) = strLetter
'indicate that a replacement was made
blnIsDashReplaced = True
End If
Next intIndex
'Determine whether a replacement was made
If blnIsDashReplaced Then
intAllowedGuesses = -+1
If intAllowedGuesses = 0 Then
'if the word does not contain any dashes, then
'the user guessed the word, so the game is over
blnIsGameOver = True
MessageBox.Show("Great guessing!", "Hangman Game")
Else
'reset the blnDashReplaced variable
blnIsDashReplaced = False
End If
Else
'Proceesed when no dash was replaced
'Diaplay the incorrect letter, then update
'The incorrect counter variable, and show
'The appropriate picture box
lblIncorrectGuesses.Text = lblIncorrectGuesses.Text & " " & strLetter
intIncorrectCounter = intIncorrectCounter + 1
Select Case intIncorrectCounter
Case 1
pctPostBottom.Visible = True
Case 2
pctPostSide.Visible = True
Case 3
pctPostTop.Visible = True
Case 4
pctHead.Visible = True
Case 5
pctBody.Visible = True
Case 6
pctRightArm.Visible = True
Case 7
pctLeftArm.Visible = True
Case 8
pctRightLeg.Visible = True
Case 9
pctLeftLeg.Visible = True
blnIsGameOver = True
MessageBox.Show("Sorry, the word is " & strWord & ".", "Hangman Game", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Select
End If
End Sub
End Class