Exemplo n.º 1
0
 public void CheckUser(UserPlayer newPlayer)
 {
     foreach (UserPlayer existing in Users)
     {
         if (existing.Name == newPlayer.Name)
         {
             existing.Wins   = existing.Wins + newPlayer.Wins;
             existing.Losses = existing.Losses + newPlayer.Losses;
             return;
         }
     }
     Users.Add(newPlayer);
 }
Exemplo n.º 2
0
        //The PlayHangMan method controls all the gameplay for hangman
        public static void PlayHangMan()
        {
            file = new FileIO();
            bool   ok = true;
            string userName;
            string word;
            //char [] word = new char[];
            List <string> userEntries      = new List <string>();
            int           incorrectGuesses = 7;
            string        enterLetter;
            int           wins        = 0;
            int           losses      = 0;
            bool          letterFound = false;
            Random        rnd         = new Random();
            int           randomWord;

            //welcome the user to the program
            Console.WriteLine("Welcome to Hangman! Get ready to take this L!\n");
            bool playing = true;
            bool inMenu;

            while (playing)
            {
                //Ask the user to enter their name
                userName = Validator.GetUserInputTwo("Please enter a memorable username\n");

                PlayerRecognition(userName);

                inMenu = true;
                while (inMenu)
                {
                    Console.WriteLine("What do you want to do?\n \n 1. Play a Game\n 2. See High Scores\n 3. New User\n 4. Quit\n");
                    switch (Console.ReadLine())
                    {
                    case ("1"):
                        #region
                        //use while loop to process user input
                        ok = true;
                        while (ok)
                        {
                            Console.WriteLine();
                            List <string> hangManWords = WordDifficultySelector();
                            Console.WriteLine();
                            List <string> guessedLettersList = new List <string>();
                            PopulateGuessList();
                            //create a random int variable that will be used to draw words from hangManWords list
                            randomWord = rnd.Next(0, hangManWords.Count);

                            //set the word that the user will guess
                            word = hangManWords[randomWord];

                            //generate dashes
                            userEntries = UserInput(word);

                            Console.WriteLine();

                            while (incorrectGuesses != 0 && ConvertStringList(userEntries) != word.ToLower())
                            {
                                ShowInput(userEntries);
                                Console.Write("Letters Entered: ");
                                DisplayListLetters(guessedLettersList);
                                Console.WriteLine();

                                //ask the user for an entry
                                enterLetter = Validator.LetterInputOnly("Enter a letter\n").ToLower();

                                //check to see if the letter has already been guessed
                                for (int x = 0; x < unguessedLetters.Count; x++)
                                {
                                    //check to see if the letter has already been guessed
                                    if (unguessedLetters[x] == enterLetter)
                                    {
                                        unguessedLetters.RemoveAt(x);
                                        guessedLettersList.Add(enterLetter);
                                        for (int i = 0; i < word.Length; i++)
                                        {
                                            if (enterLetter == word[i].ToString().ToLower())
                                            {
                                                //populate underscore from user entries with letter
                                                letterFound = true;
                                                userEntries.RemoveAt(i);
                                                userEntries.Insert(i, enterLetter);
                                            }
                                        }
                                        break;
                                    }

                                    if (x == unguessedLetters.Count - 1)
                                    {
                                        letterFound = true;
                                        //this letter has already been guessed, tell the user.
                                        Console.WriteLine("This letter has already been guessed. Please enter different letter. \n");

                                        incorrectGuesses--;
                                        CharacterArtCountdown(incorrectGuesses);
                                    }
                                }

                                //if the letter is not found, decrement the number of guesses
                                if (letterFound == false)
                                {
                                    Console.WriteLine("Letter not found. You suck.\n");
                                    incorrectGuesses--;
                                    CharacterArtCountdown(incorrectGuesses);
                                }
                                //reset the letter found bool for next loop interation
                                letterFound = false;
                            }
                            if (incorrectGuesses == 0)
                            {
                                InsultGenerator(word);
                                losses++;
                            }
                            else if (ConvertStringList(userEntries) == word.ToLower())
                            {
                                Console.WriteLine($"Congratulations! You won! Your word was {word}. Yay! You're awesome! =)");
                                wins++;
                            }
                            incorrectGuesses = 7;
                            word             = "";
                            UserPlayer CurrentPlayer = new UserPlayer(userName, wins, losses);
                            file.CheckUser(CurrentPlayer);
                            file.WriteUsers();

                            ok = Validator.GetContinue();
                            Console.WriteLine();
                        }
                        #endregion
                        break;

                    case ("2"):
                        Console.WriteLine();
                        DisplayHighScores();
                        Console.WriteLine();
                        break;

                    case ("3"):
                        inMenu = false;
                        break;

                    case ("4"):
                        Environment.Exit(0);
                        break;     //<----THIS SHOULDN'T BE NEEEDED!!!! RAAAAAAAAAAAAAAGE!!!!!!!!!\

                    case ("14"):
                        file.Words.Add(Validator.LetterInputOnly("What word do you want to add?"));
                        foreach (string testword in file.Words)
                        {
                            Console.WriteLine(testword);
                        }
                        file.WriteWords();
                        break;

                    default:
                        Console.WriteLine("You done f****d it all up. You broke it! It's ruined!");
                        break;
                    }
                }
            }
        }