private void FindWords(List <Word> words, Word current, Grid grid, int position, int dimension, ParseDirection direction) { int dimensionCheck = direction == ParseDirection.Right ? grid.Width : grid.Height; int newPosition = position + (direction == ParseDirection.Right ? 1 : grid.Width); if (dimension >= dimensionCheck) { return; } int currentCount = current.Letters.Count; int added = 0; Cell cell = grid.Cells[position]; int objects = cell.Objects.Count; for (int j = 0; j < objects; j++) { Item item = cell.Objects[j]; if (item.ID != 0 && item.ID != short.MaxValue && !item.IsObject && item.Type == (byte)TextType.Letter) { Word newWord = added == 0 ? current : current.CopyWord(currentCount); added++; newWord.Letters.Add(item); newWord.EndPosition = newPosition; newWord.EndDimension = dimension + 1; if (newWord.IsValid(item.Position)) { words.Add(newWord); } else { FindWords(words, newWord, grid, newPosition, dimension + 1, direction); } } } }
private void Parse(List <Sentance> sentances, Sentance current, HashSet <int> calculated, Grid grid, int position, int dimension, ParseDirection direction) { int dimensionCheck = direction == ParseDirection.Right ? grid.Width : grid.Height; int newPosition = position + (direction == ParseDirection.Right ? 1 : grid.Width); if (dimension >= dimensionCheck) { if (current.Words.Count > 2) { sentances.Add(current); } return; } int currentCount = current.Words.Count; int added = 0; calculated.Add(position); Cell cell = grid.Cells[position]; int objects = cell.Objects.Count; for (int j = 0; j < objects; j++) { Item item = cell.Objects[j]; if (item.ID != 0 && item.ID != short.MaxValue) { if (!item.IsObject && item.Type == (byte)TextType.Letter) { List <Word> words = new List <Word>(); Word word = new Word(); word.Letters.Add(item); FindWords(words, word, grid, newPosition, dimension + 1, direction); for (int i = 0; i < words.Count; i++) { word = words[i]; Sentance newSentance = added == 0 ? current : current.Copy(currentCount); added++; newSentance.Words.Add(word); Parse(sentances, newSentance, calculated, grid, word.EndPosition, word.EndDimension, direction); } } else if (!item.IsObject || useObjects) { Sentance newSentance = added == 0 ? current : current.Copy(currentCount); added++; newSentance.Words.Add(item); Parse(sentances, newSentance, calculated, grid, newPosition, dimension + 1, direction); } } } if (added == 0 && current.Words.Count > 2) { sentances.Add(current); } }