Option Explicit On
Option Strict Off
Public Class MainForm
Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub playButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles playButton.Click
'Simulates the Hangman game
Dim word As String
Dim letter As String
Dim isDashReplaced As Boolean
Dim isGameOver As Boolean
Dim incorrectCounter As Integer
' hide the picture boxes
bottomPictureBox.Visible = False
postPictureBox.Visible = False
topPictureBox.Visible = False
ropePictureBox.Visible = False
headPictureBox.Visible = False
bodyPictureBox.Visible = False
rightArmPictureBox.Visible = False
leftArmPictureBox.Visible = False
rightLegPictureBox.Visible = False
leftLegPictureBox.Visible = False
'get a 5*letter word from the first player
word = InputBox("Enter a word:", "Hangman Game")
Do Until word.Length >= 0
word = InputBox("Enter a word:", "Hangman Game")
Loop
'convert the word to uppercase
word = word.ToUpper()
'Display dashes in the wordLabel
'and clear the incorrectLabel
If word.Length = 1 Then
wordLabel.Text = "-"
End If
If word.Length = 2 Then
wordLabel.Text = "--"
End If
If word.Length = 3 Then
wordLabel.Text = "---"
End If
If word.Length = 4 Then
wordLabel.Text = "----"
End If
If word.Length = 5 Then
wordLabel.Text = "-----"
End If
If word.Length = 6 Then
wordLabel.Text = "------"
End If
If word.Length = 7 Then
wordLabel.Text = "-------"
End If
If word.Length = 8 Then
wordLabel.Text = "--------"
End If
If word.Length = 9 Then
wordLabel.Text = "---------"
End If
If word.Length = 10 Then
wordLabel.Text = "----------"
End If
incorrectLabel.Text = String.Empty
'allow the second player to guess a letter
'the game is over when the second player
'either guesses the word or makes 10
'incorrect guesses
Do Until isGameOver
'get a letter from the second player, then
'convert the letter to uppercase
letter = InputBox("Enter a letter", _
"letter", "", 550, 400)
letter = letter.ToUpper
'Search the word for the next letter
For indexNum As Integer = 0 To 9
'if the word contains the letter, then
'replace the dash in the wordLabel and
'assign True to isDashReplaced to
'indicate that a replacement was made
If word.Substring(indexNum, 1) = letter Then
Mid(wordLabel.Text, indexNum + 1) = letter
isDashReplaced = True
End If
Next indexNum
'determine wether a dash was replaced
If isDashReplaced Then
'if the word does not contain any dashes,
'then the game is over because the second
'player guessed the word; otherwise, reset
'the isDashReplaced variable for the next
'search
If wordLabel.Text.Contains("-") = False Then
isGameOver = True
MessageBox.Show("Great guessing!", "Hangman Game", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
isDashReplaced = False
End If
Else 'processed when no dash was replaced
'display the incorrect letter, then update
'the incorrect counter variable, then show
'the appropriate picture box
incorrectLabel.Text = incorrectLabel.Text & " " & letter
incorrectCounter = incorrectCounter + 1
Select Case incorrectCounter
Case 1
bottomPictureBox.Visible = True
Case 2
postPictureBox.Visible = True
Case 3
topPictureBox.Visible = True
Case 4
ropePictureBox.Visible = True
Case 5
headPictureBox.Visible = True
Case 6
bodyPictureBox.Visible = True
Case 7
rightArmPictureBox.Visible = True
Case 8
leftArmPictureBox.Visible = True
Case 9
rightLegPictureBox.Visible = True
Case 10
leftLegPictureBox.Visible = True
isGameOver = True
MessageBox.Show("Sorry, the word is " _
& word & ".", "Hangman Game", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Select
End If
Loop
End Sub
End Class