/// <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()); }
public virtual bool FillLast(CodeTree toAdd) { if (child2 == null) { child2 = toAdd; return(true); } else if (child2.FillLast(toAdd)) { return(true); } else if (child1 == null) { child1 = toAdd; return(true); } else if (child1.FillLast(toAdd)) { return(true); } else { return(false); } }
/// <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()); }