Esempio n. 1
0
        void Start()
        {
            // Choose a word
            List <string> potentialWords = ReadWords();
            string        gameWord       = SelectWord(potentialWords);

            // Init the game
            HangmanGame hangman = new HangmanGame();

            hangman.Init(gameWord);
            DisplayWord(hangman.guessedWord);
            bool playing = PlayHangman(hangman);

            // Evaluate if the player won or lost the game
            if (playing)
            {
                Console.WriteLine("You guessed the word!");
            }
            else
            {
                Console.WriteLine("You didn't guess the word...");
            }

            Console.ReadKey();
        }
        bool PlayHangman(HangmanGame hangman)
        {
            List <char> enteredLetters = new List <char>();

            DisplayWord(hangman.guessedWord); //from question
            int attempts = 8;                 //starting amount of attempts for guessing

            Console.WriteLine();              //for spacing

            //this is for the attepts and askng for the letters
            while (!hangman.isGuessed() && attempts > 1) //if the word is not guess and the nr of attempts are more than one.
            {
                char letter = ReadLetter(enteredLetters);

                if (!hangman.GuessLetter(letter))//of the letter is not in the word, lose 1 attempts.
                {
                    attempts--;
                }
                Console.Write("Entered letters: ");
                DisplayLetters(enteredLetters);

                Console.WriteLine();//for the spacings

                Console.WriteLine("Attempts left: " + attempts);
                DisplayWord(hangman.guessedWord);
                Console.WriteLine();
            }
            return(hangman.isGuessed());
        }
Esempio n. 3
0
        /// <summary>
        /// Main loop; game logic
        /// </summary>
        /// <param name="hangman"></param>
        /// <returns></returns>
        bool PlayHangman(HangmanGame hangman)
        {
            List <char> enteredLetters = new List <char>();
            int         attempts       = 8;
            bool        wordGuessed    = hangman.isGuessed();

            while (attempts > 0 && wordGuessed == false)
            {
                char letter = ReadLetter(enteredLetters);
                enteredLetters.Add(letter);

                // Deduct attempts if a wrong letter was entered
                if (!hangman.GuessLetter(letter))
                {
                    attempts--;
                }

                // Display current information
                Console.Write("Entered letters: ");
                DisplayLetters(enteredLetters);
                Console.WriteLine("{0} Attempts left.", attempts);
                Console.WriteLine();
                DisplayWord(hangman.guessedWord);
                Console.WriteLine();

                // Exit once the game is won
                wordGuessed = hangman.isGuessed();
                if (wordGuessed)
                {
                    return(true);
                }
            }
            return(false);
        }
        void Start()
        {
            List <string> words = new List <string>();

            words = ListOfWords("words.txt");
            HangmanGame hangman = new HangmanGame();

            hangman.Init(SelectWord(words));
            //this is to see if the words are being displayed.
            //Console.WriteLine("the secret word is: " + hangman.secretWord);
            //Console.WriteLine("the guessed word is: " + hangman.guessedWord);
            if (PlayHangman(hangman))
            {
                Console.WriteLine("You guessed the word!!");
            }
            else
            {
                Console.WriteLine("You run out of attempts");
            }
        }