This is the source code for hangman in a console application. To run it, just start a new console application and replace any existing code with this:
Screenshot:
Image may be NSFW.
Clik here to view.![Name: hangman.png
Views: 23
Size: 27.1 KB]()
Code:
Option Strict On
Option Explicit On
Module Module1
Private wordlist As New List(Of String)
Private r As New Random
Sub Main()
Call AddWords()
Do Until True = False
Dim guessedword As String = wordlist.Item(r.Next(0, wordlist.Count))
Dim h As New Hangman
h.Text = guessedword
Do Until h.ShowRightLeg = True OrElse h.GetWord.Contains("_") = False
Console.WriteLine(h.Draw)
Console.WriteLine()
Console.WriteLine(h.GetWord)
Console.WriteLine()
Console.Write("Please enter in a letter: ")
Dim response As String = Console.ReadLine
If response.Length > 1 OrElse response.Length = 0 Then
Console.WriteLine("Please only enter in a letter.")
Else
If h.GuessedLetters.Contains(response) Then
Console.WriteLine("You've already guessed " & response)
Console.WriteLine("Press any key to continue...")
Console.ReadLine()
Else
Dim isstrike As Boolean = False
For i As Integer = 0 To h.Text.Length - 1
If h.Text.Substring(i, 1) = response Then
isstrike = False
Exit For
Else
isstrike = True
End If
Next
If isstrike = True Then
h.Strike()
End If
h.GuessedLetters.Add(response)
End If
If h.ShowRightLeg = True Then
Console.WriteLine(h.Draw)
Console.WriteLine()
Console.WriteLine("Sorry, but you lose.")
Console.WriteLine("Press any key to continue...")
Console.ReadLine()
ElseIf h.GetWord.Contains("_") = False Then
Console.WriteLine(h.GetWord)
Console.WriteLine()
Console.WriteLine("You guessed correctly, you win!")
Console.WriteLine("Press any key to continue...")
Console.ReadLine()
End If
End If
Console.Clear()
Loop
Loop
End Sub
Private Sub AddWords()
wordlist.AddRange({"dwelle", "active", "fair", "portal", "ant", "dodge", "territory", "state", "nation", "world", "remind", _
"letter", "summary", "day", "plan", "orange", "inspection", "picture", "photo", "yellow", "paper", "pen", _
"pencil", "lisp", "basic", "sharp", "percent", "colon", "plane", "axis", "ally", "zoo", "outpost", _
"willow", "wick", "minotaur", "human", "visual", "tramatic"})
End Sub
End Module
Public Class Hangman
Private word As String = String.Empty
Public Property Text() As String
Get
Return word
End Get
Set(ByVal value As String)
word = value
End Set
End Property
Private guessed As New List(Of String)
Public Property GuessedLetters() As List(Of String)
Get
Return guessed
End Get
Set(ByVal value As List(Of String))
guessed = value
End Set
End Property
Private head As Boolean = False
Public Property ShowHead() As Boolean
Get
Return head
End Get
Set(ByVal value As Boolean)
head = value
End Set
End Property
Private body As Boolean = False
Public Property ShowBody() As Boolean
Get
Return body
End Get
Set(ByVal value As Boolean)
body = value
End Set
End Property
Private lArm As Boolean = False
Public Property ShowLeftArm() As Boolean
Get
Return lArm
End Get
Set(ByVal value As Boolean)
lArm = value
End Set
End Property
Private rArm As Boolean = False
Public Property ShowRightArm() As Boolean
Get
Return rArm
End Get
Set(ByVal value As Boolean)
rArm = value
End Set
End Property
Private lLeg As Boolean = False
Public Property ShowLeftLeg() As Boolean
Get
Return lLeg
End Get
Set(ByVal value As Boolean)
lLeg = value
End Set
End Property
Private rLeg As Boolean = False
Public Property ShowRightLeg() As Boolean
Get
Return rLeg
End Get
Set(ByVal value As Boolean)
rLeg = value
End Set
End Property
Public Function Draw() As String
Dim line1 As String = "_____"
Dim line2 As String = "| |"
Dim line3 As String
Dim line4 As String
Dim line5 As String
Dim line6 As String = "_"
If head = True Then
line3 = "| O"
Else
line3 = "| "
End If
If body Then
If lArm Then
If rArm Then
line4 = "| -|-"
Else
line4 = "| -|"
End If
Else
line4 = "| |"
End If
Else
line4 = "| "
End If
If lLeg Then
If rLeg Then
line5 = "| / \"
Else
line5 = "| / "
End If
Else
line5 = "| "
End If
Return String.Format("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}{6}", Environment.NewLine, line1, line2, line3, line4, line5, line6)
End Function
Public Function GetWord() As String
Dim txt As String = word
For Each l As String In guessed
txt = txt.Replace(l, "_")
Next
Dim returnable As String = String.Empty
For i As Integer = 0 To word.Length - 1
If txt.Substring(i, 1) = "_" Then
returnable &= word.Substring(i, 1) & " "
Else
returnable &= "_ "
End If
Next
Return returnable
End Function
Public Sub Strike()
If head = False Then
head = True
ElseIf body = False Then
body = True
ElseIf lArm = False Then
lArm = True
ElseIf rArm = False Then
rArm = True
ElseIf lLeg = False Then
lLeg = True
Else
rLeg = True
End If
End Sub
End Class
Image may be NSFW.
Clik here to view.