Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm writing a hangman code but i'm having a problem where words that have the same letter multiple times give me a weird output such as if the word would be ARRAY the output would be "A-R---RR--" also if a letter would be guessed wrong the if statement for it would still be displayed and also loop multiple times. How do I fix this?

    public string[] words = new string[5] { "ARRAY", "OBJECT", "CLASS", "LOOP", "HUMBER" };
    public string[] torture = new string[] { "left arm", "right arm", "left leg", "right leg", "body", "head" };
    int i;

    public void randomizedWord()
    {

        Random random = new Random();

        int index = random.Next(0, 5);
        char[] hidden = new char[words[index].Length];
        string word = words[index];
        Console.WriteLine(words[index]);

        Console.Write("The word is: ");
        for (i = 0; i < hidden.Length; i++)
        {
            Console.Write('-');
            hidden[i] = '-';
        }
        Console.WriteLine();

        int lives = 6;
        do
        {
            Console.WriteLine("Guess a letter: ");
            char userinput = Console.ReadLine().ToCharArray()[0];

            for (int i = 0; i < hidden.Length; i++)
            {

                if (word[i] == userinput)
                {
                    hidden[i] = userinput;

                    for (int x = 0; x < hidden.Length; x++)
                    {
                        Console.Write(hidden[x]);
                    }
                }
                if (userinput != hidden[i])
                {
                    Console.WriteLine("That is not a correct letter");
                    Console.WriteLine("You lost a " + torture[i]);
                    lives--;
                }
            }
            Console.WriteLine();
        } while (lives != 0);
          Console.WriteLine("You guessed right!");
          Console.ReadLine();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
139 views
Welcome To Ask or Share your Answers For Others

1 Answer

You're not waiting till you check all the letters. Check all the letters first...and at the same time you need to check if the letter matches any character. You remove a life if ANY letter doesn't match.

bool foundLetter = false
for (int i = 0; i < hidden.Length; i++)
{
    if (word[i] == userinput)
    {
        hidden[i] = userinput;
        foundLetter = true;
    }
}

Now that you've checked everything you can print out your word, after the for loop, not in it. Before you were printing out with every correct letter giving you a lot of extra characters.

for (int x = 0; x < hidden.Length; x++)
{
    Console.Write(hidden[x]);
}

and take away a life if NONE of the characters match instead of every incorrect letter. That would be a much harder version of the game. Again, after your for loop completes. Also, you were picking a body part based on the letter that was incorrect. If the 6th letter is wrong you are going to be out of bounds on the index for torture. Instead the number of lives should be torture.Length and you should use lives as the index.

if (!foundLetter)
{
    lives--;
    Console.WriteLine("That is not a correct letter");
    Console.WriteLine("You lost a " + torture[lives]);      
}

In addition, you only get to the "You guessed right!" after you used all your letters incorrectly. If you did guess right, you would then have to guess a bunch of wrong letters to get to this point. If you didn't guess right within the lives then it tells you that you won. I like this part of the game, games that you can't lose even you do are my specialty. ;-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...