0

am trying to make a hangman game where it picks a random word from a text file of words. It then displays the word in asterisks and asks the user to guess each letter of the word if they guess right it uncovers that letter.They keep playing until they guess all the letters in the word.After the word is guessed it will display the number of misses and ask if they want to play again.

The Problem I am having is when the word is guessed correctly it just keeps asking for a letter even if the word is uncovered. I am not sure how to fix this. I would like to do this without using linq if possible. any help would be appericated

 static void Main(string[] args)
    {
        char[] guessed = new char[26];
        char guess = ' ';
        char playAgain= ' ';
        bool validLetterInput = false;
        bool validAnswer = false;


        int amountMissed = 0, index = 0;

        do
        {
            // initilization of word and testword so that we could generate a testword with the same length as original
            char[] word = RandomLine().Trim().ToCharArray();

            char[] testword = new string('*', word.Length).ToCharArray(); 
            char[] copy = word;

            Console.WriteLine(testword);
            Console.WriteLine("I have picked a random word on animals");
            Console.WriteLine("Your task is to guess the correct word");

           //Check if the 2 arrays are equal     
            while (testword != word)
            {
                while (!validLetterInput)
                {
                    try
                    {
                        Console.Write("Please enter a letter to guess: ");
                        guess = char.Parse(Console.ReadLine().ToLower());
                        //Checks if guess is letter or not
                        if (((guess >= 'A' && guess <= 'Z') || (guess >= 'a' && guess <= 'z')))
                        {
                            validLetterInput = true;
                        }
                        else
                        {
                            Console.WriteLine("Invalid Input");
                        }


                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);

                    }
                }
                validLetterInput = false;

                bool right = false;
                for (int j = 0; j < copy.Length; j++)
                {
                    if (copy[j] == guess)
                    {
                        Console.WriteLine("Your guess is correct.");
                        testword[j] = guess;
                        guessed[index] = guess;
                        index++;
                        right = true;
                    }
                }
                if (right != true)
                {
                    Console.WriteLine("Your guess is incorrect.");
                    amountMissed++;
                }
                else
                {
                    right = false;
                }
                Console.WriteLine(testword);

            }
            Console.WriteLine($"The word is {string.Join("",testword)}. You missed {amountMissed} times.");
            while (!validAnswer)
            {
                try
                {
                    Console.WriteLine("Do you want to guess another word? Enter y or n: ");
                    playAgain = char.Parse(Console.ReadLine());
                    if(playAgain == 'y' || playAgain == 'Y' || playAgain == 'n' || playAgain == 'N')
                    {
                        validAnswer = true;
                    }
                    else
                    {
                        Console.WriteLine("Invalid input try again");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            validAnswer = false;
        } while (playAgain == 'y' || playAgain == 'Y');


        Console.WriteLine("Good-Bye and thanks for playing my Hangman game.");
    }
        public static string RandomLine()
    {

            // store text file in an array and return a random value
            string[] lines = File.ReadAllLines("E:\\Amimals1.csv");
            Random rand = new Random();
            return lines[rand.Next(lines.Length)].ToLower();



    }
}
  • 2
    This is a good chance to learn how the debugger works. You can put *breakpoints* in your code, *single step* through the statements in your code and *watch* the values of your variables whenever the program is stopped – Flydog57 Dec 02 '19 at 05:20

2 Answers2

0

There are various ways to compare two arrays / lists. Simple method i see for character arrays / lists is to convert them to strings and then compare.

Array Comparison thread on stackoverflow

testword.ToString() != word.ToString()

In regards to what Flydog57, use this link to learn how to debug code:

Jawad
  • 11,028
  • 3
  • 24
  • 37
  • The two questions do not seem to be duplicates. It is absolutely ok to ask two different questions about the same code. In fact if they _are_ two different questions, they should be asked as two different questions. However, if they _are_ duplicates and you become aware of that, please mark them as such. ("close" -> "duplicate" -> pick dupe) – Fildor Dec 02 '19 at 08:04
0

Compare the arrays with "SequenceEqual" instead of !=. This way you will need to change your while statement to:

while (!testword.SequenceEqual(word))

This is noted by Quartermeister and further explained by ohn Buchanan in the question "Easiest way to compare arrays in C#". link is here:

Easiest way to compare arrays in C#

Otherwise great program!

Note when you are trying debugging, a simple thing I like to put in there is a write of what response I want to see initially if I cannot figure out through debug. you can always remove the line later. I put this at the end of the while statement.

Console.WriteLine(testword.SequenceEqual(word));
John B
  • 120
  • 1
  • 9