繁体   English   中英

Hangman递归c#

[英]Hangman recursive c#

我试图在使用递归函数时制作一个关于刽子手的控制台应用程序。 我不明白我将如何在没有循环的情况下实现它。 我在没有递归的情况下完成了它并且工作正常,但是我需要将其修改为递归方法,以下代码是我的非递归代码。

static void Main(string[] args)
    {

        string HiddenWord = "csharp";

        //--make a dash array
        char[] dashes = new char[HiddenWord.Length];

        for (int i = 0; i < dashes.Length; i++)
        {
            dashes[i] = '_';
        }

        // --type dashes equal to array length
        for (int i = 0; i < dashes.Length; i++)
        {
            Console.Write(dashes[i] + "  ");
        }


        Console.WriteLine();

        int count = 0;
        //--ask the user to guess
        do
        {
            Console.WriteLine("Enter a letter");
            char letter = char.Parse(Console.ReadLine());

            for (int i = 0; i < HiddenWord.Length; i++)
            {
                //replace dash with letter
                if (HiddenWord[i] == letter)
                {
                    count++; //update the count to check when to exit
                    dashes[i] = letter;  //if correct letter put dash instead of letter

                    //display again dash with letters instead
                    for (int j = 0; j < dashes.Length; j++)
                    {
                        Console.Write(dashes[j] + " ");
                    }
                }
            }

            Console.WriteLine();
        } while (count < dashes.Length);

        Console.ReadLine();
    }

你需要这样的东西吗?

class Program
{
    static string HiddenWord = "";
    static char[] dashes;

    static void Main(string[] args)
    {
        HiddenWord = "csharp";
        dashes = new char[HiddenWord.Length];


        for (int i = 0; i < dashes.Length; i++)
        {
            dashes[i] = '_';
        }

        // --type dashes equal to array length
        for (int i = 0; i < dashes.Length; i++)
        {
            Console.Write(dashes[i] + "  ");
        }


        Console.WriteLine();

        int count = 0;
        //--ask the user to guess
        askUserToGuess(count);


        Console.ReadLine();
    }

    private static void askUserToGuess(int count)
    {
        Console.WriteLine("Enter a letter");
        char letter = char.Parse(Console.ReadLine());

        for (int i = 0; i < HiddenWord.Length; i++)
        {
            //replace dash with letter
            if ((HiddenWord[i] == letter) && (dashes[i] != letter))
            {
                count++; //update the count to check when to exit
                dashes[i] = letter;  //if correct letter put dash instead of letter

                //display again dash with letters instead
                for (int j = 0; j < dashes.Length; j++)
                {
                    Console.Write(dashes[j] + " ");
                }
            }
        }
        if (count < dashes.Length) askUserToGuess(count);
    }
}

在 (HiddenWord[i] == letter) 附近我添加了另一个控件 (dashes[i] != letter) 因为如果你点击 5 次“r”字符(没有检查破折号 []),程序无论如何都会停止

编辑:

    class Program
{
    static string HiddenWord = "";
    static char[] dashes;

    static void Main(string[] args)
    {
        int count = 0;
        HiddenWord = "csharp";
        dashes = new char[HiddenWord.Length];

        recursiveSetDashes(0);
        recursiveWriteDashes(0);
        Console.WriteLine();

        askUserToGuess(count);
        Console.ReadLine();
    }

    private static void recursiveWriteDashes(int v)
    {
        Console.Write(dashes[v] + "  ");
        v++; if (v < dashes.Length) recursiveWriteDashes(v);
    }

    private static void recursiveSetDashes(int i)
    {
        dashes[i] = '_';
        i++; if (i < dashes.Length) recursiveSetDashes(i);            
    }

    private static void askUserToGuess(int count)
    {
        Console.WriteLine("Enter a letter");
        char letter = char.Parse(Console.ReadLine());

        recursiveReplaceDashes(0,letter,ref count);

        if (count < dashes.Length) askUserToGuess(count);
    }

    private static void recursiveReplaceDashes(int v, char letter, ref int count)
    {
        if ((HiddenWord[v] == letter) && (dashes[v] != letter))
        {
            count++; dashes[v] = letter;  

            recursiveWriteDashes(0);
        }
        v++; if (v < dashes.Length) recursiveReplaceDashes(v, letter, ref count);
    }
}

要停止递归方法,您需要一个条件。 在您的情况下,您已经使用它:

} while (count < dashes.Length);

你需要在你的方法中构建它,无论是在开始还是在最后

编辑:(我删除了旧示例,如果您仍然需要代码,请查看之前的编辑)

C# 的美妙之处在于它有时非常直观。 中断递归的条件可以表述为:如果有任何破折号,则执行代码。 存在同名的方法,并且count变量已过时。 通过这种方式,您还可以避免在用户过于频繁地键入相同字母时错误地停止递归。 这是您的代码的略短版本:

public static char[] recursive2(string HiddenWord, char[] dashes)
{
    // do only if the are Any dashes in the array
    if (dashes.Any(x=>x == '_'))
    {                
        Console.WriteLine("Enter a letter");
        char letter = char.Parse(Console.ReadLine());

        for (int i = 0; i < HiddenWord.Length; i++)
        {
            //replace dash with letter
            //if correct letter put dash instead of letter ELSE put old character
            dashes[i] = HiddenWord[i] == letter ? letter : dashes[i];
        }

        //display again dash with letters instead
        Console.Write(String.Join(" ", dashes) + Environment.NewLine);

        // here happens the recursion! call the same method with the newly
        // changed parameters
        return recursive2(HiddenWord, dashes);
    }
    else
    {
        return "\nCongratulations!".ToCharArray();
    }
}

以下是准备游戏和调用方法的方法:

static void Main(string[] args)
{
    string HiddenWord = "csharp";

    //--make a dash array
    char[] dashes = new String('_',HiddenWord.Length).ToCharArray();

    //display dash separated by spaces
    Console.Write(String.Join(" ", dashes) + Environment.NewLine);

    // Call the recursive method and when it breaks write "Congratulation" to the console
    Console.WriteLine(String.Join("", recursive2(HiddenWord, dashes)));

    Console.ReadLine();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM