Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.0 kB
3
Indexable
Never
using System;
using System.Collections.Generic;
using System.IO;

namespace MooGame
{
    interface IGame
    {
        void Play();
    }

    interface IGuessChecker
    {
        string Check(string goal, string guess);
    }

    interface IGoalGenerator
    {
        string Generate();
    }

    interface IPlayerData
    {
        string Name { get; }
        int NGames { get; }
        void Update(int guesses);
        double Average();
    }

    class MainClass : IGame
    {
        private readonly IGoalGenerator goalGenerator;
        private readonly IGuessChecker guessChecker;
        private readonly IPlayerDataStorage playerDataStorage;

        public MainClass(IGoalGenerator generator, IGuessChecker checker, IPlayerDataStorage storage)
        {
            goalGenerator = generator;
            guessChecker = checker;
            playerDataStorage = storage;
        }

        public void Play()
        {
            bool playOn = true;
            Console.WriteLine("Enter your username:\n");
            string name = Console.ReadLine();

            while (playOn)
            {
                string goal = goalGenerator.Generate();

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

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

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

                Console.WriteLine("Correct, it took " + nGuess + " guesses.\nContinue?");
                string answer = Console.ReadLine();
                if (answer != null && answer != "" && answer.Substring(0, 1) == "n")
                {
                    playOn = false;
                }
            }
        }
    }

    class GoalGenerator : IGoalGenerator
    {
        private readonly Random randomGenerator;

        public GoalGenerator()
        {
            randomGenerator = new Random();
        }

        public string Generate()
        {
            string goal = "";
            for (int i = 0; i < 4; i++)
            {
                int random = randomGenerator.Next(10);
                string randomDigit = "" + random;
                while (goal.Contains(randomDigit))
                {
                    random = randomGenerator.Next(10);
                    randomDigit = "" + random;
                }
                goal = goal + randomDigit;
            }
            return goal;
        }
    }

    class GuessChecker : IGuessChecker
    {
        public string Check(string goal, string guess)
        {
            int cows = 0, bulls = 0;
            guess += "    ";     // if player entered fewer than 4 characters
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (goal[i] == guess[j])
                    {
                        if (i == j)
                        {
                            bulls++;
                        }
                        else
                        {
                            cows++;
                        }
                    }
                }
            }
            return "BBBB".Substring(0, bulls) + "," + "CCCC".Substring(0, cows);
        }
    }

    class PlayerData : IPlayerData
    {
        public string Name { get; private set; }
        public int NGames { get; private set; }
        private int totalGuess;

        public PlayerData(string name, int guesses)
        {
            Name = name;
            NGames = 1;
            totalGuess = guesses;
        }

        public void Update(int guesses)
        {
            totalGuess += guesses;
            NGames++;
        }

        public double Average()
        {
            return (double)totalGuess / NGames;
        }

        public override bool Equals(object p)
        {
            return Name.Equals(((PlayerData)p).Name);
        }

        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
    }

    interface IPlayerDataStorage
    {
        void Write(string name, int guesses);
        void ShowTopList();
    }

    class PlayerDataStorage : IPlayerDataStorage
    {
        private readonly string filePath;

        public PlayerDataStorage(string path)
        {
            filePath = path;
        }

        public void Write(string name, int guesses)
        {
            StreamWriter output = new StreamWriter(filePath, append: true);
            output.WriteLine(name + "#&#" + guesses);
            output.Close();
        }

        public void ShowTopList()
        {
            StreamReader input = new StreamReader(filePath);
            List<IPlayerData> results = new List<IPlayerData>();
            string line;
            while ((line = input.ReadLine()) != null)
            {
                string[] nameAndScore = line.Split(new string[] { "#&#" }, StringSplitOptions.None);
                string name = nameAndScore[0];
                int guesses = Convert.ToInt32(nameAndScore[1]);
                IPlayerData pd = new PlayerData(name, guesses);
                int pos = results.IndexOf(pd);
                if (pos < 0)
                {
                    results.Add(pd);
                }
                else
                {
                    results[pos].Update(guesses);
                }
            }
            results.Sort((p1, p2) => p1.Average().CompareTo(p2.Average()));
            Console.WriteLine("Player   Games  Average");
            foreach (IPlayerData p in results)
            {
                Console.WriteLine(string.Format("{0,-9}{1,5:D}{2,9:F2}", p.Name, p.NGames, p.Average()));
            }
            input.Close();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string resultFilePath = "result.txt";
            IGoalGenerator generator = new GoalGenerator();
            IGuessChecker checker = new GuessChecker();
            IPlayerDataStorage storage = new PlayerDataStorage(resultFilePath);

            IGame game = new MainClass(generator, checker, storage);
            game.Play();
        }
    }
}