我在Mastermind应用程序中偶然发现了控制测试字符串的问题

时间:2014-01-17 14:47:05

标签: c# console-application

首先,我对编程很新,所以请记住这一点。我目前正在使用C#控制台应用程序完成MasterMind游戏。在测试我的不同方法时,我偶然发现了一个问题。我在system.debug中编写了所有测试方法响应,并且我从方法“isValidKey(string key)”中找到了方法“

我从“createSecretKey()”和MasterMind构造函数得到了一个“真实”的响应; 所以我认为他们没事。

所以,如果你能帮我找到“isVaildKey(字符串键”方法)的错误,我真的很感激。

所以这是我的代码:

命名空间MasterMind {

class MasterMindModel
{

    private string _secretKey;

    public MasterMindModel()
    {


        _secretKey = createSecretKey();

    }



    private static string createSecretKey()
    {
        string secretKey;
        string[] t_secretKey = new string[4];

        Random rnd = new Random();
        string symbols = "1234567";


        for (int i = 0; i <  4; i++)
        {
            char ch = symbols[rnd.Next(symbols.Length)];
            t_secretKey[i] += ch;

        }

        secretKey = t_secretKey[0] + t_secretKey [1] + t_secretKey[2] + t_secretKey[3];

        return secretKey;

    }


    public static bool SelfTest()
    {


        bool ok = true;

        for (int i = 0; i < 1000 && ok; ++i)
        {
            ok = isValidKey(createSecretKey());
        }

        System.Diagnostics.Debug.WriteLine("CreateSecretKey: " + ok);



        MasterMindModel model = new MasterMindModel();
        ok = isValidKey(model._secretKey);
        System.Diagnostics.Debug.WriteLine("MasterMindModel constructor: " + ok);

        ok = isValidKey("2361") && !isValidKey("2368")
             && !isValidKey("ABCD") && !isValidKey("+-*=")
             && !isValidKey("2301") && !isValidKey("23611")
             && !isValidKey("231");
        System.Diagnostics.Debug.WriteLine("MasterMindModel isValidKey " + ok);

        return ok;
    }

    public static bool isValidKey(string key)
    {


        Random rnd = new Random();
        string symbols = "1234567";

        for (int i = 0; i < 4; i++)
        {

          char ch = symbols[rnd.Next(symbols.Length)];
          key += ch;

      }


        if (key != "2368" && key != "ABCD" && key != "+-*=" && key != "2301" && key !=   "23611" && key != "231")
        {
            key = t_key[0] + t_key[1] + t_key[2] + t_key[3];
        }


        return true;


    }
}

} 编译器没有给我任何错误,我似乎也无法弄清楚问题是什么。

正如我所说,我很新,所以关于我应该研究什么以及我应该阅读更多关于等等的语法的任何提示我也会对它有所了解。

另外请告诉我这篇文章是否要vauge,我将删除它并再制作一个。这是我在这里的第一篇文章。

感谢。

2 个答案:

答案 0 :(得分:0)

我在你的方法中看到的直接问题是t_key没有在任何地方定义,无法解释为什么编译器没有抱怨,除非你没有显示所有的代码。

答案 1 :(得分:0)

您正在混合SelfTest和isValidKey函数中的测试用例。你想把它们放在一个地方。此外,您还希望通过不使用硬编码值来使代码更加灵活。如果不需要,也不要使用静态,您可能希望一次运行模型的多个实例。以下是我将如何做到你想要实现的目标:

class MasterMindModel
{
    private string createSecretKey(int keyLength, List<char> keyItems)
    {
        StringBuilder secretKey = new StringBuilder();

        for (int i = 0; i < keyLength; i++)
        {
            secretKey.Append(keyItems[_rnd.Next(keyItems.Count)]);
        }

        return secretKey.ToString();
    }


    private bool isValidKey(string key, int keyLength, List<char> keyItems)
    {
        if (key.Length != keyLength)
        {
            return false;
        }

        foreach (char c in key)
        {
            if (!keyItems.Contains(c))
            {
                return false;
            }
        }

        return true;
    }

    public void SelfTest()
    {
        int keyLength = 4;
        List<char> keyItems = new List<char> { '1', '2', '3', '4', '5', '6', '7' };

        Debug.Assert(isValidKey(createSecretKey(keyLength, keyItems), keyLength, keyItems));
        Debug.Assert(isValidKey(createSecretKey(keyLength, keyItems), 3, keyItems) == false);
        Debug.Assert(isValidKey(createSecretKey(keyLength, keyItems), keyLength, new List<char> { '0', '8', '9' }) == false);
    }

    private static Random _rnd = new Random();
}