Esempio n. 1
0
        /// <summary>
        /// Runs and handles the hangman game.
        /// </summary>
        private void GameLoop()
        {
            _isRunning = true;

            while (wordGuessed.Contains('_'))
            {
                // Game screen.
                Console.Clear();
                _gameVisualizer.RefreshGameScreen(wordGuessed, incorrectGuesses);

                // Request the users next guess.
                _gameVisualizer.RequestGuess();
                var playerGuess = Console.ReadLine().ToUpper();

                if (ValidateGuess(playerGuess))
                {
                    var guess = Convert.ToChar(playerGuess);

                    if (!wordGuessed.Contains(guess) && !incorrectGuesses.Contains(guess))
                    {
                        if (wordToGuess.Contains(guess))
                        {
                            // Handle a correct guess.
                            for (var x = 0; x < wordToGuess.Count; x++)
                            {
                                if (wordToGuess[x] == guess)
                                {
                                    wordGuessed[x] = guess;
                                }
                            }
                        }
                        else
                        {
                            // Handle an incorrect guess.
                            incorrectGuesses.Add(guess);
                            if (incorrectGuesses.Count >= 6)
                            {
                                _gameVisualizer.LoseScreen(wordToGuess);
                            }
                        }
                    }
                    else
                    {
                        _gameVisualizer.AlreadyGuessed();
                    }
                }
                else
                {
                    _gameVisualizer.InvalidGuess();
                }
            }

            // The player must have won.
            _gameVisualizer.WinScreen(wordToGuess, incorrectGuesses.Count);
        }