Example #1
0
        //Allow to input a guess from outside od the dll (from the ui)
        public bool Guess(char letter)
        {
            bool res = false;

            //validate the input
            if (!ValidateLetter(letter))
            {
                //if not valid, do not cotinue and throw an argument exception
                throw new ArgumentException($"{letter} is not a valid English letter");
            }
            //use upper case letters only
            char upperLetter = char.ToUpper(letter);

            //check if the game is active and that the letter is available to guess
            if (IsGameActive && GamePlayer.IsAvaileableToGuess(upperLetter))
            {
                //guess the letter in two places,
                //in the word model
                //res will be the flag that will tell the user of the class if he was successful in his guess
                res = WordToGuess.Guess(upperLetter);
                //in the Player model
                GamePlayer.Guess(upperLetter, res);

                //check if the game is done(player guessed the entire word)
                if (WordToGuess.IsComplete)
                {
                    //acitvate the win game method(holds instructions for the end of the game)
                    WinGame();
                    //return sucsessfull guess and exit the method
                    return(true);
                }

                //the guess was unsuccessful
                if (!res)
                {
                    //update the hang man status
                    HangedManStatus++;
                }
            }
            return(res);
        }
Example #2
0
        public void ReadyToStartGame()
        {
            while (GameRunning == false)
            {
                Words  word     = new Words();
                Random nextWord = new Random();

                // Takes a word form the WordPool and get the length off that.
                WordToGuess = word.WordPool[nextWord.Next(0, word.WordPool.Length)];

                // I want it to be LowerCase so no matter how the user inputs the input it will check it,
                // and uppercase is easier to work with
                WordToGuessToLowerCase = WordToGuess.ToLower();

                DisplayToPlayer = new StringBuilder(WordToGuess.Length);
                for (int i = 0; i < WordToGuess.Length; i++)
                {
                    DisplayToPlayer.Append('-');
                }
                GameRunning = true;
            }
        }
Example #3
0
        public void GetWrongGuessesIsAccurate()
        {
            // Arrange
            Guess        guess       = new Guess();
            WordToGuess  wordToGuess = new WordToGuess();
            const string THE_WORD    = "jellybean";

            wordToGuess.setWord(THE_WORD);
            const string INCORRECT_LETTER = "z";

            guess.guessedLetter(INCORRECT_LETTER, wordToGuess);
            const string ANOTHER_INCORRECT_LETTER = "x";

            guess.guessedLetter(ANOTHER_INCORRECT_LETTER, wordToGuess);

            // Act
            int actualWrongGuessCount = guess.getWrongGuesses();

            // Assert
            const int EXPECTED_WRONG_GUESS_COUNT = 2;

            Assert.AreEqual(EXPECTED_WRONG_GUESS_COUNT, actualWrongGuessCount);
        }
Example #4
0
        public void Guess(string letter)
        {
            letter = letter.ToUpper();

            // is it a valid guess? game still going, letter not guessed before?
            if (!IsValidGuess(letter))
            {
                return;
            }

            // if letter isn't in word, then increase the number of incorrect guesses
            if (!WordToGuess.ToUpper().Contains(letter))
            {
                NrOfIncorrectGuesses++;
            }

            GuessedLetters.Add(letter);

            // has the word been solved?
            if (HasWordBeenGuessed())
            {
                WordGuessed = true;
            }
        }
Example #5
0
        void RunGameLoop()
        {
            HangmanConsoleWriter.PrintGuesses(Guesses);

            GenerateTiles();
            var guess = HangmanConsoleWriter.RequestInput();

            if (HangmanInputValidation.ValidateInput(guess))
            {
                var guessCharacter = guess[0];
                Guesses.Add(guessCharacter);

                if (!WordToGuess.Contains(guessCharacter))
                {
                    NumberOfWrongGuesses++;
                }
                else
                {
                    if (HaveWeWonYet())
                    {
                        GameIsOngoing = false;

                        HangmanConsoleWriter.WinnerOutput(WordToGuess);
                        return;
                    }
                }
            }
            else
            {
                HangmanConsoleWriter.InvalidGuess();
            }

            DrawHangman();

            CheckNumberOfWrongGuesses();
        }
Example #6
0
        public bool MakeGuess(char letter)
        {
            if (GuessedLetters.Contains(letter))
            {
                if (++gameStep > 6)
                {
                    GameOver   = true;
                    GameResult = false;
                    return(false);
                }
            }

            if (WordToGuess.ToString().Contains(letter))
            {
                GuessedLetters.Add(letter);

                if (MaskedWord() == WordToGuess.ToString())
                {
                    GameOver   = true;
                    GameResult = true;
                }

                return(true);
            }
            else
            {
                if (++gameStep > 6)
                {
                    GameOver   = true;
                    GameResult = false;
                    return(false);
                }
            }

            return(false);
        }
Example #7
0
 //will print the (Coded) Word
 public override string ToString()
 {
     return(WordToGuess.ToString());
 }
Example #8
0
 public void startGame()
 {
     guess       = new Guess();
     wordToGuess = new WordToGuess();
 }
 private bool HasBeenDiscoveredEveryTime(char guessChar)
 {
     return(WordToGuess.Count(x => x == guessChar) == HiddenWordToGuess.Count(x => x == guessChar));
 }