I am writing a basic hangman game. I want to store a string Word as the word to be found by the user. My problem is that I want to display this word as underscores (like you do in a paper hangman) but keep it’s string value. A friend of mine said this could be done via Regex but researching it doesn’t seem to help me.
Share
As suggested by Farinha, but using
HashSetSome importang points: we are using a
HashSetbecause in the end the letters can have two states: discovered (present in the HS) or not discovered (not present). We initialize theHashSetpassing as a parameterStringComparer.InvariantCultureIgnoreCasebecause we considerLandlto be the same thing. To use theStringComparer.InvariantCultureIgnoreCasewe have to use anHashSet<string>(an HS ofstrings) instead of anHashSet<char>(an HS ofchars). So when using thediscovered.Contains()we have to convert theccharin astringwith aToStringWe could have done the ConvertWord in much less characters, but for today it’s enough 🙂
Ok… I’ll give you an example, in C# 4.0: