public void Ask() { // Ask user to enter a letter and capture it Console.WriteLine("Enter a letter: "); string letter = Console.ReadLine(); if (letter.Length > 1 || letter.Length == 0) { Console.WriteLine("Please input one letter at a time!"); return; } char guess = letter.Trim().ToCharArray()[0]; if (codeword.Contains(guess)) { Console.WriteLine($"{guess} is in the word!"); for (int index = 0; index < codeword.Length; index++) { if (guess == codeword[index]) { currentword = currentword.Remove(index, 1).Insert(index, letter); } } } else { Console.WriteLine($"'{letter}' is not in the word!"); currentNumOfWrongGuesses++; u.AddPart(); } }
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(); } }
///<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(); } }
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(); } }
public void Ask() { Console.Write("Guess a letter: "); string input = Console.ReadLine(); while (input.Length != 1) { Console.Write("Only single characters are accepted!\nEnter another letter: "); input = Console.ReadLine(); } char guess = char.Parse(input); if (codeWord.Contains(guess)) { Console.WriteLine($"Correct! The word contains the letter {guess}."); while (codeWord.Contains(guess)) { int guessPos = codeWord.IndexOf(guess); codeWord = codeWord.Remove(guessPos, 1).Insert(guessPos, " "); currentWord = currentWord.Remove(guessPos, 1).Insert(guessPos, input); } } else { wrongGuess++; Console.WriteLine($"Wrong! The word does not contain the letter {guess}.\nYou have {maxGuess-wrongGuess} guesses remaining."); ufo.AddPart(); } }
public void Ask() { Console.Write("Guess a letter: "); string guessLetter = Console.ReadLine(); var guessChar = guessLetter.ToCharArray(); if (guessChar.Length != 1) { Console.WriteLine("Guess one letter at a time."); return; } if (codeWord.Contains(guessChar[0])) { Console.WriteLine($"The letter {guessChar[0]} appears in the word."); for (int i = 0; i < codeWord.Length; i++) { if (codeWord[i] == guessChar[0]) { currentWord = currentWord.Remove(i, 1).Insert(i, guessChar[0].ToString()); } } } else { Console.WriteLine($"Sorry! The letter {guessChar[0]} is not in the word..."); currentNumGuesses++; u.AddPart(); } }
/* Extra challenge: * The game doesn’t record past guesses and doesn’t check if a user has already guessed a letter. Add that feature. */ public void Ask() { Console.WriteLine("Guess a letter"); string guess = Console.ReadLine().ToLower(); if (guess.Length == 1) { Console.WriteLine($"Your guess was {guess}."); if (codeword.Contains(guess)) { Console.WriteLine($"Yay! {guess.ToUpper()} is in the codeword!"); for (int i = 0; i < codeword.Length; i++) { if (codeword[i].ToString() == guess) { currentWord = currentWord.Remove(i, 1).Insert(i, guess); } } } else { Console.WriteLine($"Oh no, {guess} is not in the codeword!"); currentWrongGuesses++; spaceship.AddPart(); } } else { Console.WriteLine("Please guess one letter at a time!"); } }
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(); } }
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(); } }
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]}"); } }
public void Ask() { Console.WriteLine("Please guess a character: "); string guessedChar = Console.ReadLine(); while (guessedChar.Length != 1) { Console.WriteLine("Please just enter one character: "); guessedChar = Console.ReadLine(); } if (this.codeWord.Contains(guessedChar)) { updateCurrentWord(Convert.ToChar(guessedChar)); } else { this.previousAnswer.Add(guessedChar); ufo.AddPart(); this.wrongGuess++; } }