Exemplo n.º 1
0
 private void EndGame(HangmanGameReport report)
 {
     _lastState         = HangmanState.Finished;
     report.State.State = _lastState;
     History.Add(report);
     _stopwatch.Stop();
     _timer.Stop();
     IsGameStarted = false;
     OnFinish?.Invoke(report);
 }
Exemplo n.º 2
0
        public void StopGame()
        {
            if (!IsGameStarted)
            {
                throw new HangmanGameNotStartedException();
            }
            _lastState = HangmanState.Stopped;
            FetchGameState();
            var report = new HangmanGameReport()
            {
                Result = HangmanResult.Stopped,
                Word   = GivenWord,
                State  = _lastGameState
            };

            LostGames++;
            EndGame(report);
        }
Exemplo n.º 3
0
        private void CheckGameState()
        {
            if (!IsGameStarted)
            {
                return;
            }
            FetchGameState();

            if (_difficulty.IsTimeLimited && _stopwatch.Elapsed.Seconds >= _difficulty.TimeLimit)
            {
                var report = new HangmanGameReport()
                {
                    Result = HangmanResult.LostTimeout,
                    Word   = GivenWord,
                    State  = _lastGameState
                };
                LostGames++;
                EndGame(report);
            }
            else if (IncorrectLetters.Count > _difficulty.ToleretableErrors)
            {
                var report = new HangmanGameReport()
                {
                    Result = HangmanResult.LostErrors,
                    Word   = GivenWord,
                    State  = _lastGameState
                };
                LostGames++;
                EndGame(report);
            }
            else if (!DisplayWord.Contains("_"))
            {
                var report = new HangmanGameReport()
                {
                    Result = HangmanResult.WonByTrying,
                    Word   = GivenWord,
                    State  = _lastGameState
                };
                WonGames++;
                EndGame(report);
            }
        }
Exemplo n.º 4
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);
     }
 }