Hangman Game (Guess the Word) Using Win Form in C#

You will enjoy this article; you are going to play the popular word guessing game "Hangman". This game is developed as a Windows Application in C#.

This word guessing game is very similar to the popular word guessing game "Hangman". The only difference is that here the computer is not drawing the "Hangman" picture on every wrong guess made by the guessing player.

This game is developed as a Windows Application in C#. The source code and exe of this game is also attached with this article.

Rules

This game is usually played by two persons. One player has to guess a word which is given by another player. Here you have to play this game with the computer.

In order to win this game, the guessing player has to guess the word correctly. While guessing the player can't make more than five wrong guesses.

If he suggests more than five wrong letters, he looses the game.
Initially, the word to guess is represented by a row of dashes, giving the number of letters.

If the guessing player suggests a letter which occurs in the word, then the other player (in this case the computer) writes it in all of its correct positions.

If the suggested letter does not occur in the word, then the guessing player looses his further attempts to guess.

The game is over when:

  • The guessing player completes the word, or guesses the entire word correctly
  • The guessing player makes more than five wrong guesses and unable to guess the word correctly

Logic used in the code:

  1. We take a random word from a hard coded string array and store it into a string "WordToFind".
  2. (Note: The source of the word list could be a text file having many words or could be a web service that gets a new word(s) from some site.)
  3. Convert the "Word to find" string into a character array.
  4. Added 26 buttons on the GUI. The player provides the input letter (A, B, C.. etc.) by pressing a button.
  5. When any input button is pressed, the letter is stored into a "CurrentInputLetter" string.
  6. Make another string "StringToDisplay" to display in the GUI for the intermediate status.
    (For example: If the word is "APPLE" then at some point of time, your "StringToDisplay" could be "A_PL_")
  7. Iterate each character in the "WordToFind" string and check each letter in the "WordToFind" character array to determine whether any letter matches with "CurrentInputLetter".

    • If we find any match, we keep appending that letter into "StringToDisplay".
    • If we don't find a match, then we append "_" into StringToDisplay. In addition to this, whenever the computer finds a wrong attempt (guess) by a player, the computer draws a small portion of the Hangman
    • The "StringToDisplay" is always displayed in a label in the GUI for the player to view his current progress.
     
  8. Let the player keep guessing until the computer completes its Hangman drawing or the player guesses all the letters in the word correctly.

Some Screen Shots of this game:

Hangman-Game1.jpg

Hangman-Game2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all