public IEnumerable <Placement> FindPlacements(Gaddag words)
        {
            if (Grid.IsEmpty())
            {
                foreach (Gaddag word in words.FindAllWords(Bank))
                {
                    Bank after = new Bank(Bank);
                    after.TakeWord(word.word);
                    yield return(new Placement(word.word, 0, 0, false, after));

                    yield return(new Placement(word.word, 0, 0, true, after));
                }

                yield break;
            }

            IEnumerable <Placement> FindPlacementsOriented(bool vertical)
            {
                int start = (vertical ? Grid.LeftmostColumnIndex : Grid.TopRowIndex);
                int end   = (vertical ? Grid.RightmostColumnIndex : Grid.BottomRowIndex);

                for (int i = start; i <= end; ++i)
                {
                    if (Grid.IsEmptyLine(i, vertical))
                    {
                        continue;
                    }

                    foreach (Placement placement in Grid.FindLinePlacements(i, vertical, Bank, words))
                    {
                        yield return(placement);
                    }
                }
            }

            foreach (Placement placement in FindPlacementsOriented(false))
            {
                yield return(placement);
            }
            foreach (Placement placement in FindPlacementsOriented(true))
            {
                yield return(placement);
            }
        }
Exemple #2
0
        public IEnumerable <Placement> FindLinePlacementsFrom(int lineIndex, int start, int frontLeeway, bool vertical, Bank startingBank, Gaddag words)
        {
            Dictionary <int, Line> axis = vertical ? columns : rows;
            Line line = axis[lineIndex];

            Line[] neighbors = new Line[2];
            neighbors[0] = GetLineLazy(axis, lineIndex - 1);
            neighbors[1] = GetLineLazy(axis, lineIndex + 1);

            Stack <(Gaddag, int, Bank)> nodes = new Stack <(Gaddag, int, Bank)>();

            nodes.Push((words, start, startingBank));

            while (nodes.Count != 0)
            {
                (Gaddag node, int index, Bank bank) = nodes.Pop();

                if (line.IsEmptyAt(index))
                {
                    if (!(neighbors[0].IsEmptyAt(index) && neighbors[1].IsEmptyAt(index)))
                    {
                        continue;
                    }
                    for (char c = 'a'; c <= 'z'; ++c)
                    {
                        if (!bank.HasLetter(c) || node[c] == null)
                        {
                            continue;
                        }

                        Bank after = new Bank(bank);
                        after.TakeLetter(c);

                        nodes.Push((node[c], index - 1, after));
                    }

                    if (node['{'] != null)
                    {
                        foreach (Gaddag word in node['{'].FindAllWords(bank))
                        {
                            if (word.word.Length - word.reversePoint >= frontLeeway)
                            {
                                continue;
                            }
                            Bank after = new Bank(bank);
                            after.TakeWord(word.word.Substring(word.reversePoint + 1));
                            if (Enumerable.SequenceEqual(startingBank.letters, after.letters))
                            {
                                continue;
                            }
                            yield return(new Placement(word.word, lineIndex, start - word.reversePoint, vertical, after));
                        }
                    }
                }
                else
                {
                    char c = line[index];
                    if (node[c] != null)
                    {
                        nodes.Push((node[c], index - 1, bank));
                    }
                }
            }
        }