VB2022 VB2019 VB6 VB Sample Code About Us

Hangman Game



Our VB Hangman game is originated from the paper and pencil guessing game for two or more players. The game starts with one player thinks of a word and the other player tries to guess it by suggesting letters within a certain number of guesses.

The hidden word is represented by a row of dashes, representing each letter of the word. If the player correctly guesses a letter in the word, the other player writes it down in all its correct positions. If the suggested letter does not occur in the word, the other player draws one element of a hanged man figure as a tally mark, starting with the head, following by the body then the arm and so forth.

In addition, the guesser has the option of guessing the whole word when he thinks he has enough clues. If the word is correct, the game is over and the guesser wins. Otherwise, the other player penalizes the guesser by adding an element to the diagram. On the other hand, if the guesser commits enough number of incorrect guesses that allows the opponent to complete the hangman diagram, the game is also over, and the guesser loses. However, the guesser can also win by guessing all the letters or numbers that appear in the word, before the diagram is completed.

In our program, we need to draw a hangman diagram. I suggest you use the Line control to draw the diagram. It is also advisable to convert the Line control into a control array so that we can program it easily using a Loop or the If...Then...Else statement. In addition, we also need to place several Label controls onto the form for the purpose of displaying the letters. These Label controls also need to be converted into a control array and also make invisible at startup or replaying the game. Besides that, we also need to place several Line control below the Label control and make them as a control array and invisible at startup. Last but not least, we also need to design a keyboard by placing 26 command buttons onto the form for the user to choose the alphabet. We follow the design of the standard computer keyboard. The buttons must also be converted to a control array.

The Interface

The Code

		
		
Private Sub CmdKey_Click(Index As Integer)
Dim myletter, myword As String
Dim word(5) As String
Dim hit, count As Integer
hit = Val(Text1.Text) + 1
count = Val(Text2.Text) + 1

Text1.Text = hit
word(0) = "CAT"
word(1) = "DESK"
word(2) = "WATER"
word(3) = "FIRE"
word(4) = "ZEBRA"
word(5) = "EERIE"
myword = alphabet(0).Caption & alphabet(1).Caption & alphabet(2).Caption & alphabet(3).Caption & alphabet(4).Caption & alphabet(5).Caption


Select Case Index
Case Is = 0
myletter = "A"
Case Is = 1
myletter = "B"
Case Is = 2
myletter = "C"
Case Is = 3
myletter = "D"
Case Is = 4
myletter = "E"
Case Is = 5
myletter = "F"
Case Is = 6
myletter = "G"
Case Is = 7
myletter = "H"
Case Is = 8
myletter = "I"
Case Is = 9
myletter = "J"
Case Is = 10
myletter = "K"
Case Is = 11
myletter = "L"
Case Is = 12
myletter = "M"
Case Is = 13
myletter = "N"
Case Is = 14
myletter = "O"
Case Is = 15
myletter = "P"
Case Is = 16
myletter = "Q"
Case Is = 17
myletter = "R"
Case Is = 18
myletter = "S"
Case Is = 19
myletter = "T"
Case Is = 20
myletter = "U"
Case Is = 21
myletter = "V"
Case Is = 22
myletter = "W"
Case Is = 23
myletter = "X"
Case Is = 24
myletter = "Y"
Case Is = 25
myletter = "Z"
End Select

On Error GoTo error_handler
If myletter <> alphabet(0) And myletter <> alphabet(1) And myletter <> alphabet(2) And myletter <> alphabet(3) And myletter <> alphabet(4) And myletter <> alphabet(5) Then
count = Val(Text2.Text) + 1
Line1(count).Visible = True
Text2.Text = count

End If



If myletter = alphabet(0) Then
alphabet(0).Visible = True
End If

If myletter = alphabet(1) Then
alphabet(1).Visible = True
End If

If myletter = alphabet(2) Then
alphabet(2).Visible = True
End If
If myletter = alphabet(3) Then
alphabet(3).Visible = True
End If

If myletter = alphabet(4) Then
alphabet(4).Visible = True
End If

If myletter = alphabet(5) Then
alphabet(5).Visible = True

End If

If count > 4 Then
 MsgBox ("You lost!")
 If myword = word(0) Then
 alphabet(0).Visible = True
 alphabet(1).Visible = True
 alphabet(2).Visible = True

End If
 If myword = word(1) Then
 alphabet(0).Visible = True
 alphabet(1).Visible = True
 alphabet(2).Visible = True
  alphabet(3).Visible = True
 
End If
 If myword = word(2) Then
 alphabet(0).Visible = True
 alphabet(1).Visible = True
 alphabet(2).Visible = True
 alphabet(3).Visible = True
 alphabet(4).Visible = True
End If

If myword = word(3) Then
 alphabet(0).Visible = True
 alphabet(1).Visible = True
 alphabet(2).Visible = True
alphabet(3).Visible = True
End If

 If myword = word(4) Then
 alphabet(0).Visible = True
 alphabet(1).Visible = True
 alphabet(2).Visible = True
 alphabet(3).Visible = True
 alphabet(4).Visible = True
End If
 If myword = word(5) Then
 alphabet(0).Visible = True
 alphabet(1).Visible = True
 alphabet(2).Visible = True
 alphabet(3).Visible = True
 alphabet(4).Visible = True
End If
 
 
End If
If count <= 4 Then


If myword = word(0) And alphabet(0).Visible = True And alphabet(1).Visible = True And alphabet(2).Visible = True Then

MsgBox ("You win!")
End If
If myword = word(1) And alphabet(0).Visible = True And alphabet(1).Visible = True And alphabet(2).Visible = True And alphabet(3).Visible = True Then

MsgBox ("You win!")
End If
If myword = word(2) And alphabet(0).Visible = True And alphabet(1).Visible = True And alphabet(2).Visible = True And alphabet(3).Visible = True And alphabet(4).Visible = True Then

MsgBox ("You win!")
End If
If myword = word(3) And alphabet(0).Visible = True And alphabet(1).Visible = True And alphabet(2).Visible = True And alphabet(3).Visible = True Then

MsgBox ("You win!")
End If
If myword = word(4) And alphabet(0).Visible = True And alphabet(1).Visible = True And alphabet(2).Visible = True And alphabet(3).Visible = True And alphabet(4).Visible = True Then

MsgBox ("You win!")
End If
If myword = word(5) And alphabet(0).Visible = True And alphabet(1).Visible = True And alphabet(2).Visible = True And alphabet(3).Visible = True And alphabet(4).Visible = True Then

MsgBox ("You win!")
End If


End If

error_handler:






End Sub

Public Sub CmdStart_Click()
generate


End Sub
Sub generate()

Dim num, i As Integer
Dim myChr(), myword As String
Dim word(5) As String
Text1.Text = -1
Text2.Text = -1
For i = 0 To 5
Letter(i).Visible = False

Next

word(0) = "CAT"
word(1) = "DESK"
word(2) = "WATER"
word(3) = "FIRE"
word(4) = "ZEBRA"
word(5) = "EERIE"
For i = 0 To 5

Line1(i).Visible = False

Next

For i = 0 To 5
alphabet(i).Visible = False
alphabet(i).Caption = ""

Next

Randomize Timer

num = Int(Rnd() * 6)

myword = word(num)


If Len(myword) = 3 Then
Letter(0).Visible = True
Letter(1).Visible = True
Letter(2).Visible = True
ElseIf Len(myword) = 4 Then
Letter(0).Visible = True
Letter(1).Visible = True
Letter(2).Visible = True
Letter(3).Visible = True
ElseIf Len(myword) = 5 Then
Letter(0).Visible = True
Letter(1).Visible = True
Letter(2).Visible = True
Letter(3).Visible = True
Letter(4).Visible = True

End If


For i = 0 To Len(myword) - 1
ReDim myChr(i)
myChr(i) = Mid(myword, i + 1, 1)
alphabet(i) = myChr(i)
Next
End Sub

Public Sub Form_Load()
Dim word(4) As String
Text1.Text = -1

End Sub


Private Sub MenuExit_Click()
Unload Me

End Sub


Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy