/// <summary> /// Pick names for a squad of six robots. Uses names from the list provided. /// Base names are non-repeating and start with different letters. /// </summary> /// <param name="names"></param> /// <returns>An array containing 6 strings.</returns> public string[] PickRobotSquadNames(List <string> names) { // call a class that can take the given list and break it into categories. ListConverter lc = new ListConverter(); Dictionary <char, List <string> > alphabetDict = lc.GetAlphabetDict(names); // fill chosenNames array with 6 letters from the alphabet AlphabetLettersForNames(); // use letter chosen in previous step as a key in the alphabetic dictionary. // pick a random name from the sublist found in the dict, to replace the letter. // if a sublist in the dict is empty, we leave the capitalized letter as the name. // robot names can be short, after all. int i = 0; foreach (string item in _chosenNames) { var list = alphabetDict[item.ElementAt(0)]; if (list.Count() > 0) { _chosenNames[i] = list[_r.Next(list.Count())]; } i++; } ApplyPrefix(); ApplyPostfix(); return(_chosenNames); }
/// <summary> /// Same as PickRobotSquadNames(List) but should be slightly more efficient with /// memory usage because it doesn't need a dictionary object for its alphabetic /// splitting process. /// </summary> /// <param name="names"></param> /// <returns>An array containing 6 strings for a "robot squad".</returns> public string[] PickRobotSquadNamesOptimized(List <string> names) { // call a class that can take the given list and break it into categories ListConverter lc = new ListConverter(); var listArray = lc.GetAlphabetizedArray(names); // fill chosenNames array with 6 letters from the alphabet AlphabetLettersForNames(); // use letter chosen in previous step as a key in alphabetized array of size 26. // pick a random name from the sublist found in the array, to replace the letter. // if a sublist in the array is empty, we leave the capitalized letter as the name. // robot names can be short, after all. int i = 0; foreach (string item in _chosenNames) { //var list = alphabetDict[item.ElementAt(0)]; int k = (int)Enum.Parse(typeof(AlphabetEnum), item.ElementAt(0).ToString()); var list = listArray[k]; if (list.Count() > 0) { _chosenNames[i] = list[_r.Next(list.Count())]; } i++; } ApplyPrefix(); ApplyPostfix(); return(_chosenNames); }