Untitled

 avatar
unknown
plain_text
2 years ago
5.2 kB
4
Indexable
namespace LaborationCC.Classes
{
    internal class MainClass : IGame
    {
        private readonly IGoalGeneratorFactory goalGeneratorFactory;
        private readonly IGuessCheckerFactory guessCheckerFactory;
        private readonly IPlayerDataStorage playerDataStorage;
        private readonly IIO io;
        private readonly Action userInputHandler;

        public MainClass(IGoalGeneratorFactory generatorFactory, IGuessCheckerFactory checkerFactory, IPlayerDataStorage storage, IIO io)
        {
            goalGeneratorFactory = generatorFactory;
            guessCheckerFactory = checkerFactory;
            playerDataStorage = storage;
            this.io = io;

            userInputHandler = HandleUserInput;
        }

        public void Play()
        {
            io.WriteLine("Welcome to the Number Guessing Game!");
            io.WriteLine("=====================================\n");

            bool playOn = true;
            while (playOn)
            {
                HandleUserInput();

                io.WriteLine("Enter your username:\n");
                string name = io.ReadLine();

                IGoalGenerator goalGenerator = goalGeneratorFactory.CreateGoalGenerator();
                IGuessChecker guessChecker = guessCheckerFactory.CreateGuessChecker();
                string goal = goalGenerator.Generate();

                io.WriteLine("New game:\n");
                // Comment out or remove the next line to play real games!
                io.WriteLine("For practice, the number is: " + goal + "\n");
                string guess = io.ReadLine();

                int nGuess = 1;
                string bbcc = guessChecker.Check(goal, guess);
                io.WriteLine(bbcc + "\n");
                while (bbcc != "BBBB,")
                {
                    nGuess++;
                    guess = io.ReadLine();
                    io.WriteLine(guess + "\n");
                    bbcc = guessChecker.Check(goal, guess);
                    io.WriteLine(bbcc + "\n");
                }

                playerDataStorage.Write(name, nGuess);
                playerDataStorage.ShowTopList();


                io.WriteLine("Correct, it took " + nGuess + " guesses.\nContinue?\n \"n\" for no or just press any key to continue");
                string answer = io.ReadLine();
                if (answer != null && answer != "" && answer.Substring(0, 1) == "n")
                {
                    playOn = false;
                }
            }
        }

        private void HandleUserInput()
        {
            while (true)
            {
                io.WriteLine("Select an option:");
                io.WriteLine("1. Play");
                io.WriteLine("2. Clear Player Data");
                io.WriteLine("3. Exit");

                string choice = io.ReadLine();

                switch (choice)
                {
                    case "1":
                        io.WriteLine("Enter your username:\n");
                        string name = io.ReadLine();

                        IGoalGenerator goalGenerator = goalGeneratorFactory.CreateGoalGenerator();
                        IGuessChecker guessChecker = guessCheckerFactory.CreateGuessChecker();
                        string goal = goalGenerator.Generate();

                        io.WriteLine("New game:\n");
                        // Comment out or remove the next line to play real games!
                        io.WriteLine("For practice, the number is: " + goal + "\n");
                        string guess = io.ReadLine();

                        int nGuess = 1;
                        string bbcc = guessChecker.Check(goal, guess);
                        io.WriteLine(bbcc + "\n");
                        while (bbcc != "BBBB,")
                        {
                            nGuess++;
                            guess = io.ReadLine();
                            io.WriteLine(guess + "\n");
                            bbcc = guessChecker.Check(goal, guess);
                            io.WriteLine(bbcc + "\n");
                        }

                        playerDataStorage.Write(name, nGuess);
                        playerDataStorage.ShowTopList();


                        io.WriteLine("Correct, it took " + nGuess + " guesses.\nContinue?\n \"n\" for no or just press any key to continue");
                        string answer = io.ReadLine();
                        if (answer != null && answer != "" && answer.Substring(0, 1) == "n")
                        {
                            return;
                        }
                        break;
                    case "2":
                        playerDataStorage.ClearData();
                        io.WriteLine("Player data cleared.");
                        break;
                    case "3":
                        io.WriteLine("Exiting the game...");
                        return;
                    default:
                        io.WriteLine("Invalid option. Please try again.");
                        break;
                }
            }
        }
    }
}
Editor is loading...