void init() { if (Category != null) { txtTime.Content = "02:00"; helper = new FileHelper(Category); Random rnd = new Random(); int random_start = rnd.Next(helper.WordCount()); CurrentWord = random_start; CurrentState = HangmanState.stage1; TimerStart = DateTime.Now.AddMinutes(2); timer.Interval = TimeSpan.FromSeconds(1); timer.IsEnabled = true; timer.Tick += dispatcherEvent; Hangman = new HangmanAvatar(image); txtCategory.Content = Category; txtTime.Foreground = Brushes.White; msgSelect_Cat.Visibility = Visibility.Hidden; txtJumbled.Visibility = Visibility.Visible; txtPrompt.Visibility = Visibility.Visible; txtScore.Content = 0; txtJumbled.Content = helper.JumbledWords[CurrentWord]; tabControl.SelectedIndex = 1; WordsCounter = 0; } }
public Hangman(string word) { _word = word; var initialState = new HangmanState(MaskWord(Array.Empty <char>()), ImmutableHashSet <char> .Empty, 9); _state = new BehaviorSubject <HangmanState>(initialState); }
private void EndGame(HangmanGameReport report) { _lastState = HangmanState.Finished; report.State.State = _lastState; History.Add(report); _stopwatch.Stop(); _timer.Stop(); IsGameStarted = false; OnFinish?.Invoke(report); }
public void Initially_no_letters_are_guessed() { var game = new HangmanGame("foo"); HangmanState lastState = null; game.StateChanged += (sender, state) => lastState = state; game.Start(); Assert.Equal("___", lastState.MaskedWord); }
public HangmanGame(string word) { this.word = word; state = new HangmanState { RemainingGuesses = NumberOfAllowedGuesses, Guesses = new HashSet <char>() }; UpdateMaskedWord(); UpdateStatus(); }
public void Initially_9_failures_are_allowed() { var game = new HangmanGame("foo"); HangmanState lastState = null; game.StateChanged += (sender, state) => lastState = state; game.Start(); Assert.Equal(HangmanStatus.Busy, lastState.Status); Assert.Equal(9, lastState.RemainingGuesses); }
public void Guess_changes_state() { var hangman = new Hangman("foo"); HangmanState actual = null; hangman.StateObservable.Subscribe(x => actual = x); var initial = actual; // +--x-> // +a-b-> hangman.GuessObserver.OnNext('x'); Assert.NotEqual(initial, actual); }
public void Wrong_guess_decrements_remaining_guesses() { var hangman = new Hangman("foo"); HangmanState actual = null; hangman.StateObservable.Subscribe(x => actual = x); var initial = actual; // +--x-> // +a-b-> hangman.GuessObserver.OnNext('x'); Assert.Equal(initial.RemainingGuesses - 1, actual.RemainingGuesses); }
public void ResetGame() { Hangman.Reset(); TimerStart = DateTime.Now.AddMinutes(2); txtTime.Foreground = Brushes.White; timer.IsEnabled = false; CurrentState = HangmanState.stage1; txtTime.Content = "02:00"; txtScore.Content = 0; txtJumbled.Content = ""; WordsCounter = 0; init(); }
public void After_10_failures_the_game_is_over() { var game = new HangmanGame("foo"); HangmanState lastState = null; game.StateChanged += (sender, state) => lastState = state; game.Start(); for (var i = 0; i < 10; i++) { game.Guess('x'); } Assert.Equal(HangmanStatus.Lose, lastState.Status); }
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); }
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(); }
public Hangman(string word) { var emptySetOfChars = new HashSet <char>(); var maskedWord = MaskedWord(word, emptySetOfChars); var hangmanState = new HangmanState(maskedWord, emptySetOfChars.ToImmutableHashSet(), MaxGuessCount); var stateSubject = new BehaviorSubject <HangmanState>(hangmanState); StateObservable = stateSubject; GuessObserver = Observer.Create <char>(@char => { var guessedChars = new HashSet <char>(stateSubject.Value.GuessedChars); var isHit = !guessedChars.Contains(@char) && word.Contains(@char); guessedChars.Add(@char); var maskedWord = MaskedWord(word, guessedChars); if (maskedWord == word) { stateSubject.OnCompleted(); } else if (stateSubject.Value.RemainingGuesses < 1) { stateSubject.OnError(new TooManyGuessesException()); } else { var guessCount = isHit ? stateSubject.Value.RemainingGuesses : stateSubject.Value.RemainingGuesses - 1; var hangmanState = new HangmanState(maskedWord, guessedChars.ToImmutableHashSet(), guessCount); stateSubject.OnNext(hangmanState); } }); }
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); }
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); } }
public void Feeding_a_correct_letter_twice_counts_as_a_failure() { var game = new HangmanGame("foobar"); HangmanState lastState = null; game.StateChanged += (sender, state) => lastState = state; game.Start(); game.Guess('b'); Assert.Equal(HangmanStatus.Busy, lastState.Status); Assert.Equal(9, lastState.RemainingGuesses); Assert.Equal("___b__", lastState.MaskedWord); game.Guess('b'); Assert.Equal(HangmanStatus.Busy, lastState.Status); Assert.Equal(8, lastState.RemainingGuesses); Assert.Equal("___b__", lastState.MaskedWord); }
public void Feeding_a_correct_letter_removes_underscores() { var game = new HangmanGame("foobar"); HangmanState lastState = null; game.StateChanged += (sender, state) => lastState = state; game.Start(); game.Guess('b'); Assert.Equal(HangmanStatus.Busy, lastState.Status); Assert.Equal(9, lastState.RemainingGuesses); Assert.Equal("___b__", lastState.MaskedWord); game.Guess('o'); Assert.Equal(HangmanStatus.Busy, lastState.Status); Assert.Equal(9, lastState.RemainingGuesses); Assert.Equal("_oob__", lastState.MaskedWord); }
public void Getting_all_the_letters_right_makes_for_a_win() { var game = new HangmanGame("hello"); HangmanState lastState = null; game.StateChanged += (sender, state) => lastState = state; game.Start(); game.Guess('b'); Assert.Equal(HangmanStatus.Busy, lastState.Status); Assert.Equal(8, lastState.RemainingGuesses); Assert.Equal("_____", lastState.MaskedWord); game.Guess('e'); Assert.Equal(HangmanStatus.Busy, lastState.Status); Assert.Equal(8, lastState.RemainingGuesses); Assert.Equal("_e___", lastState.MaskedWord); game.Guess('l'); Assert.Equal(HangmanStatus.Busy, lastState.Status); Assert.Equal(8, lastState.RemainingGuesses); Assert.Equal("_ell_", lastState.MaskedWord); game.Guess('o'); Assert.Equal(HangmanStatus.Busy, lastState.Status); Assert.Equal(8, lastState.RemainingGuesses); Assert.Equal("_ello", lastState.MaskedWord); game.Guess('h'); Assert.Equal(HangmanStatus.Win, lastState.Status); Assert.Equal("hello", lastState.MaskedWord); }
private void Window_KeyDown(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Enter: if (Category != null && !txtJumbled.Content.Equals("")) { string word = helper.Words[CurrentWord]; string answer = txtAnswer.Text; if (word.ToLower().Equals(answer.ToLower())) { txtAnswer.Text = ""; Player.Score += 50; txtScore.Content = Convert.ToString(Player.Score); helper.Words.RemoveAt(CurrentWord); helper.JumbledWords.RemoveAt(CurrentWord); correct(); LoopSound(); if (helper.WordCount() == 0) { timer.IsEnabled = false; MessageBoxResult result = MessageBox.Show($"You got {WordsCounter} words\nPlay again?", "You won!!", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { init(); } else { new MainWindow().Show(); this.Close(); } } Random rnd = new Random(); int random_start = rnd.Next(0, helper.WordCount()); CurrentWord = random_start; WordsCounter += 1; txtJumbled.Content = helper.JumbledWords[CurrentWord]; } else { CurrentState += 1; incorrect(); LoopSound(); if (CurrentState <= HangmanState.final_stage) { Hangman.ChangeImage((int)CurrentState); } else { Hangman.ChangeImage((int)CurrentState); timer.IsEnabled = false; MessageBoxResult result = MessageBox.Show($"You got {WordsCounter} words\nTry again?", "OOPS! You lost", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { init(); } else { new MainWindow().Show(); this.Close(); } } } } break; } }