예제 #1
0
        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();
        }
예제 #2
0
 public void TrySolve(String word)
 {
     if (String.IsNullOrEmpty(word))
     {
         throw new HangmanException("TrySolve(string) expected a string of letters of the English Alphabet. Got empty or null string");
     }
     if (word.Any(x => !char.IsLetter(x)))
     {
         throw new HangmanException("TrySolve(string) expected a string of letters of the English Alphabet. Got " + word);
     }
     if (!IsGameStarted)
     {
         throw new HangmanGameNotStartedException();
     }
     _lastState = HangmanState.SolveTried;
     CheckGameState();
     OnAttempt?.Invoke(_lastGameState);
     if (GivenWord.Equals(word.ToUpper()))
     {
         var report = new HangmanGameReport()
         {
             Result = HangmanResult.WonByGuessing,
             Word   = GivenWord,
             State  = _lastGameState
         };
         WonGames++;
         EndGame(report);
     }
     else
     {
         var report = new HangmanGameReport()
         {
             Result = HangmanResult.LostByGuessing,
             Word   = GivenWord,
             State  = _lastGameState
         };
         LostGames++;
         EndGame(report);
     }
 }