Word Guessing Game/Hangman

neymark

Member
Joined
Nov 9, 2016
Messages
8
Programming Experience
3-5
Any one have an idea how I would output the letters that are guessed correctly from a word that I enter compared to the random word generated.
The code below outputs the length of word using underscores but I would like to replace the underscores with letters that I enter in and have been guessed correctly to it like hangman, thanks!


C#:
Code:
private void btnClue_Click(object sender, EventArgs e)
        {
            char[] a = new char[randomWord.Length];//creates new array of random word length
            

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = '_';
            }// 

            
            for (int i = 0; i < a.Length; i++)
            {
                lblClue.Text += a[i].ToString() + " "; // outputs underscores for random words length
            }               

        }
 
Here's one option:
private string GetWordWithGuessedCharacters(string word, params char[] guessedCharacters)
{
    var chars = word.ToCharArray();
    var output = chars.Select(ch => guessedCharacters.Contains(ch) ? ch : '_').ToArray();

    return new string(output);
}
Try calling that method like this:
var output1 = GetWordWithGuessedCharacters("medicine", 'e', 'i');
or like this:
var output1 = GetWordWithGuessedCharacters("medicine", new[] {'e', 'i'});
As that shows, you just need to have the original word as a string and the correct guesses as chars.
 
Any one have an idea how I would output the letters that are guessed correctly from a word that I enter compared to the random word generated.
The code below outputs the length of word using underscores but I would like to replace the underscores with letters that I enter in and have been guessed correctly to it like hangman, thanks!


C#:
Code:
private void btnClue_Click(object sender, EventArgs e)
        {
            char[] a = new char[randomWord.Length];//creates new array of random word length
            

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = '_';
            }// 

            
            for (int i = 0; i < a.Length; i++)
            {
                lblClue.Text += a[i].ToString() + " "; // outputs underscores for random words length
            }               

        }

Thank you! But how would it work when i enter the word into a listbox and its stored as randomWord instead of calling the word in a parameter?
 
Good grief. You obviously must know what word the user is trying to guess. You simply pass that word. Where comes from is irrelevant. It's a string regardless. Stop waiting for people to post code you can copy and paste and start using your head when people post examples. I provided you with an example that demonstrates passing a string contain a word. Whether that string is a literal or a string variable or the Text of a TextBox or somewhere else is completely irrelevant because a string is a string.
 
Back
Top Bottom