Click here to Skip to main content
11,567,325 members (45,824 online)
Click here to Skip to main content

Hangman Game

, 2 Mar 2010 CPOL 74.3K 6.7K 29
Rate this:
Please Sign up or sign in to vote.
A Hangman game as a console application
Hangman_Game_VS2005

Introduction

Hangman is a popular word guessing game where the player attempts to build a missing word by guessing one letter at a time. After a certain number of incorrect guesses, the game ends and the player loses. The game also ends if the player correctly identifies all the letters of the missing word.

Using the Code

The program consists of several classes. The class diagram is shown below:

Hangman_Class_Diagram.jpg

The main thing from the program is holding the guessed letters in an array collection and manipulating against the randomly picked word. In addition, you need to count the missing letters. Let's see the code that verifies the user's guessed letter and builds the word.

/// <summary>
/// Process the user guessed letter against the random picked word
/// </summary>
public void Play()
{
    guessed_FoundLetters = new List<string>();
 
    for (int i = 0; i < PickedWord.WordLength; i++)
    // Add underscore to the guessed and found string collection
    {
        guessed_FoundLetters.Add(" _ ");
    }
 
    for (int i = 0; i < PickedWord.WordLength; i++)
    {
        string letter = PickedWord.Content.Substring(i, 1);
        if (GuessedLetters.Count > 0)
        {
            foreach (string guessedLetter in this.GuessedLetters)
            {
                if (letter.Equals(guessedLetter.Trim().ToUpper()))
                // If the guessed letter is found from the picked
                // word then replace underscore with the letter
                {
                    guessed_FoundLetters.RemoveAt(i);
                    guessed_FoundLetters.Insert(i, " " + letter + " ");
                }
            }
        }
    }
    drawHangMan();
    Console.WriteLine(buildString(guessed_FoundLetters, false));
    Console.WriteLine();
}

The enumeration class is an indicator whether a user is winning or losing the game.

/// <summary>
/// Game result enumeration
/// </summary>
public enum GAMERESULT
{
    WIN,// Game is finished and player won the game
    LOSE,// Game is finished and player lose the game
    CONTINUE,// Continue play
}

The program class is the one which takes all the actions from the user and runs the game. The method that actually plays the game is shown below:

/// <summary>
/// Play game
/// </summary>
private static void playGame()
{
    Words words = new Words();
    Word pickedWord = words.Pick;           
    PlayHangman playHangman = new PlayHangman();
    playHangman.PickedWord = pickedWord;
    for (int i = 0; i < pickedWord.WordLength; i++)
    {
        Console.Write(" _ ");
    }
    Console.WriteLine();
    Console.WriteLine();
    Console.WriteLine();
    while (playHangman.Result() == GAMERESULT.CONTINUE)
    {
        Console.Write("Pick a letter --> ");
        ConsoleKeyInfo guessedLetter = Console.ReadKey();
        if (playHangman.AddGuessedLetters(guessedLetter.KeyChar))
            playHangman.Play();
    }
    if (playHangman.Result() == GAMERESULT.LOSE)
    {
        Console.WriteLine("So sorry. You struck out.");
        makeTextBlink("The mystery word was '" + 
                      pickedWord.Content.ToUpper() + "'",500);
        return;
    }
    else
    {
        makeTextBlink("You won !",500);
        return;
    }
}

Points of Interest

In this program, I learnt how to solve word guessing problems. I hope you enjoyed the Hangman game and the implementation.

History

  • February 27, 2010: First version
  • March 1, 2010: Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Wonde Tadesse
Software Developer (Senior)
United States United States
MSCS, MCTS, Senior Software Engineer, Craftsman ...

You may also be interested in...

Comments and Discussions

 
QuestionQuestion Pin
tas ka moka2-Jan-13 2:32
membertas ka moka2-Jan-13 2:32 
GeneralMy vote of 5 Pin
tas ka moka2-Jan-13 2:25
membertas ka moka2-Jan-13 2:25 
GeneralRe: My vote of 5 Pin
Wonde Tadesse8-Jan-13 12:10
memberWonde Tadesse8-Jan-13 12:10 
GeneralMy vote of 5 Pin
Kenneth Haugland27-Aug-12 10:32
memberKenneth Haugland27-Aug-12 10:32 
GeneralRe: My vote of 5 Pin
Wonde Tadesse13-Nov-12 12:12
memberWonde Tadesse13-Nov-12 12:12 
Questionnice work Pin
Member 876818921-Jun-12 2:27
memberMember 876818921-Jun-12 2:27 
I miss the old days of playing this type of game Big Grin | :-D now it seems more all about the graphics then the game play   Sigh | :sigh: but thats whats good about being a noob like me i get to start out making these good old games and have fun with them .

If you have any ideas as a place for me to start in C# then please let me know Big Grin | :-D
i have with Very little VB experience.

ive made about 40 applications on my own over this year just with what i got from the books and guides for VB.
AnswerRe: nice work Pin
Wonde Tadesse13-Nov-12 12:43
memberWonde Tadesse13-Nov-12 12:43 
GeneralMy vote of 5 Pin
Reza Ahmadi15-Apr-12 4:19
memberReza Ahmadi15-Apr-12 4:19 
GeneralRe: My vote of 5 Pin
Wonde Tadesse13-Nov-12 12:11
memberWonde Tadesse13-Nov-12 12:11 
GeneralMy vote of 5 Pin
Jαved16-Feb-12 3:57
memberJαved16-Feb-12 3:57 
GeneralRe: My vote of 5 Pin
Wonde Tadesse14-Apr-12 11:42
memberWonde Tadesse14-Apr-12 11:42 
Questiongobez Pin
abysinia9-Nov-11 23:07
memberabysinia9-Nov-11 23:07 
GeneralRe: gobez Pin
Wonde Tadesse10-Nov-11 2:44
memberWonde Tadesse10-Nov-11 2:44 
GeneralMy vote of 5 Pin
abysinia9-Nov-11 23:06
memberabysinia9-Nov-11 23:06 
GeneralExcellent Work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
yunegu mahesh reddy9-Mar-10 22:23
memberyunegu mahesh reddy9-Mar-10 22:23 
GeneralRe: Excellent Work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
Yewondwossen C.Tadesse (Wonde)10-Mar-10 6:02
memberYewondwossen C.Tadesse (Wonde)10-Mar-10 6:02 
GeneralBravo ! Pin
Kanou924-Mar-10 4:55
memberKanou924-Mar-10 4:55 
GeneralRe: Bravo ! Pin
Yewondwossen C.Tadesse (Wonde)4-Mar-10 5:03
memberYewondwossen C.Tadesse (Wonde)4-Mar-10 5:03 
GeneralNice! Pin
Nishad S1-Mar-10 13:40
memberNishad S1-Mar-10 13:40 
GeneralRe: Nice! Pin
Yewondwossen C.Tadesse (Wonde)1-Mar-10 15:44
memberYewondwossen C.Tadesse (Wonde)1-Mar-10 15:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 2 Mar 2010
Article Copyright 2010 by Wonde Tadesse
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid