Exemplo n.º 1
0
        ///<summary>
        ///this method capture the user's input letter(it has to be one letter at a time), checks if that letter is indeed anywhere
        ///in the codeword. If yes print those leters at the exact places in the codeword. If the codeword does not contains the user's guess
        ///increase the wrongGuess counter by 1
        /// </summary>

        public void Ask()
        {
            Console.WriteLine("Guess a letter: ");
            string userGuess             = Console.ReadLine();
            string userGuessWithoutSpace = userGuess.Trim();

            while (userGuessWithoutSpace.Length != 1)
            {
                Console.WriteLine("Please enter one letter at a time");
            }
            char guess = userGuessWithoutSpace.ToCharArray()[0];

            if (CodeWord.Contains(guess))
            {
                Console.WriteLine("Here are your guess in the codeWord!");
                for (int i = 0; i < CodeWord.Length; i++)
                {
                    if (guess == CodeWord[i])
                    {
                        CurrentWord = CurrentWord.Remove(i, 1).Insert(i, guess.ToString());
                    }
                }
            }
            else
            {
                Console.WriteLine("Sorry, your guess does not appear in the the codeWord");
                CurrentNumberOfWrongGuess++;
                ufo.AddPart();
            }
        }
Exemplo n.º 2
0
        public void Ask()
        {
            Console.Write("Guess a letter: ");
            string stringGuess = Console.ReadLine();

            Console.WriteLine();
            if (stringGuess.Trim().Length != 1)
            {
                Console.WriteLine("One letter at a time!");
                return;
            }
            char guess = stringGuess.Trim().ToCharArray()[0];

            if (CodeWord.Contains(guess))
            {
                Console.WriteLine($"'{guess}' is in the word!");
                for (int i = 0; i < CodeWord.Length; i++)
                {
                    if (guess == CodeWord[i])
                    {
                        CurrentWord = CurrentWord.Remove(i, 1).Insert(i, guess.ToString());
                    }
                }
            }
            else
            {
                Console.WriteLine($"'{guess}' isn't in the word! The tractor beam pulls the person in further...");
                WrongGuesses++;
                spaceship.AddPart();
            }
        }
Exemplo n.º 3
0
        public void Ask()
        {
            Console.WriteLine("Hey human! Try to guess a letter or I'll take this other human!");
            string answer = Console.ReadLine();

            Console.WriteLine();
            if (answer.Trim().Length != 1)
            {
                Console.WriteLine("Please, one letter at a time.");
                return;
            }
            char charCode = answer.Trim().ToCharArray()[0];

            if (CodeWord.Contains(charCode))
            {
                Console.WriteLine("You've got it right!");
                for (int i = 0; i < CurrentWord.Length; i++)
                {
                    if (CodeWord[i] == charCode)
                    {
                        CurrentWord = CurrentWord.Remove(i, 1).Insert(i, charCode.ToString());
                    }
                }
            }
            else
            {
                NumGuesses++;
                ufo.AddPart();
            }
        }
Exemplo n.º 4
0
        public void Ask()
        {
            Console.WriteLine("\nGuess a letter:");
            PlayerGuess = Console.ReadLine().ToLower();
            LettersUsed.Add(PlayerGuess);

            while (PlayerGuess.Trim().Length != 1)
            {
                Console.Clear();
                Console.WriteLine("Input 1 letter at a time");
                return;
            }

            char guess = PlayerGuess.Trim().ToCharArray()[0];

            if (CodeWord.Contains(guess))
            {
                Console.WriteLine($"{guess} is in the word!");
                for (int i = 0; i < CodeWord.Length; i++)
                {
                    if (guess == CodeWord[i])
                    {
                        Console.Clear();
                        CurrentWord = CurrentWord.Remove(i, 1).Insert(i, guess.ToString());
                    }
                }
            }
            else
            {
                CurrentNumWrongGuess++;
                ufos.AddPart();
                Console.Clear();
            }
        }
Exemplo n.º 5
0
        public void Ask()
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("\nPlease type a letter.");
            Console.ResetColor();
            string input = Console.ReadLine();

            // check and tell user to enter ONLY 1 letter
            if (input.Length > 1)
            {
                Console.WriteLine("Please only type ONE letter at a time!\nPress Enter to continue...");
                Console.ReadLine();
                return;
            }

            // check if the input is a letter and prompt the user if not
            if (!char.IsLetter(input[0]))
            {
                Console.WriteLine("Please only enter letters!\nPress Enter to continue...");
                Console.ReadLine();
                return;
            }

            // convert input to lower letter
            string letter = input[0].ToString().ToLower();

            // if the letter has already been inclear
            if (letterList.Contains(letter))
            {
                Console.WriteLine("'{0}' has already been found.\nPlease insert another letter\nPress Enter to continue...", letter);
                Console.ReadLine();
                return;
            }
            letterList.Add(letter);

            // check if the word contains the inserted letter
            if (Codeword.Contains(letter))
            {
                // prompt the user
                Console.WriteLine("Good guess. The codeword contains '{0}'.", letter);
                // loop thru codeword for inserted letter
                // if found replace '_' with letter in current word
                for (int i = 0; i < Codeword.Length; i++)
                {
                    if (Codeword[i].ToString() == letter)
                    {
                        CurrentWord = CurrentWord.Remove(i, 1).Insert(i, letter);
                    }
                }
            }
            // increase the wrong guesses by 1 and Update ufo image
            else
            {
                CurrentNumWrongGuesses += 1;
                ufo.AddPart();
            }
        }
Exemplo n.º 6
0
        public void Ask()
        {
            if (_guessedLetters.Count > 0)
            {
                Console.WriteLine($"Already typed letters in thi game session: ");
                _guessedLetters.ForEach(item => Console.Write($" {item} "));
                Console.WriteLine("");
            }

            Console.Write("Enter your guess letter: ");
            var input = Console.ReadLine()?.ToLower();

            if (input == null)
            {
                return;
            }

            if (input.Length > 1)
            {
                Console.WriteLine("Only one letter at a time is valid.");
                return;
            }

            var contains = Codeword.Contains(input);
            var letter   = input.ToCharArray()[0];

            if (_guessedLetters.Contains(letter))
            {
                Console.WriteLine($"You already typed the ({input}) letter, try another one...");
                return;
            }

            if (contains)
            {
                var currentIndex = 0;
                while (true)
                {
                    var index = Codeword.IndexOf(letter, currentIndex);
                    if (index == -1)
                    {
                        break;
                    }
                    currentIndex = index + 1;
                    CurrentWord  = CurrentWord.Remove(index, 1).Insert(index, letter.ToString());
                }
            }
            else
            {
                Ufo.AddPart();
                WrongGuesses++;
            }
            _guessedLetters.Add(letter);
        }
Exemplo n.º 7
0
        public void Ask()
        {
            PrintColorMessage(ConsoleColor.Yellow, "Guess a letter: ");
            string stringGuess = Console.ReadLine(); // Get users input

            Console.WriteLine();

            // Check if users input is of length 1  and is a letter
            if (stringGuess.Trim().Length != 1 || int.TryParse(stringGuess.Trim(), out _))
            {
                PrintColorMessage(ConsoleColor.Red, "Only a single letters are accepted!");
                return;
            }

            // Convert user inpur string to char and add to char array
            char guess = stringGuess.Trim().ToCharArray()[0];

            if (CodeWord.Contains(guess))
            {
                // If word contains given letter, find its index ....
                PrintColorMessage(ConsoleColor.Yellow, $"'{guess}' is in the word!");

                for (int i = 0; i < CodeWord.Length; i++)
                {
                    if (guess == CodeWord[i])
                    {
                        //... and replace '_' in a string with a letter
                        CurrentWord = CurrentWord.Remove(i, 1).Insert(i, guess.ToString());
                    }
                }
            }
            else
            {
                // If codeword doesn't contain the letter, increase number of wrong guesses...
                Console.WriteLine();
                PrintColorMessage(ConsoleColor.Yellow, $"'{guess}' isn't in the word! The tractor beam pulls the person in further...");
                WrongGuesses++;
                // Update the string representation of a spaceship according to a current number of wrong guesses
                spaceship.AddPart();
            }
        }
Exemplo n.º 8
0
        public void Ask()
        {
            Console.WriteLine("Guess a letter:");
            string userGuess = Console.ReadLine();

            if (userGuess.Length != 1)
            {
                Console.WriteLine("One letter at a time!");
                return;
            }

            char guess = userGuess.ToCharArray()[0];

            char[] guessArray = userGuess.ToCharArray();

            if (CodeWord.Contains(guess))
            {
                Console.WriteLine($"{guess} is in the word");
                for (int i = 0; i < CodeWord.Length; i++)
                {
                    if (guess == CodeWord[i])
                    {
                        CurrentWord = CurrentWord.Remove(i, 1).Insert(i, guess.ToString());
                    }
                }
            }

            else
            {
                Console.WriteLine($"{guess} is not in the word. The beam pulls you up further...");
                WrongGuesses++;
                spaceship.AddPart();
            }

            for (int i = 0; i < guessArray.Length; i++)
            {
                Console.WriteLine($"Letters you have guessed so far:{guessArray[i]}");
            }
        }