猜游戏陷入无限循环

时间:2019-12-02 05:15:04

标签: c#

我试图制作一个game子手游戏,它从单词的文本文件中选择一个随机单词。然后以星号显示该单词,并要求用户猜出单词的每个字母(如果猜对了),并发现该字母,然后继续播放直到猜到单词中的所有字母。猜出单词后将显示数字错过,问他们是否想再次比赛。

我遇到的问题是,正确猜出该单词时,即使发现该单词,它也会不断要求输入字母。我不确定如何解决此问题。我想尽可能不使用linq来执行此操作。 任何帮助都会被

 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 个答案:

答案 0 :(得分:0)

有多种比较两个数组/列表的方法。我看到的用于字符数组/列表的简单方法是将它们转换为字符串,然后进行比较。

Array Comparison thread on stackoverflow

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

关于Flydog57,使用this link学习如何调试代码:

答案 1 :(得分:0)

使用“ SequenceEqual”而不是!=比较数组。这样,您将需要将while语句更改为:

while (!testword.SequenceEqual(word))

Quartermeister对此进行了说明,ohn Buchanan在问题“在C#中比较数组的最简单方法”中作了进一步解释。链接在这里:

Easiest way to compare arrays in C#

否则很棒的程序!

请注意,当您尝试调试时,我想输入的一个简单内容是,如果我无法通过调试弄清楚,我最初希望看到什么响应。您以后可以随时删除该行。我将此放在while语句的末尾。

Console.WriteLine(testword.SequenceEqual(word));
相关问题