Ejemplo n.º 1
0
        /// <summary>
        /// Checks for second letter
        /// </summary>
        /// <param name="letterGuessed"></param>
        /// <param name="firstIndex"></param>
        public void CheckForAnotherLetter(string letterGuessed, int firstIndex)
        {
            bool doesContainLetter = false; //assume false first
            //returns -1 if index not there, otherwise returns the first index of the letter
            int indexAtGuess = PickedWord.IndexOf(letterGuessed, ++firstIndex);

            //If the letter was found in the word to guess
            if (indexAtGuess != -1)
            {                                           //Change the * to the letter guess correctly
                this.GuessedCharacters[indexAtGuess] = char.Parse(letterGuessed);
                doesContainLetter = !doesContainLetter; //Flag for letter found updated to true
            }

            //Change the value of guess spaces to what was guessed correctly
            if (doesContainLetter)
            {
                switch (indexAtGuess)
                {
                case 0:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 11) + " " + letterGuessed + this.GameBoard[8].Substring(13);
                    break;

                case 1:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 14) + " " + letterGuessed + this.GameBoard[8].Substring(16);
                    break;

                case 2:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 17) + " " + letterGuessed + this.GameBoard[8].Substring(19);
                    break;

                case 3:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 20) + " " + letterGuessed + this.GameBoard[8].Substring(22);
                    break;

                case 4:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 23) + " " + letterGuessed + this.GameBoard[8].Substring(25);
                    break;

                case 5:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 26) + " " + letterGuessed + this.GameBoard[8].Substring(28);
                    break;
                }
                //Call again to check for another letter
                CheckForAnotherLetter(letterGuessed, indexAtGuess);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Checks if guessed letter is in the word
        /// </summary>
        /// <param name="letterGuessed"></param>
        /// <returns></returns>
        public bool ContainsLetter(string letterGuessed)
        {
            bool doesContainLetter = false; //assume false first
            //returns -1 if index not there, otherwise returns the first index of the letter
            int indexAtGuess = PickedWord.IndexOf(letterGuessed);

            //If the letter was found in the word to guess
            if (indexAtGuess != -1)
            {                                           //Change the * to the letter guess correctly
                this.GuessedCharacters[indexAtGuess] = char.Parse(letterGuessed);
                doesContainLetter = !doesContainLetter; //Flag for letter found updated to true
            }

            //Change the value of guess spaces to what was guessed correctly
            if (doesContainLetter)
            {
                switch (indexAtGuess)
                {
                case 0:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 11) + " " + letterGuessed + this.GameBoard[8].Substring(13);
                    break;

                case 1:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 14) + " " + letterGuessed + this.GameBoard[8].Substring(16);
                    break;

                case 2:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 17) + " " + letterGuessed + this.GameBoard[8].Substring(19);
                    break;

                case 3:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 20) + " " + letterGuessed + this.GameBoard[8].Substring(22);
                    break;

                case 4:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 23) + " " + letterGuessed + this.GameBoard[8].Substring(25);
                    break;

                case 5:
                    this.GameBoard[8] = this.GameBoard[8].Substring(0, 26) + " " + letterGuessed + this.GameBoard[8].Substring(28);
                    break;
                }

                //Run method checking for 2nd letter
                CheckForAnotherLetter(letterGuessed, indexAtGuess);

                //Print the board
                PrintOutBoard();
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine("Correct!");
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine($"Remaining incorrect guesses: {RemainingAttempts}");
                PrintIncorrectLetters();
                Console.ResetColor();

                string strGuessed = new string(this.GuessedCharacters);
                string strPicked  = this.PickedWord;

                //This will print the entire word:
                //Console.WriteLine($"Word to guess: {this.PickedWord}");

                //If its the last guess remaining
                if (strGuessed.Equals(strPicked))
                {
                    this.RemainingAttempts = 0;
                    Console.WriteLine("\nYou WIN!!");
                }
            }

            /* If index 0   change row 10, spaces 12, 13
             * If index 1   spaces 15, 16
             * If index 2   spaces 18, 19
             * If index 3   spaces 21, 22
             * If index 4   spaces 24, 25
             * If index 5   spaces 27, 28
             */

            //Did not guess correctly
            else
            {
                // Check for correct format (single letter only)
                if (!letterGuessed.Any(x => char.IsLetter(x)) || letterGuessed.Length != 1)
                {
                    Console.WriteLine("Enter only a single letter");
                }
                //If the letter was already guess incorrectly
                else if (incorrectLetters.Contains(char.Parse(letterGuessed)))
                {
                    //Reprint board and tell user they already guessed that
                    AlreadyGuessedLetter();
                }

                //New incorrect guessed letter
                else
                {
                    this.RemainingAttempts--;
                    incorrectLetters.Add(char.Parse(letterGuessed));
                    IncorrectGuess();
                }
            }

            return(doesContainLetter);
        }