Hangman Demo

This is a demo of the childhood game Hangman using ASP.NET!


      _   _   _   _   _   _   _   

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z


Source Code
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
      ResetGame()
    End If
  End Sub
  
  
  Sub ResetGame()
       'This is the first time the user is visiting the page,
       'use the defaults
       hangmanImage.ImageUrl = "/images/hang_0.gif"
       
       'Choose a random word from a text file
       Session("hangman_word") = GetRandomWord(Server.MapPath("/demos/hangmanWords.txt"))
       Session("wrong_guesses") = 0
       
       'Specify the current "guess", which is no letters guessed
       Dim i as Integer
       Dim initialGuess as String
       For i = 0 to Session("hangman_word").ToString().Length - 1         
         initialGuess &= "*"
       Next i       
       Session("current_word") = initialGuess       
       
       'Put in blanks for the various letters
       DisplayCurrentWord()
  End Sub  


  Sub DisplayCurrentWord()
    currentWord.Text = ""
  
    Dim i as Integer
    For i = 0 to Session("current_word").ToString().Length - 1
      If Session("current_word").ToString().Substring(i,1) = "*" then
        currentWord.Text &= "_   "
      Else
        currentWord.Text &= Session("current_word").ToString().Substring(i,1).ToUpper() & _
                            "   "
      End If
    Next i
  End Sub


  Sub LetterGuessed(sender as Object, e as CommandEventArgs)
    'First, make the letter selected disabled
    Dim clickedButton as LinkButton = FindControl(e.CommandArgument)
    clickedButton.Enabled = False
    clickedButton.ForeColor = Color.Red
    
    'Now, determine if the letter is in the word
    If Session("hangman_word").ToString().ToLower().IndexOf(e.CommandArgument.ToLower()) >= 0 then
      'The letter was found
      Dim i as Integer
      Dim current as String = String.Empty
      For i = 0 to Session("hangman_word").ToString().Length - 1
        If Session("hangman_word").ToString().Substring(i,1).ToLower() = e.CommandArgument.ToLower() then
          current &= Session("hangman_word").ToString().Substring(i,1)
        Else
          current &= Session("current_word").ToString().Substring(i,1)
        End If
      Next i
            
      Session("current_word") = current
      DisplayCurrentWord()
      
      'See if they have guessed the word correctly!
      If Session("hangman_word").ToString() = Session("current_word").ToString() then
        EndGame(True)
      End If
    Else
      'The letter was not found, increment the # of wrong guesses
      Session("wrong_guesses") = Convert.ToInt32(Session("wrong_guesses")) + 1
      
      'Update the hangman image
      hangmanImage.ImageUrl = "/images/hang_" & Session("wrong_guesses").ToString() & ".gif"

      If Convert.ToInt32(Session("wrong_guesses")) >= 6 then
        'Eep, the person has lost
        EndGame(False)
      End If  
    End If
  End Sub


  Sub EndGame(won as Boolean)
    If won then
        lblEndGameMessage.Text = "Congratulations!  You won!"
    Else
	    lblEndGameMessage.Text = "Sorry, you lost.  The correct word was: " & _
	           Session("hangman_word").ToString().ToUpper() 
	    lblEndGameMessage.ForeColor = Color.Red
    End If
    
    lblEndGameMessage.Text &= "<p><a href=""hangman.aspx"">Play Again!</a>"
  End Sub


  Function GetRandomWord(filePath as String)
    'Open the file
    Dim objTextReader as TextReader = File.OpenText(filePath)
    
    'Read in all the lines into an ArrayList
    Dim words as ArrayList = new ArrayList()
    Dim word as String = objTextReader.ReadLine()
    While Not word is Nothing
      words.Add(word)
      word = objTextReader.ReadLine()
    End While
    
    'Close the Text file
    objTextReader.Close()
    
    'Now, randomly choose a word from the word ArrayList
    Dim rndNum as Random = new Random()
    Dim iLine as Integer = rndNum.Next(words.Count)
    Dim selectedWord as String = words(iLine)
    
    Return selectedWord    
  End Function
</script>

<form runat="server">
  <asp:image id="hangmanImage" runat="server" />
  <p>
       
  <asp:literal runat="server" id="currentWord" />
  <p>
  <asp:LinkButton runat="server" id="A" CommandArgument="A" Text="A" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="B" CommandArgument="B" Text="B" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="C" CommandArgument="C" Text="C" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="D" CommandArgument="D" Text="D" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="E" CommandArgument="E" Text="E" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="F" CommandArgument="F" Text="F" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="G" CommandArgument="G" Text="G" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="H" CommandArgument="H" Text="H" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="I" CommandArgument="I" Text="I" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="J" CommandArgument="J" Text="J" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="K" CommandArgument="K" Text="K" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="L" CommandArgument="L" Text="L" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="M" CommandArgument="M" Text="M" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="N" CommandArgument="N" Text="N" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="O" CommandArgument="O" Text="O" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="P" CommandArgument="P" Text="P" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="Q" CommandArgument="Q" Text="Q" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="R" CommandArgument="R" Text="R" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="S" CommandArgument="S" Text="S" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="T" CommandArgument="T" Text="T" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="U" CommandArgument="U" Text="U" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="V" CommandArgument="V" Text="V" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="W" CommandArgument="W" Text="W" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="X" CommandArgument="X" Text="X" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="Y" CommandArgument="Y" Text="Y" OnCommand="LetterGuessed" /> |
  <asp:LinkButton runat="server" id="Z" CommandArgument="Z" Text="Z" OnCommand="LetterGuessed" />
  <p>
  <center>
      <asp:label id="lblEndGameMessage" runat="server"
          Font-Size="18pt" Font-Weight="Bold" />
  </center>
</form>


[Return to the article]