static void Main(string[] args) { Console.WriteLine("Welcome to Hangman\n"); //game loop do { string again; GameState currentGameState = GameState.GamePlaying; GuessChecker checkCharacter = new GuessChecker(); HangmanPicture drawHangman = new HangmanPicture(); Random rnd = new Random(); SecretWord currentWord = new SecretWord(rnd.Next(20)); //converts the secret word into a char array, then creates a DisplayLetter object for //each char in the array and adds these to a list char[] currentArray = currentWord.Word.ToCharArray(); List <DisplayLetter> secretLetters = new List <DisplayLetter>(); foreach (char letter in currentArray) { DisplayLetter character = new DisplayLetter(letter); secretLetters.Add(character); } do { //runs through each letter in the word and displays either an underscore //or the letter if it was already guessed foreach (DisplayLetter item in secretLetters) { item.DisplayCharacters(item); } Console.WriteLine(); Console.WriteLine(); checkCharacter.DisplayGuesses(); checkCharacter.CheckGuess(secretLetters, drawHangman); Console.WriteLine(drawHangman.HangImg); //checks to see if the user has guessed all the letters or made //too many incorrect guesses if (checkCharacter.Correct == currentArray.Length) { foreach (DisplayLetter item in secretLetters) { item.DisplayCharacters(item); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("You have Won!"); Console.WriteLine(); currentGameState = GameState.GameWon; } if (checkCharacter.Incorrect == 10) { Console.WriteLine("Hangman!"); currentGameState = GameState.GameOver; } } while (currentGameState == GameState.GamePlaying); do { Console.WriteLine("Play again? (Y/N)"); ConsoleKeyInfo keyPress = Console.ReadKey(); again = keyPress.Key.ToString(); Console.WriteLine(); if (again.ToUpper() != "Y" && again.ToUpper() != "N") { Console.WriteLine("Invalid response, please enter Y or N"); continue; } else { break; } } while (true); if (again.ToUpper() == "Y") { Console.WriteLine(); continue; } else { break; } } while (true); }
//this method checks if the char inputted by the user matches any letters in the secret word public void CheckGuess(List <DisplayLetter> displayLetters, HangmanPicture draw) { bool correctGuess = false; bool validInput = false; char guess; Console.Write("Please enter a letter: "); do { int checkedLetters = 0; //Gets user input ConsoleKeyInfo info = Console.ReadKey(); string keyString = info.Key.ToString(); guess = keyString[0]; Console.WriteLine(); //checks user input against already guessed letters, if letter has already been guessed prompts user //to try another letter foreach (char character in guesses) { if (character == char.ToUpper(guess)) { Console.Write("You have already entered \"" + guess + "\", please enter a different letter: "); break; } else { checkedLetters++; } } if (checkedLetters == guesses.Count()) { validInput = true; } } while (validInput == false); Console.WriteLine(); //checks user input against secretWord foreach (DisplayLetter item in displayLetters) { if (item.Letter == char.ToUpper(guess) && item.Guessed == false) { item.Guessed = true; correctGuess = true; this.Correct++; } } if (correctGuess) { Console.WriteLine("You guessed correctly!"); } else { this.Incorrect++; Console.WriteLine("I'm sorry, you guessed incorrectly"); draw.Draw(this.Incorrect); } this.guesses.Add(guess); Console.WriteLine(); }