HangMan Game in C#

This is a C# Program to create a HangMan game.

Problem Description

This C# Program Creates a HangMan Game.

Problem Solution

Here words are guessed by the user and the game is continued.

Program/Source Code

Here is source code of the C# Program to Create a HangMan Game. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Create a HangMan Game
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Hangman
{
    class Program
    {
        static void Main(string[] args)
        {
 
            Console.WriteLine("Welcome to Hangman!!!!!!!!!!");
            string[] listwords = new string[10];
            listwords[0] = "sheep";
            listwords[1] = "goat";
            listwords[2] = "computer";
            listwords[3] = "america";
            listwords[4] = "watermelon";
            listwords[5] = "icecream";
            listwords[6] = "jasmine";
            listwords[7] = "pineapple";
            listwords[8] = "orange";
            listwords[9] = "mango";
            Random randGen = new Random();
            var idx = randGen.Next(0, 9);
            string mysteryWord = listwords[idx];
            char[] guess = new char[mysteryWord.Length];
            Console.Write("Please enter your guess: ");
 
            for (int p = 0; p < mysteryWord.Length; p++)
                guess[p] = '*';
 
            while (true)
            {
                char playerGuess = char.Parse(Console.ReadLine());
                for (int j = 0; j < mysteryWord.Length; j++)
                {
                    if (playerGuess == mysteryWord[j])
                        guess[j] = playerGuess;
                }
                Console.WriteLine(guess);
            }
        }
    }
}
Program Explanation

In this C# program, we are reading any word using ‘guess’ variable. The words are guessed by the user and the game is continued. Using for loop we are entering the ‘*’ to the guess variable. Initialize the value of ‘p’ variable as 0, and check the condition that the value of ‘p’ variable value is less than length of the ‘mysteryWord’ variable.

advertisement
advertisement

While loop is used to execute the statement, if condition statement is used to check the value of ‘playerGuess’ variable is equal to the length of the value of ‘mysteryWord’ variable. If the condition is true then execute the statement.

Runtime Test Cases
 
Welcome to Hangman!!!!!!!!!!
Please enter your guess: i
****
a
**a*
e
**a*
g
g*a*
o
goa*
t
goat

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.