Hangman Game with Source Code in C#

Another C Sharp Game application "The Hangman". This weekend, i programmed Hangman in C#. Its open source so you can check its coding & can also download Hangman game with its visual studio files & resources. It select its word randomly.

Hangman in C#

This program is too much long rather than being complicated. See below code, it shows how the program started. There are blank boxes at the top of windows form where the letters of a word go and you have to guess these letters. Each time you make a wrong guess, the program displays another part of the hanged man. If you did 5 mistakes, you lose. If you guess all of the letters, you win.

Source Code:

Explanation of How this Game Works:

Let’s explain. The event handler first disables the clicked button so the user can’t click it again. It then gets the button’s Tag property, converts it to the button’s letter, and determines whether the letter is in the current word or not.

If the letter is in the word, the program loops through the letters labels. If a label corresponds to the letter, the code displays the letter in the label. If any label’s text is still blank, then the user has not guessed all of the letters yet and the game continues. If the user has guessed all of the letters, the program displays the “you won” message and disables all of the keyboard buttons.

If the guessed letter is not in the word, the program hides the currently displayed skeleton image, increments CurrentPictureIndex to show the next skeleton image, and displays that image. Then if the current image is the last one, the program displays the “you lost” message, disables all of the keyboard buttons, and makes each label letter display its letter.

If you like you can invent a scoring system for the game and save statistics such as high scores and win/loss percentages in the registry or in program settings.

Additionals:

Also note that the dictionary that the program uses comes from words that are common to several dictionaries. That means a lot of the words are uncommon so the game is fairly hard. You can change the dictionary if you like to make the game easier. It might also be interesting to use a domain-specific dictionary containing words about a particular topic, for example programming.

Download:

You can download this Game application with Visual Studio & Exe files.

File Size : 242 KB
Dated : 21 April 2014
Coded by Sajjad Gull

I hope you understand this post. If you have any misconception, ask in our group.



Comments

  1. your good work …same as it is doing..and also ….I improve our GK about C#,,SO it is good
    I really impress.

  2. This is very good example of Hangman game in C#. It would look more better if you used animations. Thanks for the share.

  3. I am a high school IT student using visual studio and do not understand how to use this. It would be more helpful if it were put like a visual studio program but thanks anyway

Leave a Reply

Your email address will not be published. Required fields are marked *

Sajjad Arif Gul

Hi! I am Sajjad Arif Gul, Software Engineer, Web & Mobile Developer from Karachi Pakistan. I love sharing my knowledge that could possibly help someone’s life in some capacity. Happy Coding.