I am making a ruzzle-esqe type board game that has rows and columns of letters that a player moves (switches two pieces at a time) to make connected letters form a word. This match can only exist in row or column configuration.
I’ve run into performance, as well as design issues. So far i’m iterating each row in the board and concatenating a single letter at a time into a “currentWord” string for that row, then using LINQ to search the entire dictionary (120,000 string list) for a word that is the same length (i thought to save search time) as well as contains a match on the currentWord (which since currentWord contains a concatenated list, this does not find words in the middle of the row, only from origin, but thats another topic). Then I reverse the word and re-do the operation to check the other way.
In addition to the logic not producing the entirely desired results yet, whenever I move a piece the move doesn’t complete until the word check method is completed, which takes almost an entire second, so there is lag in the game play during piece moves.
Can somebody look at my method for searching the dictionary for board matches and give me some feedback or alternative code to speed up this operation or perform it more efficiently? Bonus points for giving tips about how to search every permutation of the row efficiently.
void FindAllWords()
{
//Properties
_board.Rows = GetRows();
_board.Columns = GetColumns();
string currentWord = "";
foreach (string row in _board.Rows)
{
foreach(char letter in row)
{
currentWord += letter;
currentWord = currentWord.ToLower();
if (currentWord.Length > 1 && currentWord.Length <= _rows)
{
string reverseWord = currentWord.Reverse().ToString();
List<string> matchFoward = _wordGen.Dictionary.Where(x => x.Length == currentWord.Length).Where (y => y.Contains(currentWord)).ToList();
List<string> matchBackward = _wordGen.Dictionary.Where(x => x.Length == reverseWord.Length).Where (y => y.Contains(reverseWord)).ToList();
if (matchFoward.Count() > 0)
{
Debug.Log(matchFoward[0]);
}
if (matchBackward.Count() > 0)
{
Debug.Log(matchBackward[0]);
}
}
}
currentWord = "";
}
}
Edit** I considered using multithreading but it seems like an unnecessary additional work load if there is a less complex solution.