If you’re looking for a fun way to sharpen your C# skills while building something tangible, I think the best thing you can do is write something that is both familiar and exciting.
How would you feel about writing your very first own computer game?
Writing a simple Hangman game as a console application is an awesome exercise. Not only is it a great way to improve your problem-solving abilities, but it also helps you practice working with loops, conditionals, arrays, string manipulation, and user input handling—all essential building blocks of a solid C# developer.
In this article, we’ll walk through the process of creating a Hangman game in C# and explore what we can learn from it. I’ll break things down in a way that makes it fun, practical, and easy to understand while keeping the code clean and maintainable.
Let’s get to it!
What is Hangman?
Hangman is a classic word-guessing game. The objective is simple:
- The game randomly selects a word.
- The player guesses letters one at a time.
- If the guessed letter is in the word, the letter is revealed in the correct position(s).
- If the guessed letter is wrong, the player loses an attempt.
- The player wins if they guess all the letters correctly before running out of attempts.
- The game ends when the player either guesses the word or runs out of attempts.
This makes Hangman a great beginner-friendly programming project while still providing valuable coding experience. The coding is beginner-friendly, but complex enough to present some
Writing the Hangman Game in C#
Let’s start with the code for our C# console-based Hangman game. We’ll then break it down step by step.
Full Hangman Code
using System;
class HangmanGame
{
static void Main()
{
string[] words = { "tutorial", "coding", "programmer", "csharp" };
Random random = new Random();
string selectedWord = words[random.Next(words.Length)];
char[] wordToGuess = selectedWord.ToCharArray();
char[] guessedLetters = new char[wordToGuess.Length];
for (int i = 0; i < guessedLetters.Length; i++)
{
guessedLetters[i] = '_';
}
int attemptsLeft = 10;
while (attemptsLeft > 0)
{
Console.WriteLine("Current word: " + new string(guessedLetters));
Console.Write("Guess a letter: ");
char guess = char.ToLower(Console.ReadKey().KeyChar);
Console.WriteLine();
bool correctGuess = false;
for (int i = 0; i < wordToGuess.Length; i++)
{
if (wordToGuess[i] == guess)
{
guessedLetters[i] = guess;
correctGuess = true;
}
}
if (!correctGuess)
{
attemptsLeft--;
Console.WriteLine("Incorrect! Attempts left: " + attemptsLeft);
}
if (new string(guessedLetters) == selectedWord)
{
Console.WriteLine("Congratulations! You guessed the word: " + selectedWord);
Console.ReadLine();
break;
}
}
if (attemptsLeft == 0)
{
Console.WriteLine("Sorry, you have run out of attempts. The word was: " + selectedWord);
Console.ReadLine();
}
Console.WriteLine("Thanks for playing Hangman!");
Console.ReadLine();
}
}
Breaking Down the Code
Now that we have our game, let’s go through the key components and what we can learn from them.
1. Using Arrays for Word Storage
string[] words = { "tutorial", "coding", "programmer", "csharp" };
Instead of hardcoding a single word, we store multiple words in an array, making the game more dynamic. This is a simple data structure practice.
2. Random Word Selection
Random random = new Random();
string selectedWord = words[random.Next(words.Length)];
Here, we use Random
to randomly select a word, which introduces us to working with pseudo-random numbers in C#.
3. Character Arrays for Word Representation
char[] wordToGuess = selectedWord.ToCharArray();
char[] guessedLetters = new char[wordToGuess.Length];
Using char[]
helps us easily manage word manipulation, which is often more efficient than string operations when dealing with single characters.
4. Initializing the Guessed Word with Underscores
for (int i = 0; i < guessedLetters.Length; i++)
{
guessedLetters[i] = '_';
}
This loop fills our guessedLetters
array with underscores (_
) to represent unguessed letters. A practical example of looping and array manipulation.
5. The Game Loop and User Input
while (attemptsLeft > 0)
The game runs inside a while
loop, continuing until the player either wins or runs out of attempts.
6. Processing User Guesses
char guess = char.ToLower(Console.ReadKey().KeyChar);
This captures the player’s input and ensures it’s case-insensitive.
7. Checking the Guess
for (int i = 0; i < wordToGuess.Length; i++)
{
if (wordToGuess[i] == guess)
{
guessedLetters[i] = guess;
correctGuess = true;
}
}
We loop through the word and update the guessedLetters
array if the guessed letter matches.
8. Tracking Attempts
if (!correctGuess)
{
attemptsLeft--;
Console.WriteLine("Incorrect! Attempts left: " + attemptsLeft);
}
Incorrect guesses reduce the remaining attempts. A simple yet effective way to track game state.
9. Checking for a Win Condition
if (new string(guessedLetters) == selectedWord)
Converting guessedLetters
back into a string lets us compare it with selectedWord
, determining if the player has won.
10. Handling Game Over
if (attemptsLeft == 0)
{
Console.WriteLine("Sorry, you have run out of attempts. The word was: " + selectedWord);
}
When attemptsLeft
reaches zero, we inform the player and end the game.
What You Learn from This Project
- Basic C# Syntax and Structure
- Working with Arrays
- Using Loops for Iteration
- Handling User Input
- String and Character Manipulation
- Implementing Game Logic
- Tracking and Updating Game State
Building small projects like this is the best way to solidify programming skills while having fun. If you want to expand the game, try adding word categories, difficulty levels, or even ASCII art for the Hangman drawing!
There you have it—a fully functional Hangman game in C#! Writing simple games like this is a fantastic way to learn, practice, and have fun with coding. If you’re serious about improving your C# skills, keep experimenting and build more small projects!
Check out this tutorial from our friends on Youtube: C-Sharp HUB channel
They have very good tutorials and a down-to-earth attitude towards teaching, which I admire very much.
Suggested reading; books that explain this topic in
depth:
– “C# Player’s Guide” (4th Edition) – by RB Whitaker —>
see on Amazon.com
– “Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET” – by Andrew Stellman & Jennifer Greene
—> see on
Amazon.com
– “C# Game Programming for Absolute Beginners” – by Jerry Lee Ford Jr. —>
see on Amazon.com