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.
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.
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.
You can download this Game application with Visual Studio & Exe files.
I hope you understand this post. If you have any misconception, ask in our group.
14 thoughts on “Hangman Game with Source Code in C#”
Its a little complicated. But its perfect. Too much is spent on this.
Yes.. too much time was spent on it but i did it in my free time… which either i would have passed on facebook
I love your open source projects. Great work.
Thanks for the kind words.
Not an easy one. 😛
:p . yes it is. But it doesnt have any bug. thats why its that much long.
your good work …same as it is doing..and also ….I improve our GK about C#,,SO it is good
I really impress.
Thanks Abdul Rahman for Appreciation. 🙂
How can i add Ç and Ë to the keyboard ?
you have to change a long piece of code to add these.. It loads buttons from Lists.
This is very good example of Hangman game in C#. It would look more better if you used animations. Thanks for the share.
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
Comments are closed.