Exemplo n.º 1
0
        public void TestLoadTrieWithAWordWithSpecialCharacter()
        {
            List <string> words = new List <string> {
                "pygobranchiate",
                "/pygofer",
                "pygopagus",
                "pygopod",
                "pygopodine",
                "pygopodous",
                "pygostyle",
                "pygostyled",
                "pygos/tylous",
                "pyic",
                "pyin",
                "pyin/s",
                "pyjama/",
                "pyjamaed",
                "pyjamas"
            };

            TrieSearch    trie        = new TrieSearch(words);
            List <string> resultWords = trie.GetWordsForPrefix("PY");

            Assert.AreEqual(words.Count, resultWords.Count);

            Assert.IsTrue(trie.IsPrefixAWord("pygofer".ToUpper()));
            Assert.IsTrue(trie.IsPrefixAWord("pygostylous".ToUpper()));
            Assert.IsTrue(trie.IsPrefixAWord("pyins".ToUpper()));
            Assert.IsTrue(trie.IsPrefixAWord("pyjama".ToUpper()));
        }
Exemplo n.º 2
0
        public void TestLoadTrieWithOneDuplicateWord()
        {
            List <string> words = new List <string> {
                "pygobranchiate",
                "pygofer",
                "pygopagus",
                "pygopod",
                "pygopodine",
                "pygopodous",
                "pygostyle",
                "pygostyled",
                "pygostylous",
                "pyic",
                "pyin",
                "pyins",
                "pyjama",
                "pyjama",
                "pyjamaed",
                "pyjamas"
            };

            TrieSearch    trie        = new TrieSearch(words);
            List <string> resultWords = trie.GetWordsForPrefix("");

            Assert.AreEqual(words.Count - 1, resultWords.Count);
        }
Exemplo n.º 3
0
        public void TestLoadTrieAndCheckNumberOfLeafs()
        {
            List <string> words = new List <string> {
                "pygobranchiate",
                "pygofer",
                "pygopagus",
                "pygopod",
                "pygopodine",
                "pygopodous",
                "pygostyle",
                "pygostyled",
                "pygostylous",
                "pyic",
                "pyin",
                "pyins",
                "pyjama",
                "pyjamaed",
                "pyjamas"
            };

            TrieSearch    trie        = new TrieSearch(words);
            List <string> resultWords = trie.GetWordsForPrefix("PY");

            Assert.AreEqual(words.Count, resultWords.Count);

            foreach (var w in resultWords)
            {
                Assert.IsTrue(trie.IsPrefixAWord(w));
            }
        }
Exemplo n.º 4
0
        public GameState processLetter(string word, string letter)
        {
            GameState gameState = new GameState();

            gameState.word = word + letter;
            gameState.setGameInProgress();

            if (gameState.word.Length >= 4 && wordsTree.IsPrefixAWord(gameState.word))
            {
                //HGP: Letter entered leads to an existing word...Human loses
                gameState.message = "Computer wins: " + gameState.word + " is an existing word";
                gameState.setComputerWin();
                return(gameState);
            }

            List <string> resultWords = wordsTree.GetWordsForPrefix(gameState.word);

            if (resultWords.Count == 0)
            {
                //HGP: Letter entered doesnt have a word result...Human loses
                gameState.setComputerWin();
                gameState.message = "Computer wins: it doesn't exist a word starting by: " + gameState.word;
                gameState.setComputerWin();
                return(gameState);
            }
            else
            {
                //HGP: Letter entered is valid...Computer plays next letter

                //HGP: Ask for winning words to the trie
                List <string> winningWords = wordsTree.GetWinningWords(gameState.word);

                // it should play randomly among all its winning movements;
                if (winningWords.Count > 0)
                {
                    Random random       = new Random();
                    int    index        = random.Next(winningWords.Count);
                    string wordToFollow = winningWords[index];

                    gameState.word    = gameState.word + wordToFollow[gameState.word.Length];
                    gameState.message = "Human plays...";

                    return(gameState);
                }
                else
                {
                    List <string> loosingWords;
                    //Look for the words that doesnt contain an odd word inside so the game it doesnt finish earlier

                    //TODO HGP: This search can be optimized
                    List <string> loosingWordsWithoutOddWordsInside = resultWords.Where(s => !wordsTree.ContainsAnOddWordInsideWithLengthGreaterThan(s, 3)).ToList();
                    if (loosingWordsWithoutOddWordsInside.Count == 0)
                    {
                        loosingWords = resultWords.OrderByDescending(w => w.Length).ToList();
                    }
                    else
                    {
                        loosingWords = loosingWordsWithoutOddWordsInside.OrderByDescending(w => w.Length).ToList();
                    }

                    string loosingWord = loosingWords.First();
                    gameState.word    = gameState.word + loosingWord[gameState.word.Length];
                    gameState.message = "Human plays...";

                    if (wordsTree.IsPrefixAWord(gameState.word) && gameState.word.Length >= 4)
                    {
                        //HGP: Letter entered leads to an existing word...Computer loses
                        gameState.setHumanWin();
                        gameState.message = "Human wins...";
                        gameState.setHumanWin();
                    }
                    return(gameState);
                }
            }
        }