public bool InitalizeFromCriteria(int numLetters, int minValid, int maxValid, RegexBuilderOptions regexOptions) { foundWords = new List <string>(); correctWords = new List <string>(); int attempts = 0; while (true) { string word = dictionary.RandomWord(numLetters); string regex = RegexBuilder.GetRegex(word, regexOptions); List <char> listWord = new List <char>(word.ToCharArray()); constraint = new Constraint(regex, listWord); correctWords = dictionary.GetMatchingConstraint(constraint); if (correctWords.Count >= minValid && correctWords.Count <= maxValid) { break; } // Failsafe to prevent infinite loops if (attempts > 100) { return(false); } attempts++; } return(true); }
void CreateScriptableObject() { if (level == null) { level = new Level(GlobalValues.dictionaryPath); } string folderName = folderNames[folderNameIndex]; LevelData levelData = ScriptableObject.CreateInstance <LevelData>(); RegexBuilderOptions regexOptions = new RegexBuilderOptions(setLetters, startWithFirstLetter, endWithLastLetter); bool success = level.InitalizeFromCriteria(numLetters, minValid, maxValid, regexOptions); if (!success) { Debug.Log("Failed to find word with those critera."); return; } levelData.word = new string(level.Letters.ToArray()); levelData.regexDefinition = level.RegexDefinition; levelData.groupData = Resources.Load <GroupData>(groupDataResourcePath + "/" + folderName); int newLevelNumber = 0; // If the folder doesn't exist create it if (Array.IndexOf(Directory.GetDirectories(levelDataPath), folderName) == -1) { Directory.CreateDirectory(levelDataPath + "/" + folderName); } foreach (string path in Directory.GetFiles(levelDataPath + "/" + folderName)) { // Watch out for those .meta files if (Path.GetExtension(path) == ".asset") { string file = Path.GetFileNameWithoutExtension(path); int levelNumber = int.Parse(file); if (levelNumber >= newLevelNumber) { newLevelNumber = levelNumber + 1; } } } AssetDatabase.CreateAsset(levelData, string.Format(levelDataPath + "/" + folderName + "/{0}.asset", newLevelNumber)); AssetDatabase.SaveAssets(); EditorUtility.FocusProjectWindow(); Selection.activeObject = levelData; }
void OnGUI() { EditorGUILayout.LabelField("Regex text", EditorStyles.boldLabel); word = EditorGUILayout.TextField("Word:", word); startWithFirstLetter = EditorGUILayout.Toggle("Start with first letter of word", startWithFirstLetter); endWithLastLetter = EditorGUILayout.Toggle("End with last letter of word", endWithLastLetter); setLetters = EditorGUILayout.IntField("Num set letters", setLetters); if (GUILayout.Button("Generate Regex")) { RegexBuilderOptions regexOptions = new RegexBuilderOptions(setLetters, startWithFirstLetter, endWithLastLetter); regex = RegexBuilder.GetRegex(word, regexOptions); } if (regex != null) { GUILayout.Label(string.Format("Regex: {0}", regex)); } }
public static string GetRegex(string word, RegexBuilderOptions regexOptions) { bool startWithFirstLetter = regexOptions.startWithFirstLetter; bool endWithLastLetter = regexOptions.endWithLastLetter; int setLetters = regexOptions.setLetters; int startEndRequirements = (startWithFirstLetter ? 1 : 0) + (endWithLastLetter ? 1 : 0); if (startEndRequirements > setLetters) { throw new System.Exception(string.Format("Start and end settings require at least {0} setLetters, but setLetters was {1}", startEndRequirements, setLetters)); } if (word.Length < setLetters) { throw new System.Exception(string.Format("Word \"{0}\" only has {1} letters. But setLetters was more than that ({2}).", word, word.Length, setLetters)); } // Seperate each letter in word into a list List <string> regexComponents = new List <string>(); foreach (char letter in word.ToCharArray()) { regexComponents.Add(letter.ToString()); } string regex = "^"; // Ensure we can't remove the start or end letters // if the respective variable is set int startIndex = startWithFirstLetter ? 1 : 0; int endIndex = endWithLastLetter ? word.Length - 1 : word.Length; // While the number of letters is greater than setLetters // Pick a random component and set it to ".*" for (int i = 0; i < word.Length - setLetters; i++) { List <int> validLetters = regexComponents.FindAllIndexes(s => s != ".*"); // Remove first and last letters from the list of // letters that are valid to turn into ".*" if necessary if (startWithFirstLetter) { validLetters = validLetters.Skip(1).ToList(); } if (endWithLastLetter) { validLetters = validLetters.Take(validLetters.Count - 1).ToList(); } int index = validLetters[Random.Range(0, validLetters.Count)]; regexComponents[index] = ".*"; } // If we don't care what the first letter is than we can allow any letter to start if (!startWithFirstLetter) { regexComponents.Insert(0, ".*"); } // Allow the regex to end with anything if endWithLastLetter is false if (!endWithLastLetter) { regexComponents.Add(".*"); } regex += CreateRegexFromComponents(regexComponents); // If we don't care what the last letter is than allow it to end with anything regex += "$"; return(regex); }