Пример #1
0
        public void Run()
        {
            Console.WriteLine("Welcome to hangman! \n" +
                              "Would you like to play a game? (y/n)");
            string selection = Console.ReadLine();

            if (selection == "y")
            {
                WordGenerator word = new WordGenerator();
                PlayGame(word.GetWord());
            }
            else
            {
                Console.WriteLine("Goodbye");
                Console.ReadLine();
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            var continuePlaying = true;

            Core.Hangman  hangman;
            WordGenerator wordGenerator = new WordGenerator();

            while (continuePlaying)
            {
                // Initialize a new game of hangman
                hangman = new Core.Hangman(wordGenerator);
                var wordFound = false;
                Console.WriteLine("Welcome to the game of HANGMAN! " +
                                  "\nTry to guess the word.");

                while (!wordFound && hangman.GetAttemptsLeft() > 0)
                {
                    Console.WriteLine($"Try to guess this word: {hangman.GetGuessWord()}");
                    Console.WriteLine($"Attempts left: {hangman.GetAttemptsLeft()}");
                    Console.WriteLine($"Letters guessed so far: {hangman.GetCharactersUsed()}");
                    Console.Write("What letter do you guess? ");

                    var input = Console.ReadLine();

                    // Prevent accessing the first index of an empty string
                    if (input.Length > 0)
                    {
                        hangman.GuessLetter(input[0]);
                    }
                    else
                    {
                        Console.WriteLine("Invalid input");
                    }

                    // Check to see if word has been fully guessed
                    wordFound = hangman.IsWordBeenGuessedCompletely();

                    Console.WriteLine();
                }

                if (wordFound)
                {
                    Console.WriteLine($"Congratulations! You successfully guessed {hangman.GetSecretWord().ToUpper()}. " +
                                      "You won the game!");
                }
                else
                {
                    Console.WriteLine("Uh oh! You lost the game. " +
                                      $"\nThe word was {hangman.GetSecretWord().ToUpper()}");
                }

                Console.Write("\nDo you want to play again? (Y)es/(N)o: ");
                var playAgain = Console.ReadLine();

                if (playAgain.Length == 0 || char.ToUpper(playAgain[0]) != 'Y')
                {
                    continuePlaying = false;
                }
                Console.WriteLine();
            }
        }