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); } }
public Sentance Copy(int copyAmount = int.MaxValue) { Sentance sentance = new Sentance(); int count = Words.Count; for (int i = 0; i < count; i++) { if (i < copyAmount) { sentance.Words.Add(Words[i]); } } return(sentance); }
private void ParseRules(Grid grid) { List <Sentance> sentances = new List <Sentance>(); int x = 0; int y = 1; HashSet <int> calcRight = new HashSet <int>(); HashSet <int> calcDown = new HashSet <int>(); int size = grid.Cells.Count; for (int i = grid.Width; i < size; i++) { if (x > 0) { if (!calcRight.Contains(i)) { calcRight.Add(i); Parse(sentances, new Sentance(), calcRight, grid, i, x, ParseDirection.Right); } if (!calcDown.Contains(i)) { calcDown.Add(i); Parse(sentances, new Sentance(), calcDown, grid, i, y, ParseDirection.Down); } } x++; if (x >= grid.Width) { x = 0; y++; } } size = sentances.Count; for (int i = 0; i < size; i++) { Sentance sentance = sentances[i]; SentanceParser parser = new SentanceParser(sentance, useObjects); List <Rule> rules = parser.Parse(); for (int j = rules.Count - 1; j >= 0; j--) { Rule rule = rules[j]; if (!ContainsRule(Rules, rule)) { Rules.Add(rule); } } } }
public SentanceParser(Sentance sentance, bool useObjects) { expression = sentance; currentIndex = 0; includeObjects = useObjects; }