I'm trying to make a word puzzle game, and for that I'm using a recursive method to find all possible words in the given letters. The letters is in a 4x4 board.
Like this:
ABCD
EFGH
HIJK
LMNO
The recursive method is called inside this loop:
for (int y = 0; y < width; y++)
{
for (int x = 0; x < height; x++)
{
myScabble.Search(letters, y, x, width, height, "", covered, t);
}
}
letters is a 2D array of chars.
y & x are ints that shows where in the board
width & height is also int, that tells the dimensions of the board
"" is the string we are trying to make (the word)
covered is an array of bools, to check if we already used that square.
t is a List (which contains all the words to check against).
The recursive method that need optimizing:
public void Search(char[,] letters, int y, int x, int width, int height, string build, bool[,] covered, List<aWord> tt)
{
// Dont get outside the bounds
if (y >= width || y < 0 || x >= height || x < 0)
{
return;
}
// Dont deal with allrady covered squares
if (covered[x, y])
{
return;
}
// Get Letter
char letter = letters[x, y];
// Append
string pass = build + letter;
// check if its a possible word
//List<aWord> t = myWords.aWord.Where(w => w.word.StartsWith(pass)).ToList();
List<aWord> t = tt.Where(w => w.word.StartsWith(pass)).ToList();
// check if the list is emphty
if (t.Count < 10 && t.Count != 0)
{
//stop point
}
if (t.Count == 0)
{
return;
}
// Check if its a complete word.
if (t[0].word == pass)
{
//check if its allrdy present in the _found dictinary
if (!_found.ContainsKey(pass))
{
//if not add the word to the dictionary
_found.Add(pass, true);
}
}
// Check to see if there is more than 1 more that matches string pass
// ie. are there more words to find.
if (t.Count > 1)
{
// make a copy of the covered array
bool[,] cov = new bool[height, width];
for (int i = 0; i < width; i++)
{
for (int a = 0; a < height; a++)
{
cov[a, i] = covered[a, i];
}
}
// Set the current square as covered.
cov[x, y] = true;
// Continue in all 8 directions.
Search(letters, y + 1, x, width, height, pass, cov, t);
Search(letters, y, x + 1, width, height, pass, cov, t);
Search(letters, y + 1, x + 1, width, height, pass, cov, t);
Search(letters, y - 1, x, width, height, pass, cov, t);
Search(letters, y, x - 1, width, height, pass, cov, t);
Search(letters, y - 1, x - 1, width, height, pass, cov, t);
Search(letters, y - 1, x + 1, width, height, pass, cov, t);
Search(letters, y + 1, x - 1, width, height, pass, cov, t);
}
}
The code works as I expected it to do, however it is very slow.. it takes about 2 mins to find the words.
EDIT: I clarified that the letters array is 2D
I want to show how I got the algorithm to work really fast. I used @Enigmativity 's implementation of a Trie, with the search pattern described by @EricLippert
public void SearchWord(char[,] letters, Trie parentTrie, char[] build, int x, int y, bool[,] covered )
{
char[] pass = new char[build.Length + 1];
build.CopyTo(pass, 0);
// iterate through all squares in the board.
for (var r = 0; r < letters.GetLength(0); r++ )
{
for (var c = 0; c < letters.GetLength(1); c++)
{
//check if this square is naighbor to the last square
if ((IsNeighbor(x, y, r, c)|| x == -1) && !(covered[r, c]))
{
// check if the current Trie contains the letter
if (parentTrie.ContainsKey(letters[r,c]))
{
pass[build.Length] = letters[r, c];
covered[r, c] = true;
SearchWord(letters, parentTrie[letters[r, c]], pass, r, c, covered);
covered[r, c] = false;
}
if (parentTrie.ContainsKey('$') && (!myStrings.Contains(new string(build).ToLower())))
myStrings.Add(new string(build).ToLower());
}
}
}
}
It is initially called by this:
SearchWord(letters, trie, new char[0], -1, -1, new bool[letters.GetLength(0), letters.GetLength(1)]);
I realize that I could add letters as a property, but since its a reference type it is not very time expensive
The other answers are right: you should abandon this algorithm entirely and start over.
The way to deal with these problems in word games is to transform the dictionary into a form that is amenable to the kinds of searches you want to do. In your case, the data structure you want to build is called a trie, which is a pun because it is a "tree" that does fast "re-trie-val", ha ha ha, those computer science guys are very witty!
The way a trie works is you have a tree where every node has up to 27 children. Suppose you have the dictionary { AB, ACE, ACT, CAT }. The trie looks like this:
root
/ \
A C
/ \ |
B C A
| / \ |
$ E T T
| | |
$ $ $
where $ means "the word is ended". Every path from the root to a $ is a legal word.
Now to find all the words, first build the trie out of the dictionary. Then for each starting point in the grid, try every possible word in the trie. If you start with a grid square that contains an A then of course you do not traverse down the C branch of the trie. Then for each child of the A in the trie, see if the grid has a neighbouring square that can drive you deeper into the trie.
That's the interesting recursive algorithm, and you'll find that it is very fast because you can be very smart about abandoning huge numbers of words that can't possibly exist starting from a given square.
Does that all make sense?