C# Corner - Hangman

By Mike Gold


Source Code: Hangman

Back when C# was in its infancy, and was still in beta, Mike Gold wrote a nice little application to play hangman with 61 words in it and included sound. His original article and original code can still be found at http://www.c-sharpcorner.com/UploadFile/mgold/hangman11142005040831AM/hangman.aspx?ArticleID=3ca3a99f-f9cb-460b-8708-0b2951162376.

The code consisted of five classes:

Class Description
Form1 The form upon which everything is drawn and where the keypresses are handled.
HangDude Draws the man and remembers how many times the player has missed a letter.
LetterManager Manages the letters underneath the hangman and remembers which ones have been guessed.
RandomWordManager Picks a random word to start the game.
WinLoseManager Determines if the player has won or lost.

The code available for download above is written using C# Express and incorporates many of the features written by Mike. It also includes new features added by myself, minus the sound (my daughter prefers it this way!). The code consists of ten classes. Five classes are the same or similar to the orginals:

Class Description
Form1 The form upon which everything is drawn and where the keypresses are handled.
Man Draws the man and remembers how many times the player has missed a letter. (This class uses a different drawing routine to draw the man).
LetterManager Manages the letters underneath the hangman and remembers which ones have been guessed.
RandomWordManager Picks a random word to start the game. (Modified to include over 1000 words in several categories).
WinLoseManager Determines if the player has won or lost.

The other five classes add rules and high scores etc to the game:

Class Description
EntryForm High Score Name Entry Form.
HighScoreForm Form to display the top ten highest scores.
HighScoreEntry Simple class to store name and associated score.
HighScoreTable Handles loading and saving of high score table.
RulesForm Form to display the rules from a rtf file.

Enjoy the game. My daughter certainly does.


Contents Mastermind >>

© Publicjoe, 2008