using System; namespace AssignmentFinal { class Program { static int numGuess; static int lettersLeft; public static void Main(string[] args) { string wordToGuess = GetSecretWord(); char[] maskWord = GetHiddenLetters(wordToGuess, '-'); lettersLeft = wordToGuess.Length; char userGuess; numGuess = (wordToGuess.Length) * 2; Console.WriteLine("Welcome to Guess The Word game!"); while (numGuess > 0 && lettersLeft > 0) { Console.WriteLine("You have " + numGuess + " guesses!"); DisplayBlanks(maskWord); Console.Write("Enter your guess: "); userGuess = char.Parse(Console.ReadLine()); maskWord = CheckGuess(userGuess, wordToGuess, maskWord); } //if wrongGuess is 0 exit the program; if (numGuess == 0) { Console.WriteLine("You Lost, the secret word was: " + wordToGuess); } } static string GetSecretWord() { Random num = new Random(); string[] words = { "hyperbaric", "temporal", "quantum", "radiation", "aardvark", "accident", "dracolich", "professional", "properties", "collections" }; int wordIndex = num.Next(words.Length); string secretWord = words[wordIndex]; return secretWord; } static char[] GetHiddenLetters(string word, char mask) { char[] hidden = new char[word.Length]; for (int i = 0; i < word.Length; i++) { hidden[i] = mask; } return hidden; } static void DisplayBlanks(char[] characters) { Console.Write("Secret Word is: "); foreach (char letter in characters) { Console.Write(letter); } Console.WriteLine(); } static char[] CheckGuess(char letterToCheck, string word, char[] characters) { //boolean to check for wrong guess bool wrongGuess = true; if ( numGuess > 0) { for (int i = 0; i < word.Length; i++) { if (word[i] == letterToCheck) { characters[i] = word[i]; lettersLeft--; //if match set wrong to false wrongGuess = false; } } } //if wrong guess decrement if (wrongGuess) { numGuess--; } return characters; } } }
bool
false
a => 0, A = > 0, b => 1, B => 1, ...
true
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)