public HangmanGame(HangmanDifficulty diff) { if (diff == null) { throw new HangmanException("Game Difficulty must not be null."); } _difficulty = diff; IsGameStarted = false; _timer.Elapsed += (sender, args) => { if (IsGameStarted) { CheckGameState(); OnSecondElapsed?.Invoke(_lastGameState); } }; }
public void StartGame(String word = null) { if (IsGameStarted) { throw new HangmanGameAlreadyStartedException(); } if (_difficultyPending != null) { _difficulty = _difficultyPending; _difficultyPending = null; } var client = new WebClient(); if (word != null) { GivenWord = word.ToUpper(); } else { var wordLength = _random.Next(_difficulty.MinimumLetters, 20); try { var data = client.DownloadString(WordProvider).ToUpper(); var strings = JsonConvert.DeserializeObject <String[]>(data); GivenWord = strings[0]; } catch (WebException) { throw new HangmanGameUnableToStartException("Couldn't fetch a random word from Online API"); } } _lastState = HangmanState.Started; CorrectLetters.Clear(); IncorrectLetters.Clear(); _timer.Start(); _stopwatch.Reset(); _stopwatch.Start(); IsGameStarted = true; FetchGameState(); OnStart?.Invoke(_lastGameState); }