Split Textasset into List

Hi there,

I’m writing a Scrabble prototype for fun just to learn the ropes of scripting and I’m stuck at checking the spelled word against a dictionary.

I’ve got a text document with a list of words separated by a new line. My original thought was to create an array of strings and have the game check the word spelled on the game board against these strings. At first when I was researching, I thought to use Array.Contains, but that function does not exist for the built-in array as I’ve found out, so I decided to have it check them manually.

//Variable assignment
public static string[] wordArray = new string[];

public TextAsset dict; //assigned in inspector

//In Start()
wordArray = dict.text.Split("

"[0]);

//In other script
if (tilesPlaced)
{
     for (int i=0; i<gameMaster.wordArray.Length;i++)
     {
          if (gameMaster.wordArray *== wordOnBoard)*

{
AddScore();
break;
}
}
}
This whole thing works, but is especially slow. I’m writing and testing with a computer running around 2.4GHz with four cores, and the search through the array takes almost 90 seconds. I realize it’s a lot of strings to search, but I imagine there must be a better way to implement this kind of check, similar to the way Words With Friends or Scrabble itself may do it.
I’ve tried to use ArrayList, List, and Dictionary but I’m not sure I’m implementing them correctly. I also can’t add them straight from the Split() command. The only way I can think to do it is to add an array, then transfer the information from the array to the List using a for loop in the Start function, but that would still require a lengthy initial load.
Does any code gurus out there have any ideas to help me get this together?

Alright I put my comment here as an answer like you had requested

If this is taking 90 seconds im assuming that you have quite a few strings as well, so you might want to go with the dictionary approach and use a dictionary of string lists.

This code is just to give you the general idea, its more or less right off the top of my head so hopefully you can just copy and paste it and it works

Dictionary<char, List<string>> m_stringDictionary
 
void Start()
{
    m_stringDictionary = new Dictionary<char, List<string>>();
 
    wordArray = dict.text.Split("

"[0]);

    List<string> tempList;
    foreach(string s in wordArray)
    {
        //check to see if there is already a list belonging to the first letter, and add the string to this list if there is
        if(m_stringDictionary.TryGetValue(s[0], out tempList)
        {
            tempList.Add(s);
        }
        else
        {
            m_stringDictionary.Add(s[0], new List<string>{s});
        }
    }
}

then everything will be sorted based on the first letter of the word, and you will have smaller lists to go through. Then you could use code similar to what I had posted earlier.

int wordOnBoardLength = wordOnBoard.Length;
char firstLetter = wordOnBoard[0];
 
for (int i=0; i<m_stringDictionary[firstLetter ].Length;i++)
{
    if(m_stringDictionary[firstLetter ]*.Length == wordOnBoardLength )*

{
if (m_stringDictionary[firstLetter ] == wordOnBoard)
{
AddScore();
break;
}
}
}
I find it interesting that you chose to go with a dictionary that has the word as both a key and a value. Definitely a quick true or false since dictionaries hash its keys into some number value internally, but i would think that the overhead would get very large as you add more strings.