Exemplo n.º 1
0
        public static void run()
        {
            var provider = new Provider();
            int occurances;
            int length;
            int fails;

            int[]         positions;
            List <char>   triedCharacters = new List <char>();
            List <string> availableWords  = new List <string>();
            string        choice;
            string        newWord;
            string        lang;
            string        currentGuess;
            char          characterAsked;
            bool          wordContainsLetter;

            DisplayTitle();
            Console.ReadKey(true);
            Console.Clear();

            do
            {
                availableWords.Clear();
                triedCharacters.Clear();

                lang = SelectLanguage(provider.GetAvailableLanguages());

                Console.Clear();
                DisplayTitle();
                length         = GetWordLength();
                availableWords = provider.ReadList(length, lang);
                currentGuess   = string.Concat(Enumerable.Repeat("_", length));
                fails          = 0;
                Console.Clear();
                do
                {
                    DisplayTitle();
                    Console.WriteLine($"Amount of words left: {availableWords.Count}");
                    Console.WriteLine($"Current guess: {currentGuess}");
                    characterAsked = Guesser.GetMostRelevantCharacter(triedCharacters, availableWords);
                    triedCharacters.Add(characterAsked);
                    Console.WriteLine($"Amount of failures left: {5-fails} / 5");
                    Console.Write($"Is the letter \"{characterAsked}\" in your word? " +
                                  $"\n(Y/N): ");
                    choice = Console.ReadLine();

                    while (choice.ToUpper() != "Y" && choice.ToUpper() != "N")
                    {
                        Console.Write("Enter what was requested for: ");
                        choice = Console.ReadLine();
                    }

                    if (choice.ToUpper() == "Y")
                    {
                        wordContainsLetter = true;
                        Console.Write($"\nHow many times does the letter \"{characterAsked}\" occur in your word? " +
                                      $"\nEnter how many occurances: ");
                        occurances     = GetLetterAmount(length);
                        positions      = GetWordPosition(length, occurances);
                        currentGuess   = UpdateWord(currentGuess, positions, occurances, characterAsked);
                        availableWords = Guesser.FilterWords(characterAsked, availableWords, wordContainsLetter, positions);
                    }

                    else if (choice.ToUpper() == "N")
                    {
                        wordContainsLetter = false;
                        availableWords     = Guesser.FilterWords(characterAsked, availableWords, wordContainsLetter);
                        fails++;
                    }

                    Console.Clear();
                } while (!Game.IsOver(availableWords.Count, fails));

                if (fails >= 5 || availableWords.Count == 0)
                {
                    Console.Write("You WON! The computer could not guess your word." +
                                  "\nWhat was your word? ");
                    newWord = Console.ReadLine();
                    provider.AddNewWord(newWord, lang);
                    Console.Write("Would you like to try again? (Y/N): ");
                    choice = Console.ReadLine();
                }
                else if (availableWords.Count == 1)
                {
                    Console.Write($"The computer thinks your word is {availableWords[0]}." +
                                  $"\nIs that correct? (Y/N): ");
                    choice = Console.ReadLine();
                    Console.WriteLine("");
                    if (choice == "Y" || choice == "y")
                    {
                        Console.Write("Thank you for playing." +
                                      "\nWould you like to try again? (Y/N): ");
                        choice = Console.ReadLine();
                    }
                    else
                    {
                        Console.Write("Congratulations, you beat the computer." +
                                      "\nWhat was your word? ");
                        newWord = Console.ReadLine();
                        provider.AddNewWord(newWord, lang);
                        Console.Write("Would you like to try again? (Y/N): ");
                        choice = Console.ReadLine();
                    }
                }
                Console.Clear();
            } while (choice == "Y" || choice == "y");
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var guesser  = new Guesser();
            var provider = new Provider();
            var game     = new Game();
            int length   = 0;
            int occurances;
            int fails = 0;

            int[]         positions;
            List <char>   TriedCharacters = new List <char>();
            List <string> AvailableWords  = new List <string>();
            string        choice;
            string        NewWord;
            char          CharacterAsked;
            string        CurrentGuess;
            bool          state;
            bool          IsGameOver = false;

            do
            {
                length         = provider.GetWordLength();
                AvailableWords = guesser.RetrieveWords(length);
                CurrentGuess   = string.Concat(Enumerable.Repeat("_", length));
                IsGameOver     = false;
                fails          = 0;

                do
                {
                    Console.WriteLine($"Current guess: {CurrentGuess}");
                    CharacterAsked = guesser.GetAvaiableCharacter(TriedCharacters, AvailableWords);
                    TriedCharacters.Add(CharacterAsked);
                    Console.WriteLine($"The amount of words avaiable are {AvailableWords.Count}.");
                    Console.Write($"Is the letter \"{CharacterAsked}\" in your word? " +
                                  $"\n(Y/N): ");
                    choice = Console.ReadLine();
                    while (choice != "y" && choice != "Y" && choice != "N" && choice != "n")
                    {
                        Console.Write("Enter what was requested for: ");
                        choice = Console.ReadLine();
                    }
                    if (choice == "Y" || choice == "y")
                    {
                        state = true;
                        Console.Write($"\nHow many times does the letter \"{CharacterAsked}\" occur in your word? " +
                                      $"\nEnter how many occurances: ");
                        occurances     = provider.GetLetterAmount(length);
                        positions      = provider.GetWordPosition(length, occurances);
                        CurrentGuess   = provider.UpdateWord(CurrentGuess, positions, occurances, CharacterAsked);
                        AvailableWords = guesser.FilterWords(CharacterAsked, AvailableWords, state, positions);
                        IsGameOver     = game.IsGameOver(AvailableWords.Count, fails);
                    }
                    else
                    {
                        state          = false;
                        AvailableWords = guesser.FilterWords(CharacterAsked, AvailableWords, state);
                        fails++;
                        IsGameOver = game.IsGameOver(AvailableWords.Count, fails);
                    }
                } while (IsGameOver == false);
                if (fails >= 10 || AvailableWords.Count == 0)
                {
                    Console.Write("You WON! The computer could not guess your word." +
                                  "\nWhat was your word? ");
                    NewWord = Console.ReadLine();
                    guesser.AddNewWord(NewWord);
                    Console.Write("Would you like to try again? (Y/N): ");
                    choice = Console.ReadLine();
                }
                else if (AvailableWords.Count == 1)
                {
                    Console.Write($"The computer thinks your word is {AvailableWords[0]}." +
                                  $"\nIs that correct? (Y/N): ");
                    choice = Console.ReadLine();
                    Console.WriteLine("");
                    if (choice == "Y" || choice == "y")
                    {
                        Console.Write("Thank you for playing." +
                                      "\nWould you like to try again? (Y/N): ");
                        choice = Console.ReadLine();
                    }
                    else
                    {
                        Console.Write("Congratulations, you beat the computer." +
                                      "\nWhat was your word? ");
                        NewWord = Console.ReadLine();
                        guesser.AddNewWord(NewWord);
                        Console.Write("Would you like to try again? (Y/N): ");
                        choice = Console.ReadLine();
                    }
                }
                Console.Clear();
            } while (choice == "Y" || choice == "y");
        }