public void MakeGuess_AddUnguessedLetter_LetterAppearsInGuessedLetters() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('A'); newGame.GuessedLetters.Should().Contain('A'); }
public SimpleHangmanDTO(HangmanGame hangmanGame) { GameId = hangmanGame.GameId; Word = hangmanGame.Word; Guesses = hangmanGame.Guesses; IsReady = hangmanGame.IsReady; }
public HangmanGame StartNewGame() { var game = new HangmanGame(Interlocked.Increment(ref NextGameID), WordList[Randomizer.Next(WordList.Count)]); GameHash.TryAdd(game.GameID, game); return(game); }
public Task DoExercise() { IHangmanGame hangmanGame = new HangmanGame(); hangmanGame.ResetGame(); Console.WriteLine($"A {hangmanGame.WordLength}-letter word is made up."); while (hangmanGame.GameStatus == GameStatuses.InProgress) { Console.Write($"{Environment.NewLine}Put a letter: "); var letter = Console.ReadKey().KeyChar; Console.Write(Environment.NewLine); if (hangmanGame.TryToGuessLetter(letter)) { Console.WriteLine("The letter is open."); Console.WriteLine(GetPrintableWord(hangmanGame)); } else { Console.WriteLine($"Wrong Guesses: {hangmanGame.AttemptsUsed} of {hangmanGame.MaximumTotalAttempts}"); Console.WriteLine($"Tried letters: {GetPrintableTriedLetters(hangmanGame)}"); } } Console.WriteLine(hangmanGame.GameStatus == GameStatuses.IsWin ? "You win." : $"You lose. The word is \"{hangmanGame.GetWordIfGameEnd()}\""); Console.ReadLine(); return(Task.CompletedTask); }
static void Main(string[] args) { HangmanGame game = new HangmanGame(); string word = game.GenerateWord(); Console.WriteLine($"The word consists of {word.Length} letters."); Console.WriteLine("Try to guess the word"); while (game.GameStatus == GameStatus.InProgress) { Console.WriteLine("Pick a letter"); char c = (char)Console.ReadLine().ToCharArray()[0]; string curState = game.GuessLetter(c); Console.WriteLine(curState); Console.WriteLine($"Remaining tries = {game.RemainingTries}"); Console.WriteLine($"Tried letters: {game.TriedLetters}"); } if (game.GameStatus == GameStatus.Lost) { Console.WriteLine("You're hanged."); Console.WriteLine($"The word was: {game.Word}"); } else if (game.GameStatus == GameStatus.Won) { Console.WriteLine("You won!"); } }
/// <summary> /// Initializes category choosing. /// </summary> /// <param name="game">Instance of <see cref="HangmanGame"/> class.</param> public override void Play(HangmanGame game) { var contentReader = new FolderContentReader(); string[] categoriesToList = contentReader.GetCategories(Globals.CategoriesPath, "*" + Globals.FileExtension); game.UI.Print(categoriesToList); game.UI.Print(Messages.EnterChoiceMessage, "NewLine"); string chosenCategory = game.UI.ReadLine(); bool categoryExists = this.CategoriesToLower(categoriesToList).Contains(chosenCategory.ToLower()); if (categoryExists) { game.WordSelect.FileName = "../../Words/" + chosenCategory + Globals.FileExtension; } else { game.UI.Print(Messages.WrongCommand, "NewLine"); this.Play(game); } game.State = new InitializeGameState(); game.State.Play(game); }
public async Task Hangman([Remainder] string type = "All") { var hm = new HangmanGame(Context.Channel, type); if (!HangmanGames.TryAdd(Context.Channel.Id, hm)) { await ReplyErrorLocalized("hangman_running").ConfigureAwait(false); return; } hm.OnEnded += g => { HangmanGame throwaway; HangmanGames.TryRemove(g.GameChannel.Id, out throwaway); }; try { hm.Start(); } catch (Exception ex) { try { await Context.Channel.SendErrorAsync(GetText("hangman_start_errored") + " " + ex.Message).ConfigureAwait(false); } catch { } HangmanGame throwaway; HangmanGames.TryRemove(Context.Channel.Id, out throwaway); throwaway.Dispose(); return; } await Context.Channel.SendConfirmAsync(GetText("hangman_game_started"), hm.ScrambledWord + "\n" + hm.GetHangman()); }
public void MakeGuess_AddLowercaseLetter_LowercaseLetterIsNotInGuessedLetters() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('a'); newGame.GuessedLetters.Should().NotContain('a'); }
public void AddCharGuess(char letter) { AvailableChars.Remove(letter); HangmanGame.GuessLetter(letter); DetermineImageSrc(); Evaluate(); }
public void HasLetterBeenGuessed_LowercaseLetterGuessed_LowercaseLetterReturnsTrue() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('a'); newGame.HasLetterBeenGuessed('a').Should().BeTrue(); }
public ActionResult Index() { Random rand = new Random(); HangmanGame newHangman = new HangmanGame(HangmanGame.wordArray[rand.Next(0, 3)]); return(View()); }
/// <summary> /// All possible in game command /// </summary> /// <param name="commandAsString">Command from user</param> /// <param name="game">An instance of HangmanGame</param> /// <param name="commandTypes">All in game commands</param> /// <returns>all possible in game commands</returns> public ICommand GetGameCommand(string commandAsString, HangmanGame game, IDictionary <string, System.Type> commandTypes) { var typeCommand = commandTypes[commandAsString]; var command = (ICommand)System.Activator.CreateInstance(typeCommand, game); return(command); }
public async Task Hangman([Remainder] string type = "All") { var hm = new HangmanGame(Context.Channel, type); if (!HangmanGames.TryAdd(Context.Channel.Id, hm)) { await Context.Channel.SendErrorAsync("Hangman game already running on this channel.").ConfigureAwait(false); return; } hm.OnEnded += (g) => { HangmanGame throwaway; HangmanGames.TryRemove(g.GameChannel.Id, out throwaway); }; try { hm.Start(); } catch (Exception ex) { try { await Context.Channel.SendErrorAsync($"Starting errored: {ex.Message}").ConfigureAwait(false); } catch { } HangmanGame throwaway; HangmanGames.TryRemove(Context.Channel.Id, out throwaway); throwaway.Dispose(); return; } await Context.Channel.SendConfirmAsync("Hangman game started", hm.ScrambledWord + "\n" + hm.GetHangman() + "\n" + hm.ScrambledWord); }
public void GivenWordGenerated_WhenIMakeSevenMistakes_AndWillTryAgain_ThenILoseAndWin() { MockAppleWord(); _visualizationProviderMock.SetupSequence(x => x.AcceptGuess()) .Returns('a') .Returns('p') .Returns('n') .Returns('s') .Returns('d') .Returns('f') .Returns('z') .Returns('q') .Returns('y') .Returns('a') .Returns('p') .Returns('l') .Returns('e'); _visualizationProviderMock.SetupSequence(x => x.RequestIsContinue()) .Returns(true) .Returns(false); var game = new HangmanGame(_wordsProviderMock.Object, _visualizationProviderMock.Object); game.Run(); _visualizationProviderMock.Verify(x => x.AddVictimPart(), Times.Exactly(7)); _visualizationProviderMock.Verify(x => x.GameOver(It.Is <bool>(x => x == false)), Times.Once); _visualizationProviderMock.Verify(x => x.GameOver(It.Is <bool>(x => x == true)), Times.Once); }
private Task <string> GetStringWordResponse(HangmanGame game) { return(Task.Run(() => { var responseBuilder = new StringBuilder("`"); for (int i = 0; i < game.TargetWord.Length; i++) { bool isMatched = false; for (int j = 0; j < game.CorrectGuessedLetters.Length; j++) { if (game.TargetWord[i] == game.CorrectGuessedLetters[j]) { responseBuilder.Append($"{game.CorrectGuessedLetters[j]} "); isMatched = true; break; } } if (!isMatched) { responseBuilder.Append("_ "); } } responseBuilder.Append("`"); var v = responseBuilder.ToString(); return v; })); }
public void ctor_CreationOfGame_InitialValuesAreSet() { var newGame = new HangmanGame("Zebra"); newGame.GuessCount.Should().Be(0); newGame.HasWordBeenGuessed.Should().BeFalse(); newGame.GuessedLetters.Should().BeEmpty(); }
private static void UpdateScreenWithCurrentGuesses(HangmanGame currentGame) { Console.Clear(); Console.WriteLine($"Unguessed Letters: {currentGame.FormattedUnguessedLetters}"); Console.WriteLine($"Guesses Remaining: {currentGame.GuessesRemaining}"); Console.WriteLine(""); Console.WriteLine($"Word: {currentGame.MaskedWord}"); }
public void MakeGuess_LettersAreGuessed_GuessCountIncreases() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('a'); newGame.MakeGuess('b'); newGame.GuessCount.Should().Be(2); }
public void GetMaskedWord_OnlyIncorrectGuesses_ReturnsCorrectMaskedValue() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('x'); newGame.MakeGuess('y'); newGame.MaskedWord.Should().Be("_ _ _ _ _"); }
public void HasWordBeenGuessed_WordNotGUessedYet_ReturnsFalse() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('z'); newGame.MakeGuess('e'); newGame.HasWordBeenGuessed.Should().BeFalse(); }
public void GuessesRemaining_WithOnlyCorrectGuesses_ShouldBeMaxGuesses() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('z'); newGame.MakeGuess('e'); newGame.GuessesRemaining.Should().Be(newGame.MaxGuesses); }
/// <summary> /// Save player score and change game state to restart. /// </summary> /// <param name="game">Instance of <see cref="HangmanGame"/> class.</param> public override void Play(HangmanGame game) { game.UI.Print(string.Format(Messages.WinGameMessage, game.Player.Score), "Message"); game.UI.Print(game.Player.HiddenWord, "SecretWord"); game.Scores.PlacePlayerInScoreBoard(game.Player); game.State = new RestartGameState(); game.State.Play(game); }
public void IncorrectLetterCount_WithOnlyIncorrectGuesses_ShouldReturnCorrectCount() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('w'); newGame.MakeGuess('d'); newGame.IncorrectGuessCount.Should().Be(2); }
public void HangmanGameIsCreatedAtLeastTwice() { var wordInitializer = new WordInitializer(); var gameOne = new HangmanGame(wordInitializer); var gameTwo = new HangmanGame(wordInitializer); Assert.ReferenceEquals(gameOne.WordInitializer, gameTwo.WordInitializer); }
/// <summary> /// Executes play command and starts the game. /// </summary> public override void Execute() { var wordSelector = new WordSelectorFromFile(); var player = new Player(); var commandFactory = new CommandFactory(); var game = new HangmanGame(this.UI, new PlayerInitializationState(), wordSelector, player, commandFactory); game.StartGame(); }
public void GetMaskedWord_WordWithRepeatingLetter_ReturnsCorrectMaskedValue() { var newGame = new HangmanGame("Banana"); newGame.MakeGuess('b'); newGame.MakeGuess('a'); newGame.MaskedWord.Should().Be("B A _ A _ A"); }
public void GetMaskedWord_WithLettersGuessed_ReturnsMaskedValue() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('z'); newGame.MakeGuess('e'); newGame.MaskedWord.Should().Be("Z E _ _ _"); }
public void IncorrectLetterCount_WithGoodAndBadGuesses_ShouldReturnCorrectCount() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('z'); newGame.MakeGuess('e'); newGame.MakeGuess('d'); newGame.IncorrectGuessCount.Should().Be(1); }
public async Task AddGameAsync(ulong id, HangmanGame game) { await Task.Run(() => { while (!State.TryAdd(id, game)) { Task.Delay(1); } }); }
public void test_IsGoodGuess_returns_true_when_guess_is_correct() { var game = new HangmanGame() { CurrentGameWord = "Test", GuessedLetters = new HashSet<char>() }; Assert.IsTrue(game.IsGoodGuess('a', "abc")); }
private async Task <bool> CheckForLoss(HangmanGame game, CommandContext context) { if (game.Mistakes == 11) { await LossResponse(game, context); return(true); } return(false); }
public void GuessesRemaining_WithIncorrectGuesses_ShouldDecrementCorrectly() { var newGame = new HangmanGame("Zebra"); newGame.MakeGuess('z'); newGame.MakeGuess('d'); newGame.MakeGuess('y'); newGame.GuessesRemaining.Should().Be(8); }
public void test_GetClue_blank_when_no_guesses() { var game = new HangmanGame() { CurrentGameWord = "Test", GuessedLetters = new HashSet<char>() }; const string expected = "_ _ _ _ "; Assert.AreEqual(expected, game.GetClue(game.CurrentGameWord)); }
/// <summary> /// This is the entry point to our application. /// </summary> public static void Main() { var game = new HangmanGame(); while (true) // play forever { game.Play(); // TODO: Let user choose whether to play again } }
/// <summary> /// Initialize player. /// </summary> /// <param name="game">Instance of <see cref="HangmanGame"/> class.</param> public override void Play(HangmanGame game) { game.UI.Print("Title", "Title"); game.UI.Print("GameInit", "GameInit"); string name = game.UI.ReadLine(); var playerName = string.IsNullOrWhiteSpace(name) ? "unknown" : name; game.Player.Name = playerName; game.Player.Score = 50; game.State = new ChooseCategoryState(); game.State.Play(game); }
public void test_GetClue_includes_guessess() { var game = new HangmanGame() { CurrentGameWord = "Test", GuessedLetters = new HashSet<char>() }; game.GuessedLetters.Add('e'); const string expected = "_ e _ _ "; Assert.AreEqual(expected, game.GetClue(game.CurrentGameWord)); }
private void UpdatePlayer(HangmanGame hangmanGame, char supposedChar) { int numberOfTheAppearancesOfTheSupposedChar = this.Game.Player.Word.Count(x => x.Equals(this.Game.CurrentCommand[0])); if (numberOfTheAppearancesOfTheSupposedChar == 0) { this.Game.Player.Lives--; this.Game.Player.WrongLetters.Add(supposedChar); } else { this.Game.Player.Score += Globals.ScoreToAdd; } }
/// <summary> /// Put initial lives to player and select new play word /// </summary> /// <param name="game">Instance of <see cref="HangmanGame"/> class.</param> public override void Play(HangmanGame game) { game.Player.Lives = 7; game.Player.WrongLetters = new HashSet<char>(); game.Player.Word = game.WordSelect.SelectRandomWord(); game.Player.HiddenWord = new string('_', game.Player.Word.Length); game.UI.Print("Title", "Title"); game.UI.Print("|", "MiddleBorder"); game.UI.Print(game.Player.Lives.ToString(), "Lives"); game.UI.Print(game.Player.HiddenWord, "SecretWord"); game.UI.Print(game.Player.WrongLetters); game.UI.Print(string.Empty, "EnterCommand"); game.State = new PlayGameState(); game.State.Play(game); }
/// <summary> /// Initializes a new instance of the <see cref="StartGameCommand"/> class. /// </summary> /// <param name="currentGame">Instance of <see cref="HangmanGame"/> class.</param> public StartGameCommand(HangmanGame currentGame) : base(currentGame) { }
/// <summary> /// Initializes a new instance of the <see cref="HelpCommand"/> class. /// </summary> /// <param name="currentGame">Instance of <see cref="HangmanGame"/> class.</param> public HelpCommand(HangmanGame currentGame) : base(currentGame) { }
/// <summary> /// All possible in game command /// </summary> /// <param name="commandAsString">Command from user</param> /// <param name="game">An instance of HangmanGame</param> /// <param name="commandTypes">All in game commands</param> /// <returns>all possible in game commands</returns> public ICommand GetGameCommand(string commandAsString, HangmanGame game, IDictionary<string, System.Type> commandTypes) { var typeCommand = commandTypes[commandAsString]; var command = (ICommand)System.Activator.CreateInstance(typeCommand, game); return command; }
/// <summary> /// Play command for game start /// </summary> /// <param name="game">Instance of <see cref="HangmanGame"/> class.</param> public abstract void Play(HangmanGame game);
/// <summary> /// Initializes a new instance of the <see cref="GameCommand"/> class. /// </summary> /// <param name="currentGame">Instance of <see cref="HangmanGame"/> class.???</param> public GameCommand(HangmanGame currentGame) { this.Game = currentGame; }
/// <summary> /// Initializes a new instance of the <see cref="RevealGuessedLettersCommand"/> class. /// </summary> /// <param name="game">Instance of <see cref="HangmanGame"/> class.</param> public RevealGuessedLettersCommand(HangmanGame game) : base(game) { }
public HangmanEngine(IPrinter printer, IReader inputReader, CommandFactory commandFactory, Validator validator, HangmanGame hangmanGame) : base(printer, inputReader, commandFactory, validator, hangmanGame) { }