private async Task ValidateLetter() { Attempts++; var wordArray = Word.ToCharArray(); if (wordArray.Contains(Letter)) { foreach (Match m in Regex.Matches(Word, Letter.ToString())) { Letters[m.Index] = Letter; } if (!Letters.Any(c => c == '_')) { await Win(); } } else { IncorrectLetters.Add(Letter); SetImage(); if (IncorrectLetters.Count == 6) { await Loose(); } } }
public void TryLetter(String letter) { if (String.IsNullOrEmpty(letter)) { throw new HangmanException("TryLetter(string) expected a Letter of the English Alphabet. Got empty or null string"); } if (letter.Length > 1) { throw new HangmanException("TryLetter(string) expected a Letter of the English Alphabet. Got " + letter); } if (letter.Any(x => !char.IsLetter(x))) { throw new HangmanException("TryLetter(string) expected a Letter of the English Alphabet. Got " + letter); } if (!IsGameStarted) { throw new HangmanGameNotStartedException(); } _lastState = HangmanState.LetterTried; var c = letter.ToUpper(); if (GivenWord.Contains(c)) { if (!CorrectLetters.Contains(c)) { CorrectLetters.Add(c); FetchGameState(); OnAttempt?.Invoke(_lastGameState); } } else { if (!IncorrectLetters.Contains(c)) { IncorrectLetters.Add(c); FetchGameState(); OnAttempt?.Invoke(_lastGameState); } } CheckGameState(); }