예제 #1
0
        /// <summary>
        /// Draws the hangman to the console.
        /// </summary>
        /// <param name="hangmanPic">Array containing the hangman characters</param>
        /// <param name="hangManWord">The hidden word with various properties to help with drawing</param>
        public void DrawGraphics(char[] hangmanPic, HangManMysteryWord hangManWord)
        {
            //Extract properties from the class passed
            char[]       mysteryWord  = hangManWord.Word.ToCharArray();
            MysteryTopic mysteryTopic = hangManWord.Topic;

            bool[] mysteryWordCover = hangManWord.WordCover;
            //Clear console to redraw hangman
            Console.Clear();
            //Write each character in the hangman char array
            for (int i = 0; i < hangmanPic.Length; i++)
            {
                Console.Write(hangmanPic[i]);
            }
            //Writes the clue on what the word is
            Console.WriteLine("\n\nTry to guess the mystery {0}\n", mysteryTopic.ToString());
            //Iterate through each character of the hidden word
            for (int i = 0; i < mysteryWord.Length; i++)
            {
                //if the cover is true at the index then it will hide the character
                if (mysteryWordCover[i])
                {
                    mysteryWord[i] = '_';
                }
                Console.Write(mysteryWord[i]);
            }
            Console.WriteLine("\n");
        }
예제 #2
0
        /// <summary>
        /// Determines if a correct guess was made.
        /// Compares the hidden word with the input by iterating through each letter of the hidden word.
        /// </summary>
        /// <param name="currentWord">current inctance of the hidden word</param>
        /// <param name="currentGuess">most recent input of the user</param>
        /// <returns></returns>
        private bool CorrectGuess(ref HangManMysteryWord currentWord, char currentGuess)
        {
            int revealedLetters = 0;

            //Iterate through each letter of the hidden word and compare the current guess.
            for (int i = 0; i < currentWord.Word.ToCharArray().Length; i++)
            {
                //Increase revealed letters counter by 1 if there is a match
                if (currentGuess.Equals(currentWord.Word.ToCharArray()[i]))
                {
                    //Turn off the cover at the index where there was a match to ready the next game render
                    currentWord.WordCover[i] = false;
                    revealedLetters++;
                }
            }
            //Correct guess if at least 1 letter was revealed
            if (revealedLetters > 0)
            {
                return(true);
            }
            //Return false if none were revealed
            return(false);
        }