ARTICLE

Hangman Program Using C#

Posted by Mike Gold Articles | Games Programming C# January 19, 2002
Hangman was created to illustrate several features of C# including GDI+, string manipulation, array processing, using properties, as well as simple creation of objects.
Reader Level:
Download Files:
 



Fig 1. Hangman Game

Fig 2. Hangman UML Design reverse engineered in WithClass 2000

Hangman was created to illustrate several features of C# including GDI+, string manipulation, array processing, using properties, as well as simple creation of objects. As you can see from the diagram above, there are 5 main objects to the design. First there is a Form1 upon which everything is draw and also the key presses are captured. This form contains all the main objects which it uses to manage the game. The RandomWordManager that picks a random word to start the game, The LetterManager which manages the letters underneath the hangman and remembers which ones have been guessed, The WinLoseManager that handles determining if the player wins or loses and of course the HangDude who knows how to draw himself and remembers how many times the player missed a letter.

Below are the two main routines for processing Hangman Events:

The first routine, OnKeyPress is overriden in the form to capture key presses and decides what to do after the user guesses a character.

protected
override void OnKeyPress(KeyPressEventArgs e)
{
char j = e.KeyChar;
string bigJ = j.ToString().ToUpper();
j = bigJ.ToChar();
// determine if the game has already ended
if (TheWinLoseManager.YouLoseFlag || TheWinLoseManager.YouWinFlag )
{
if (j == 'N')
{
Application.Exit();
}
if (j == 'Y')
{
// restart the game
TheLetterManager = null;
TheLetterManager =
new LetterManager(TheWordManager.Pick());
TheLetterManager.SetPosition (20, 150);
TheWinLoseManager.Initialize();
TheMan.NumberOfMisses = 0;
this.Invalidate();
}
return;
}
// check to see if we got a letter
if (TheLetterManager.GotALetter(j) == false)
{
TheMan.NumberOfMisses++;
}
if (TheWinLoseManager.CheckForLoss(TheMan.NumberOfMisses))
{
TheWinLoseManager.YouLoseFlag =
true;
}
if (TheWinLoseManager.CheckForWin(TheLetterManager.GetNumberOfGuesses(), TheLetterManager.GetHiddenWord() ))
{
TheWinLoseManager.YouWinFlag =
true;
}
this.Invalidate();
}

The next routine, OnPaint, is overriden to determine how to draw the current state of the letters and the hangman:

protected override void OnPaint(PaintEventArgs pe)
{
Graphics g = pe.Graphics;
// draw the background
Rectangle rect = this.ClientRectangle;
LinearGradientBrush lBrush =
new LinearGradientBrush(rect, Color.Red, Color.Yellow,
LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lBrush, rect);
// draw the hangman
TheMan.DrawAllBasedOnMisses(g);
// draw the letters
TheLetterManager.DrawLetters(g);
if (TheWinLoseManager.YouLoseFlag == true)
{
TheWinLoseManager.YouLose(g);
TheWinLoseManager.PlayAgain(g);
}
else if (TheWinLoseManager.YouWinFlag == true)
{
TheWinLoseManager.YouWin(g);
TheWinLoseManager.PlayAgain(g);
}
}

The code is included so that you can compile it and play for fun!

COMMENT USING