【问题标题】:C# - Basic hangman game lives issueC# - 基本刽子手游戏生活问题
【发布时间】:2017-11-18 10:16:57
【问题描述】:

我会简短地说:基本的刽子手游戏,它循环播放玩家 2 的字母选择,如果它与玩家 1 单词的第一个字符不匹配,它将删除一个生命,第二个,直到它要么无论玩家 1 字长是多少条生命,都能找到匹配项或被删除。

我显然不希望这样,我希望它检查数组 - 如果没有匹配,则删除生命。

for (int i = 0; i < playerTwoGuesses.Length; i++)
        {
            Thread.Sleep(1400);
            Console.Write("Guess: ");
            count = 0;

            do
            {
                try
                {
                    playerTwoGuesses[i] = char.Parse(Console.ReadLine());
                    validGuess = true;
                }
                catch (Exception)
                {
                    Console.WriteLine("Please enter a single character only.");
                }
            } while (validGuess == false);


            for (int j = 0; j < playerOneDisguised.Length; j++)
            {
                if (playerOneCharacters[j] == playerTwoGuesses[i])
                {
                    playerOneDisguised[j] = playerTwoGuesses[i];

                }
                else
                {
                    lives = lives - 1;

                }
            }


            if (lives == 0)
            {
                Console.WriteLine("Oh no! It seems you've lost. Closing game in 5 seconds.");
                Thread.Sleep(5000);
                Environment.Exit(0);
            }


            Console.WriteLine(playerOneDisguised);


            for (int k = 0; k < playerOneDisguised.Length; k++)
            {
                if (playerOneDisguised[k] != '*')
                {
                    count = count + 1;
                    if (count == playerOneDisguised.Length)
                    {
                        Console.WriteLine("Congratulations you've won!");
                        Thread.Sleep(1000);
                        Console.WriteLine("Closing game in 5 seconds.");
                        Thread.Sleep(5000);
                        Environment.Exit(0);
                    }
                }
            }
        }

【问题讨论】:

  • 所以你可能想检查输入的字母是否包含在单词中。如果没有,一条生命就被移除了……

标签: c# arrays loops


【解决方案1】:

试试这个:

        bool match = false;
        for (int j = 0; j < playerOneDisguised.Length; j++)
        {
            for (int y = 0; y < playerTwoGuesses.Length; y++)
            {
                if (playerOneCharacters[j] == playerTwoGuesses[y])
                {
                    playerOneDisguised[j] = playerTwoGuesses[y];
                    match = true;
                }
            }

        }
        if (match == false) {

            lives = lives - 1;
        }
        // Reset it back to false
        match = false;   

【讨论】:

    【解决方案2】:

    与其自己循环遍历数组,不如询问猜测的字符是否存在于其中。然后你只需要一个 if 语句来处理猜测

     if (Array.Exists(playerTwoGuesses, element => element == lastGuess) {
    

    【讨论】:

    • 我可能读错了什么,但为什么playerTwoGuesses.Contains(lastGuess) 不起作用?
    • 你是对的,那会很好用。这是一个更好的答案,更具可读性
    猜你喜欢
    • 2016-02-13
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多