Click here to Skip to main content
13,766,082 members
Click here to Skip to main content
Add your own
alternative version

Stats

142.5K views
10.5K downloads
41 bookmarked
Posted 27 Feb 2010
Licenced CPOL

Hangman Game

, 2 Mar 2010
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
Architect
United States United States
MSCS, MCTS, Senior Software Engineer, Architect, Craftsman, The Ultimate DEV ...
Azure Series


You may also be interested in...

Comments and Discussions

 
GeneralMessage Closed Pin
14-Aug-18 11:27
memberMember 1394929514-Aug-18 11:27 
QuestionHANGMAN Pin
Ubaid Bin Zafar15-Apr-18 1:30
memberUbaid Bin Zafar15-Apr-18 1:30 
AnswerRe: HANGMAN Pin
OriginalGriff15-Apr-18 1:31
protectorOriginalGriff15-Apr-18 1:31 
PraiseNICE ONE! Pin
Ratul Thakur8-Apr-16 8:31
memberRatul Thakur8-Apr-16 8:31 
Questionsource code Pin
Member 121803601-Dec-15 21:52
memberMember 121803601-Dec-15 21:52 
QuestionQuestion Pin
tas ka moka2-Jan-13 3:32
membertas ka moka2-Jan-13 3:32 
GeneralMy vote of 5 Pin
tas ka moka2-Jan-13 3:25
membertas ka moka2-Jan-13 3:25 
GeneralRe: My vote of 5 Pin
Wonde Tadesse8-Jan-13 13:10
professionalWonde Tadesse8-Jan-13 13:10 
GeneralMy vote of 5 Pin
Kenneth Haugland27-Aug-12 11:32
professionalKenneth Haugland27-Aug-12 11:32 
GeneralRe: My vote of 5 Pin
Wonde Tadesse13-Nov-12 13:12
professionalWonde Tadesse13-Nov-12 13:12 
Questionnice work Pin
flinchy321-Jun-12 3:27
memberflinchy321-Jun-12 3:27 
AnswerRe: nice work Pin
Wonde Tadesse13-Nov-12 13:43
professionalWonde Tadesse13-Nov-12 13:43 
GeneralMy vote of 5 Pin
Reza Ahmadi15-Apr-12 5:19
memberReza Ahmadi15-Apr-12 5:19 
GeneralRe: My vote of 5 Pin
Wonde Tadesse13-Nov-12 13:11
professionalWonde Tadesse13-Nov-12 13:11 
GeneralMy vote of 5 Pin
Jαved16-Feb-12 4:57
professionalJαved16-Feb-12 4:57 
GeneralRe: My vote of 5 Pin
Wonde Tadesse14-Apr-12 12:42
professionalWonde Tadesse14-Apr-12 12:42 
Questiongobez Pin
abysinia10-Nov-11 0:07
memberabysinia10-Nov-11 0:07 
GeneralRe: gobez Pin
Wonde Tadesse10-Nov-11 3:44
professionalWonde Tadesse10-Nov-11 3:44 
GeneralMy vote of 5 Pin
abysinia10-Nov-11 0:06
memberabysinia10-Nov-11 0:06 
GeneralExcellent Work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
Y Mahi9-Mar-10 23:23
memberY Mahi9-Mar-10 23:23 
GeneralRe: Excellent Work!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Pin
Wonde Tadesse10-Mar-10 7:02
professionalWonde Tadesse10-Mar-10 7:02 
GeneralBravo ! Pin
Kanou924-Mar-10 5:55
memberKanou924-Mar-10 5:55 
GeneralRe: Bravo ! Pin
Wonde Tadesse4-Mar-10 6:03
professionalWonde Tadesse4-Mar-10 6:03 
GeneralNice! Pin
Nishad S1-Mar-10 14:40
memberNishad S1-Mar-10 14:40 
GeneralRe: Nice! Pin
Wonde Tadesse1-Mar-10 16:44
professionalWonde Tadesse1-Mar-10 16:44 
Thanks.
MCTS
modified on Monday, June 13, 2011 9:24 PM

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181114.1 | Last Updated 2 Mar 2010
Article Copyright 2010 by Wonde Tadesse
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid