示例#1
0
        private static IEnumerable <string> _WordSlideSearch(KnownWords knownWords, UnknownWord unknownWord, char[][] availableLetters)
        {
            if (_AllUnknownWordLengthsReasonable(knownWords, unknownWord) == false)
            {
                return(new HashSet <string>());
            }

            availableLetters = _EnsureAllLower(availableLetters);
            var searchLetters = _FillInSearchLettersFromPartialWord(unknownWord, availableLetters);

            var wordLen       = unknownWord.WordPattern.Length;
            var possibleWords = knownWords.AllWordsOfLength(wordLen);
            var matches       = new List <string>();

            foreach (var word in possibleWords)
            {
                if (_IsPossibleMatch(searchLetters, word))
                {
                    matches.Add(word);
                }
            }

            matches = matches.OrderBy(w => w).ToList();
            return(matches);
        }
        //[Test]
        ////[TestCaseSource(nameof(_SolveMultiWordTestData))]
        //public void SolveMultiWord()
        //{
        //    var knownWords = new KnownWords(new[] { "if", "in", "an", "then", "than", "else" });
        //    var wordPattern = new UnknownWord("??????????");
        //    wordPattern.SetSubWordCounts(new[] { 2, 4, 4 });
        //    char[][] letters = {
        //        new[] {'A', 'Y', 'I'},
        //        new[] {'F', 'N', 'O'},
        //        new[] {'t', 'H', 'O'},
        //        new[] {'t', 'H', 'O'},
        //        new[] {'W', 'A', 'E'},
        //        new[] {'F', 'N', 'O'},
        //        new[] {'W', 'A', 'E'},
        //        new[] {'y', 'L', 'b'},
        //        new[] {'s', 's'},
        //        new[] {'k', 'E', 'y'},
        //    };

        //    var puzzle = new WordSlidePuzzle(knownWords);
        //    var ex = Assert.Throws<ArgumentException>(() => puzzle.Solve(wordPattern, letters));
        //    Assert.That(ex.Message,Is.EqualTo("A word slide puzzle cannot solve for multiple words"));
        //}

        // TODO: WordSlidePuzzle doesn't currently know how to solve multiwords
        //public static IEnumerable<TestCaseData> _SolveMultiWordTestData()
        //{
        //    {
        //        var knownWords = new KnownWords(new[] { "if", "in", "an", "then", "than", "else" });
        //        var unknownWord = new UnknownWord("??????????");
        //        unknownWord.SetSubWordCounts(new[] { 2, 4, 4 });
        //        char[][] letters = {
        //            new[] {'A', 'Y', 'I'},
        //            new[] {'F', 'N', 'O'},
        //            new[] {'t', 'H', 'O'},
        //            new[] {'t', 'H', 'O'},
        //            new[] {'W', 'A', 'E'},
        //            new[] {'F', 'N', 'O'},
        //            new[] {'W', 'A', 'E'},
        //            new[] {'y', 'L', 'b'},
        //            new[] {'s', 's'},
        //            new[] {'k', 'E', 'y'},
        //        };
        //        var expectedWord = "if then else";
        //        yield return new TestCaseData(knownWords, unknownWord, letters, expectedWord)
        //            .SetName("expectedWord: if then else");
        //    }
        //}

        public static IEnumerable <TestCaseData> _SolveSingleWordTestData()
        {
            {
                var      knownWords  = new KnownWords(new[] { "if", "in", "an" });
                var      wordPattern = "??";
                char[][] letters     =
                {
                    new[] { 'A', 'Y', 'I' },
                    new[] { 'F', 'N', 'O' }
                };
                var expectedWord = "if";
                yield return(new TestCaseData(knownWords, wordPattern, letters, expectedWord)
                             .SetName("expectedWord: if"));
            }

            {
                var      knownWords  = new KnownWords(new[] { "walking", "talking", "walk", "talk" });
                var      wordPattern = "w??????";
                char[][] letters     =
                {
                    new[] { 'T', 'W' },
                    new[] { 'A', 'o' },
                    new[] { 'p', 'l', 'N'},
                    new[] { 'e', 'a', 'k'},
                    new[] { 'i', 'v' },
                    new[] { 'i', 'n' },
                    new[] { 'G', 'n' },
                };
                var expectedWord = "walking";
                yield return(new TestCaseData(knownWords, wordPattern, letters, expectedWord)
                             .SetName("expectedWord: walking"));
            }
        }
        public void SolveTest(KnownWords knownWords, string originalWord, string expectedAnagram)
        {
            var puzzle    = new AnagramPuzzle(knownWords);
            var solutions = puzzle.Solve(originalWord);

            Assert.That(solutions, Has.Member(expectedAnagram), "expected anagram was not in the list of solutions");
        }
        public void SolveSingleWord(KnownWords knownWords, string wordPattern, char[][] letters, string expectedWord)
        {
            var puzzle    = new WordSlidePuzzle(knownWords);
            var solutions = puzzle.Solve(wordPattern, letters);

            Assert.That(solutions.Contains(expectedWord));
        }
        private static IEnumerable <TestCaseData> _CrosswordData()
        {
            {
                var   knownWords   = new KnownWords(new[] { "one", "two", "ane", "ene", "ine" });
                var   unknownWord  = new UnknownWord("?ne");
                int[] subWordCount = null;
                var   expectedWord = "one";
                yield return(new TestCaseData(knownWords, unknownWord, subWordCount, expectedWord)
                             .SetName("word: something"));
            }

            {
                var   knownWords   = new KnownWords(new[] { "one", "two", "ane", "ene", "ine" });
                var   unknownWord  = new UnknownWord("?net??");
                int[] subWordCount = { 3, 3 };
                var   expectedWord = "one two";
                yield return(new TestCaseData(knownWords, unknownWord, subWordCount, expectedWord)
                             .SetName("word: one two"));
            }

            {
                var   knownWords   = new KnownWords(new[] { "teen", "ween", "town", "then" });
                var   unknownWord  = new UnknownWord("t??n");
                int[] subWordCount = null;
                var   expectedWord = "teen";
                yield return(new TestCaseData(knownWords, unknownWord, subWordCount, expectedWord)
                             .SetName("word: teen"));
            }
        }
        public void SolveTest(KnownWords knownWords, UnknownWord unknownWord, int[] subWordCount, string expectedWord)
        {
            var crossword = new CrosswordPuzzle(knownWords);
            var solutions = crossword.Solve(unknownWord, subWordCount);

            Assert.That(solutions, Has.Member(expectedWord), "expected word was not in the list of solutions");
        }
示例#7
0
 private void buttonKnow_Click(object sender, EventArgs e)
 {
     lines[curLineNumber] = string.Empty;
     KnownWords++;
     toolStripStatusLabelCurKnows.Text = "Known words: " + KnownWords.ToString();
     buttonRead_Click(sender, e);
 }
        private static IEnumerable <TestCaseData> _AnagramTestData()
        {
            // snagged a few easy anagrams from wikipedia
            var knownWords = new KnownWords(new[] { "evil", "funeral", "adultery", "totems", "lie", "mistletoe" });

            {
                var originalWord = "vile";
                var expectedWord = "evil";
                yield return(new TestCaseData(knownWords, originalWord, expectedWord)
                             .SetName("vile->evil"));
            }

            {
                var originalWord = "realFun";
                var expectedWord = "funeral";
                yield return(new TestCaseData(knownWords, originalWord, expectedWord)
                             .SetName("realFun->funeral"));
            }

            {
                var originalWord = "trueLady";
                var expectedWord = "adultery";
                yield return(new TestCaseData(knownWords, originalWord, expectedWord)
                             .SetName("trueLady->adultery"));
            }

            {
                var originalWord = "totems lie";
                var expectedWord = "mistletoe";
                yield return(new TestCaseData(knownWords, originalWord, expectedWord)
                             .SetName("totems lie->mistletoe"));
            }
        }
示例#9
0
        public void AllWordsOfLengthThrowsForZeroLength()
        {
            var allWords   = new[] { string.Empty, "aa", "bb", "aaa", "bbb", "1234" };
            var knownWords = new KnownWords(allWords);
            var ex         = Assert.Throws <ArgumentException>(() => knownWords.AllWordsOfLength(0));

            Assert.That(ex.Message, Is.EqualTo("A known word must have at least 1 character"));
        }
示例#10
0
        public void AllWordsOfLengthTest(string[] allWords, int wordLen, string[] expected)
        {
            var knownWords     = new KnownWords(allWords);
            var unsortedActual = knownWords.AllWordsOfLength(wordLen);
            var actual         = unsortedActual.OrderBy(s => s);

            Assert.That(actual, Is.EquivalentTo(expected));
        }
示例#11
0
 /// <summary>
 /// Words
 /// </summary>
 /// <param name="tok"></param>
 public void Visit(TokenWord tok)
 {
     if (!KnownWords.Contains(tok.Value) && tok.EndPosition - tok.StartPosition >= MinWordLengthRequired)
     {
         KnownWords.Add(tok.Value);
         PushToAutoCompletion(new WordCompletionItem(), tok);
     }
 }
示例#12
0
        public void KnownWordsIgnoresWhitespace()
        {
            var wordWithPadding = "    padding   ";
            var allWords        = new[] { wordWithPadding, "another", "word" };
            var knownWords      = new KnownWords(allWords);

            Assert.That(knownWords.IsKnownWord(wordWithPadding), Is.False, "a word with white space padding should not have been known");
            Assert.That(knownWords.IsKnownWord(wordWithPadding.Trim()), Is.True, "a word with white space padding Trimmed should have been known");
        }
示例#13
0
        public void KnownWordsIgnoresWhitespaceWords()
        {
            var oneSpace   = " ";
            var threeSpace = "   ";
            var allWords   = new[] { oneSpace, threeSpace };
            var knownWords = new KnownWords(allWords);

            Assert.That(knownWords.IsKnownWord(oneSpace), Is.False, nameof(oneSpace));
            Assert.That(knownWords.IsKnownWord(threeSpace), Is.False, nameof(threeSpace));
        }
        public void AnagramsDoNotIncludeOriginalWord()
        {
            var word       = "decorations";
            var knownWords = new KnownWords(new[] { "decorations", "coordinates" });
            var subject    = new AnagramPuzzle(knownWords);
            var anagrams   = subject.Solve(word);
            var expected   = new[] { "coordinates" };

            Assert.That(anagrams, Is.EquivalentTo(expected), "solved anagrams should not include the original word");
        }
示例#15
0
        public static void SolveMoreOrQuit(KnownWords knownWords)
        {
            Console.WriteLine();
            Console.WriteLine("Press ENTER to solve another puzzle, or type any key + ENTER to quit.");
            var ans = Console.ReadLine()?.Trim() ?? "ctrlZ";

            if (string.Empty.Equals(ans))
            {
                Console.WriteLine();
                MainMenu(knownWords);
            }
        }
示例#16
0
        public static void AnagramMenu(KnownWords knownWords)
        {
            Console.WriteLine("Type in the word you want to find anagrams for:");
            var word          = _ReadLn();
            var anagramPuzzle = new AnagramPuzzle(knownWords);
            var answers       = anagramPuzzle.Solve(word).ToList();

            Console.WriteLine($"There are {answers.Count} possible anagrams:");
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
            SolveMoreOrQuit(knownWords);
        }
示例#17
0
        public static void MainMenu(KnownWords knownWords)
        {
            Console.WriteLine("Enter the number for what type of puzzle you are trying to solve:");
            Console.WriteLine("1. Anagram");
            Console.WriteLine("2. Crossword");
            Console.WriteLine("3. Word Slide");
            //Console.WriteLine("4. Scrabble");
            var choiceStr = _ReadLn();

            if (int.TryParse(choiceStr, out var choice))
            {
                try
                {
                    switch (choice)
                    {
                    case 1:
                        AnagramMenu(knownWords);
                        break;

                    case 2:
                        CrosswordMenu(knownWords);
                        break;

                    case 3:
                        WordSlideMenu(knownWords);
                        break;

                    //case 4:
                    //    ScrabbleMenu(knownWords);
                    //    break;
                    default:
                        Console.WriteLine("The menu option " + choice + " is not available.");
                        SolveMoreOrQuit(knownWords);
                        break;
                    }
                }
                catch (PrintToConsoleException pce)
                {
                    Console.WriteLine(pce.Message);
                    SolveMoreOrQuit(knownWords);
                }
            }
            else
            {
                Console.WriteLine("The menu option " + choiceStr + " is not available.");
                SolveMoreOrQuit(knownWords);
            }
        }
        public IEnumerable <string> Solve(UnknownWord unknownWord, int[] subWordCount)
        {
            // call SetSubWordCounts just for the validation it does
            unknownWord.SetSubWordCounts(subWordCount);
            string[] wordsToSolve = { unknownWord.WordPattern };
            if (subWordCount != null)
            {
                wordsToSolve = new string[subWordCount.Length];
                var pattern    = unknownWord.WordPattern;
                var currentIdx = 0;
                for (var i = 0; i < subWordCount.Length; i++)
                {
                    var wordCount = subWordCount[i];
                    var subWord   = pattern.Substring(currentIdx, wordCount);
                    wordsToSolve[i] = subWord;
                    currentIdx     += wordCount;
                }
            }
            else
            {
                subWordCount = new [] { unknownWord.WordPattern.Length };
            }

            var matches = new List <string> [wordsToSolve.Length];

            for (var i = 0; i < matches.Length; i++)
            {
                var allWordsOfLen = KnownWords.AllWordsOfLength(subWordCount[i]);
                matches[i] = new List <string>();
                foreach (var word in allWordsOfLen)
                {
                    if (_IsMatch(wordsToSolve[i], word))
                    {
                        matches[i].Add(word);
                    }
                }
            }

            foreach (var wordList in matches)
            {
                wordList.Sort();
            }

            var allPossiblePhrases = Permutations.CartesianProduct(matches);
            var formattedPhrases   = allPossiblePhrases.Select(phrase => string.Join(" ", phrase));

            return(formattedPhrases.ToList());
        }
示例#19
0
        public static void WordSlideMenu(KnownWords knownWords)
        {
            Console.WriteLine($"Enter what you know about the word with {UnknownWord.UnknownToken} for missing characters:");
            var wordPattern = _ReadLn();

            char[][] availableLetters = _ReadWordSlideLetters(wordPattern);
            var      wordSlide        = new WordSlidePuzzle(knownWords);
            var      answers          = wordSlide.Solve(wordPattern, availableLetters).ToList();

            Console.WriteLine($"There are {answers.Count} possible solutions:");
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }

            SolveMoreOrQuit(knownWords);
        }
 internal void AddDateWordOrPostfix(string formatPostfix, string str)
 {
     if (str.Length > 0)
     {
         if (str.Equals("."))
         {
             this.AddIgnorableSymbols(".");
         }
         else
         {
             string str2;
             if (!KnownWords.TryGetValue(str, out str2))
             {
                 if (this.m_dateWords == null)
                 {
                     this.m_dateWords = new List <string>();
                 }
                 if (formatPostfix == "MMMM")
                 {
                     string item = ((char)0xe000) + str;
                     if (!this.m_dateWords.Contains(item))
                     {
                         this.m_dateWords.Add(item);
                     }
                 }
                 else
                 {
                     if (!this.m_dateWords.Contains(str))
                     {
                         this.m_dateWords.Add(str);
                     }
                     if (str[str.Length - 1] == '.')
                     {
                         string str4 = str.Substring(0, str.Length - 1);
                         if (!this.m_dateWords.Contains(str4))
                         {
                             this.m_dateWords.Add(str4);
                         }
                     }
                 }
             }
         }
     }
 }
示例#21
0
        private static bool _AllUnknownWordLengthsReasonable(KnownWords knownWords, UnknownWord unknownWord)
        {
            // TODO: if i ever get around to implementing subwords this fancier check will be needed.
            //if (unknownWord.SubWordCounts == null || unknownWord.SubWordCounts.Any() == false)
            //{
            //    if (knownWords.AllWordsOfLength(unknownWord.WordPattern.Length).Count == 0)
            //    {
            //        return false;
            //    }
            //}
            //else
            //{
            //    if (unknownWord.SubWordCounts.Any(cnt => knownWords.AllWordsOfLength(cnt).Count == 0))
            //    {
            //        return false;
            //    }
            //}

            return(knownWords.AllWordsOfLength(unknownWord.WordPattern.Length).Any());
        }
示例#22
0
        public static void CrosswordMenu(KnownWords knownWords)
        {
            Console.WriteLine($"Enter what you know about the word with {UnknownWord.UnknownToken} for missing characters:");
            var wordPattern = _ReadUnknownWord();

            Console.WriteLine("If there is more than 1 word enter the letter count of each word followed by space, if 1 word just ENTER.");
            Console.WriteLine("Example: if the solved clue is THATSALLFOLKS then the letter counts to type are '5 3 5':");
            var subWordCount = _ReadSubWordCount();

            var crosswordPuzzle = new CrosswordPuzzle(knownWords);
            var answers         = crosswordPuzzle.Solve(wordPattern, subWordCount).ToList();

            Console.WriteLine($"There are {answers.Count} possible solutions:");
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }

            SolveMoreOrQuit(knownWords);
        }
        public void SolveNullSubWordAvailableLetterMismatch()
        {
            var knownWord  = "four";
            var knownWords = new KnownWords(new [] { knownWord });
            //var unknownWord = new UnknownWord(new string(UnknownWord.UnknownToken, knownWord.Length + 1));
            var wordPattern = new string(UnknownWord.UnknownToken, knownWord.Length + 1);

            Assert.That(knownWords.AllWordsOfLength(wordPattern.Length), Is.Empty, "precondition: there is no known word the length of the unknown word");
            //Assert.That(unknownWord.SubWordCounts, Is.Null, "precondition: unknownWord.SubWordCounts must be null");
            var puzzle           = new WordSlidePuzzle(knownWords);
            var availableLetters = new char[wordPattern.Length][];

            for (var i = 0; i < availableLetters.Length; i++)
            {
                availableLetters[i] = new[] { 'X' };
            }

            var solutions = puzzle.Solve(wordPattern, availableLetters);

            Assert.That(solutions, Is.Empty, "If there are no known words of the required length the solution should be empty");
        }
示例#24
0
        public IEnumerable <string> Solve(string originalWord)
        {
            var letters = originalWord.ToAlphabetArray();

            if (letters.Any(c => c == UnknownWord.UnknownToken))
            {
                throw new ArgumentException("There cannot be any unknown characters in the word to find its anagrams.");
            }

            var cleanedWord = new string(letters);

            if (letters.Length < 2)
            {
                throw new ArgumentException("You cannot create an anagram of a word unless there are at least 2 letters");
            }

            var matches   = new List <string>();
            var sortedArr = _AllLettersSorted(cleanedWord);

            foreach (var potential in KnownWords.AllWordsOfLength(cleanedWord.Length))
            {
                if (string.Equals(cleanedWord, potential))
                {
                    continue;
                }

                var potentialArr = _AllLettersSorted(potential);
                if (sortedArr.SequenceEqual(potentialArr))
                {
                    matches.Add(potential);
                }
            }

            matches = matches.OrderBy(w => w).ToList();
            return(matches);
        }
示例#25
0
 protected BasePuzzle(KnownWords knownWords)
 {
     KnownWords = knownWords ?? throw new ArgumentNullException(nameof(knownWords));
 }
示例#26
0
 //called by service.AddControllers().
 public PermutationsController(KnownWords knownWords)
 {
     _knownWords = knownWords;
 }
示例#27
0
 public WordSlidePuzzle(KnownWords knownWords) : base(knownWords)
 {
 }
示例#28
0
 public AnagramPuzzle(KnownWords knownWords) : base(knownWords)
 {
 }
示例#29
0
 public static string ReplaceKnownWords(this string str)
 {
     return(KnownWords.Aggregate(str, (s, r) => s.ReplaceRegex(r, _ => r, RegexOptions.IgnoreCase)));
 }
 public CrosswordPuzzle(KnownWords knownWords)
     : base(knownWords)
 {
 }