Exemple #1
0
        static void StandardGame(dynamic password = null)
        {
            string category = null;

            if (password == null)
            {
                // is password is not given (not custom mode)
                // generate pass from Passwords class
                Password passObject = Passwords.GetOne();
                category = passObject.category;
                var index = new Random().Next(passObject.passwords.Count);
                password = passObject.passwords[index];
            }
            string      letterError = null;
            bool        isWin;
            bool        gameFinished;
            List <char> wrongLetters   = new List <char>();
            List <char> correctLetters = new List <char>();

            do
            {
                // print (refresh) game until it's not finished (pass not guessed or still chances available)
                UI.PrintGameMenu(category, password, correctLetters, wrongLetters, letterError);
                letterError = null;
                char newLetter = Char.ToUpper(Console.ReadKey().KeyChar);
                if (correctLetters.Contains(newLetter) || wrongLetters.Contains(newLetter))
                {
                    letterError = "You have already gave this letter!";
                }
                else if (!Passwords.VALID_CHARS.Contains(newLetter))
                {
                    letterError = "Invalid character!";
                }
                else
                {
                    if (password.Contains(newLetter))
                    {
                        correctLetters.Add(newLetter);
                    }
                    else
                    {
                        wrongLetters.Add(newLetter);
                    }
                }
                isWin        = correctLetters.Count == new HashSet <char>(password.Replace(" ", "")).Count;
                gameFinished = wrongLetters.Count >= 7 || isWin;
            } while (!gameFinished);
            UI.PrintGameMenu(category, password, correctLetters, wrongLetters, letterError);
            EndGame(isWin);
        }