Hangman Array C# [英] Hangman Array C#

查看:80
本文介绍了Hangman Array C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个hang子手代码,但是我遇到了一个问题,即多次具有相同字母的单词会给我一个奇怪的输出,例如,如果该单词为ARRAY,则输出为 AR --- RR- -,如果一个字母被猜错了,它的if语句仍然会显示并且还会循环多次。我该如何解决?

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();
    }


推荐答案

您不会等到您检查所有字母。首先检查所有字母...同时需要检查字母是否与任何字符匹配。如果任何字母都不匹配,则删除生命。

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;
    }
}

现在您已经检查了所有可以打印的内容您的话,在for循环之后,不在其中。在打印出每个正确的字母之前,会给您很多额外的字符。

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]);
}

,如果没有匹配的字符而不是每个不正确的字母,则夺去生命。那将是一个更加困难的游戏版本。同样,在for循环完成之后。另外,您还根据错误的字母来选择身体部位。如果第6个字母错误,那么您将无法进入酷刑索引。相反,寿命数应为 torture.Length ,而您应使用 lives 作为索引。

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. ;-)

这篇关于Hangman Array C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆