1

I'm creating a simple hangman game in which the console prints blanks, and if a character that is in the word to be guessed is entered by the user, i want a blank to be removed. This way once all the correct letters are entered there wont be any blanks, and the game will be won. Im stuck on how I should remove a blank if a correct letter is entered.

I'm using this as my method to generate blanks:

string blanks = "-";
for (int i = 0; i < wordlength; i++)
{
        blanks += "-";
}

where wordlength is the length of the word, defined earlier in the code (as is the word itself to be guessed). I don't know how i should remove the blanks however after a correct letter is entered. If any elaboration is necessary please specify as to what and i'll do my best to make it clearer.

Assafs
  • 3,257
  • 4
  • 26
  • 39
Anya
  • 395
  • 2
  • 13
  • What do you mean by removing blanks? And what conditions are? – Yurii N. Sep 04 '17 at 18:03
  • After a correct letter (which is a letter from the word) is entered, i want to delete one of the 'blanks' which is a "-" from the string 'blanks'. The condition is after a letter which is found in the word is entered by the user. – Anya Sep 04 '17 at 18:05
  • You could try something like this: `blanks = blanks..Remove(blanks.Length - 1)`. You also need to check of last character is `-` – Yurii N. Sep 04 '17 at 18:11
  • 1
    Try this [solution](https://stackoverflow.com/a/5015636/4471013) – Abdur Rahim Sep 04 '17 at 18:11
  • Try something like this: `string blanks = "-----------"; blanks = blanks.Remove(blanks.IndexOf("-"), 1);` – Youssef13 Sep 04 '17 at 18:11
  • @youssef13 this is not how hangman game works.… you need to replace matching space with letter asAbdur Rahim suggested. – Alexei Levenkov Sep 04 '17 at 18:15
  • Try this: https://i.imgur.com/KuhPCoQ.jpg – Youssef13 Sep 04 '17 at 18:58
  • @Youssef13 Could you please explain what's happening on the 'string maskedword' line? I don't quite understand everything there. – Anya Sep 04 '17 at 19:16
  • well, This line checks if the correctlyEnteredCharacters is empty, If it's empty, the value of the maskedWord will be what is between **?** and **:** which is repeated **-**, the repeated count is the same as the word length. If the correctedEnteredCharacters isn't empty, the value of maskedWord will be what after **:** which replaces every character isn't in correctlyEnteredCharacters with **-** – Youssef13 Sep 04 '17 at 19:35
  • Maybe the first assignment of the maskedWord changed to this because the string.IsNullOrEmpty will return true as the user didn't entered any characters: `string maskedWord = new string('-', word.Length)` – Youssef13 Sep 04 '17 at 19:38
  • Check this link: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator – Youssef13 Sep 04 '17 at 19:43
  • @Youssef13 Lastly, how do the last brackets in that line replace the word? I'm somewhat familiar with regex since my first language is python, and i'm assuming those are metacharacters, but I don't know exactly how they're replacing the blanks. – Anya Sep 04 '17 at 19:43
  • Let's take a simple example, This regex: `[^a]` will match every character except **a** character. This one: `[^abc]` will match every character except a & b & c, so the regex line matches non entered characters and replaces them with the blank **-**. so it's not replacing blanks with the entered characters, but replaces non entered characters with blanks in the real word. – Youssef13 Sep 04 '17 at 19:47
  • Yes, thank you I just found a link explaining the same. Turns out the metacharacters for python are the same as those for C#. The help is appreciated. – Anya Sep 04 '17 at 19:49
  • Yes, Regex is almost same over the programming languages. :) – Youssef13 Sep 04 '17 at 19:53

0 Answers0