static bool GameLoop(string word) { var secret = new SecretWord(word); var game = new WordGame(secret, 10); RenderSet(game, 10); while (game.GameOver == false) { Console.WriteLine("Guess a letter or the whole word"); var input = Console.ReadLine(); if (input.Length == 1f && game.MakeAGuess(input[0])) { } else if (input.Length > 1) { game.MakeAGuess(input); } else if (input.Length > 0) { if (WordGame.IsValidCharacter(input[0])) { Console.WriteLine("Cannot guess the same letter twice."); } else { Console.WriteLine("Only letters a-z is accepted"); } Console.ReadKey(true); } RenderSet(game, 10); } return(game.Solved); }
private void ProcessGuess(string letter) { if (SecretWord.Contains(letter)) { UpdateLabel(letter); CheckForWin(); } else { WrongGuesses++; UpdatePicture(); } }
public bool MakeAGuess(string word) { foreach (char c in word) { if (IsValidCharacter(c) == false && c != ' ') { return(false); } } if (SecretWord.IsWord(word)) { foreach (var c in word) { guessedCharacters.Add(c); } } TotalGuesses++; return(true); }
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); }