Esempio n. 1
0
    /// <summary>
    /// Generates random code of a given length.
    /// </summary>
    /// <param name="symbols">The amount of code to generate, excluding `</param>
    /// <param name="prepend">True if this code goes before other code</param>
    /// <returns>Returns a string of code</returns>
    public static string Generate(int count, bool prepend, Random rng)
    {
        if (count < 2 && !prepend || count < 1 && prepend)
        {
            throw new ArgumentException("Count must be at least 2. (1 in prepend mode)");
        }
        int      slots = 2;
        CodeTree tree  = new CodeTree("`");

        while (slots < count)
        {
            tree.FillRandom(new CodeTree("`"), rng);
            slots++;
        }
        if (prepend)
        {
            tree.FillLast(new Leaf(""));
            slots--;
        }
        for (; slots > 0; slots--)
        {
            tree.FillLast(rng.Next() % 2 == 0 ? new Leaf("k") : new Leaf("s"));
        }
        return(tree.ToString());
    }
Esempio n. 2
0
    /// <summary>
    /// Generates random cruel code of a given length.
    /// </summary>
    /// <param name="symbols">The amount of code to generate, excluding `</param>
    /// <param name="prepend">True if this code goes before other code</param>
    /// <returns>Returns a string of code</returns>
    public static string GenerateCruel(int count, bool prepend, Random rng)
    {
        if (count < 2 && !prepend || count < 1 && prepend)
        {
            throw new ArgumentException("Count must be at least 2. (1 in prepend mode)");
        }
        int      slots = 2;
        CodeTree tree  = new CodeTree("`");

        while (slots < count)
        {
            tree.FillRandom(new CodeTree("`"), rng);
            slots++;
        }
        if (prepend)
        {
            tree.FillLast(new Leaf(""));
            slots--;
        }
        else
        {
            tree.FillRandom(new Leaf("c"), rng);
            slots--;
        }
        for (; slots > 0; slots--)
        {
            CodeTree added = new Leaf("");
            switch (rng.Next() % 101)
            {
            case 0:
                added = new Leaf("e");
                break;

            case 1:
                added = new Leaf("v");
                break;

            default:
                switch (rng.Next() % 4)
                {
                case 0:
                    added = new Leaf("s");
                    break;

                case 1:
                    added = new Leaf("k");
                    break;

                case 2:
                    added = new Leaf("s");
                    break;

                case 3:
                    added = new Leaf("i");
                    break;
                }
                break;
            }
            tree.FillLast(added);
        }
        return(tree.ToString());
    }
Esempio n. 3
0
 public override string ToString()
 {
     return(N + child1.ToString() + child2.ToString());
 }