예제 #1
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();
            }
        }
예제 #2
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);
        }
예제 #3
0
파일: Game.cs 프로젝트: khaiz18/spaceman
        public void Ask()
        {
            Console.WriteLine("Enter a letter.");
            string stringGuess = Console.ReadLine();

            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])
                    {
                        word = word.Remove(i, 1).Insert(i, guess.ToString());
                    }
                }
            }
        }