This is a simple hangman game written in C# (.NET 6). This was done as an example for a friend who's learning programming. Hopefully this can be useful to others as well.
- A word is selected from the list of words (value assigned to
choosenWord
). - We initialize an empty array that will contain all the letters that have been submitted. This array is called
letters
. - We also store a count of how many lives are left. This variable is called
lives
. - The main game loop runs as long as the lives have not reached 0.
- Inside the loop, the following happens:
- We loop through each letter of the choosen word.
- We compare if the letter has been submitted (using the
letters
array). If yes, we write out the letter and if not, we write the blank character (_
). - In this process, we also count all the remaining letters (everytime
_
is written, this value gets incremented, telling us how far the player is from finding the word or if he's done). - After the loop, we check if there are any characters left. If there aren't any (value is
0
), the player won and we can break out of the loop. - Now we read an input character and assign it to a value. We also need to make sure it's in lowercase as this will avoid the same character in different casings from being submitted.
- We also need to validate that the given character is valid. For this, a Regex (declared higher) is used. We are only checking for single letters from a to z. If the character is invalid, we show a message and skip the rest of the loop (
continue
here will put us to the top of the loop again). - Next, we check the submitted letter against our
letters
array to know if it has already been submitted. If that's the case, we just show a message and cut to the top again. - The letter being valid and not already in our array, we can now add it to our
letters
array. - Lastly, we need to know if the letter that was submitted is used in our word or not. If it's not, we decrease the lives by 1 and tell the user how many lives he has left.
- The last statement will only be reached once the loop has been exited which happens when the lives reach 0 or if it has been broken out (see point 5. iv.).
- If the user still have lives left, we display the win message, otherwise it's a loose message.
// Hangman example in C# (.NET 6 console application)
using System.Text.RegularExpressions;
// List of words that can be used for the game.
var words = new[]
{
"coffee",
"banana",
"airport",
"cryptography",
"computer",
};
// Pick a random word from the array.
var chosenWord = words[new Random().Next(0, words.Length - 1)];
// Regex with valid characters (single character between a and z).
var validCharacters = new Regex("^[a-z]$");
// Number of lives the player has before loosing.
var lives = 5;
// Empty array that will contain all the letters submitted by the player.
var letters = new List<string>();
// As long as there are lives left, the loop continues
while (lives != 0)
{
// Counter of characters left to guess.
var charactersLeft = 0;
// Loop through all the characters of the word
foreach (var character in chosenWord)
{
// Make the letter a string (easier for conditions).
var letter = character.ToString();
// If the letter in the loop is in our array of used letters, we write
// the letter, otherwise we show an underscore (as a letter left to
// guess).
if (letters.Contains(letter))
{
Console.Write(letter);
}
else
{
Console.Write("_");
// We also increase the count of letters left, used to track whether
// the game is finished or not.
charactersLeft++;
}
}
Console.WriteLine(string.Empty);
// If there are no characters left, the game is over and we can break out
// of the loop.
if (charactersLeft == 0)
{
break;
}
Console.Write("Type in a letter: ");
// Read the input character and transform it to lowercase to make it easier
// to compare to already used letters (we could fall into the scenario of a
// player inputting a capitalized letter that has already been submitted).
var key = Console.ReadKey().Key.ToString().ToLower();
Console.WriteLine(string.Empty);
// Using the declared Regex above, we check if the the submitted character
// is valid or not.
if (!validCharacters.IsMatch(key))
{
// If the character is invalid, we loop back to the beginning using the
// "continue" statement and let the user know of the error.
Console.WriteLine($"The letter {key} is invalid. Try again.");
continue;
}
// Is the letter already in our array of used letters?
if (letters.Contains(key))
{
// Only show a message to the user that the letter has already been
// used. We don't want to do anything else (for example storing it)
// with that.
Console.WriteLine("You already entered this letter!");
continue;
}
// If the letter has not already been used, we add it to our array of
// letters.
letters.Add(key);
// If the chosen word doesn't contain the given letter, we reduce the
// number of lives left by one.
if (!chosenWord.Contains(key))
{
lives--;
// If all the lives ran out, there's no point saying how many lives
// are left as the loose message will be shown.
if (lives > 0)
{
// Here, a ternary is used in the string to either how a pluralized
// version of "try" or the singular one, depending on how many
// lives are left.
Console.WriteLine($"The letter {key} is not in the word. You have {lives} {(lives == 1 ? "try" : "tries")} left.");
}
}
}
// If the user has lives left, we display the win message, otherwise we don't
if (lives > 0)
{
// This uses a ternary to pluralize the word lives unless there only one
// life left.
Console.WriteLine($"You won with {lives} {(lives == 1 ? "life" : "lives")} left!");
}
else
{
Console.WriteLine($"You lost! The word was {chosenWord}.");
}
Thanks, I think Regex is what I have been missing this whole time. My attempts were like an amalgamation of all these little tips but I wasn't able to figure it out. Do you guys think that instead of the array of random words, I could use user input from another player and store their input the same way, still comparing the strings and player2 inputs?
edit: never mind, i did it.
` // Hangman example in C# (.NET 6 console application)
this is all i changed at the top for a two player console app game