Hangman递归c#

时间:2016-09-28 08:10:42

标签: c# recursion

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

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

2 个答案:

答案 0 :(得分:1)

你需要这样的东西吗?

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)我添加了另一个控件(破折号[i]!=字母),因为如果你点击5次“r”字符(没有检查破折号[]),程序就会停止< / p>

编辑:

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

答案 1 :(得分:0)

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

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