Exemplo n.º 1
0
        public ParticipatorsContainer Prepare()
        {
            string[] objectsDirectory = Directory.GetFiles(@"C:\Users\Пользователь\source\repos\ComparerApp\ComparerApp.Library\Comparer_Pictures");

            if (objectsDirectory == null)
            {
                throw new DirectoryNotFoundException();
            }

            ParticipatorsContainer container = new ParticipatorsContainer();

            container.Capacity = objectsDirectory.Length;

            if ((Math.Log2((double)container.Capacity) % 1) != 0)
            {
                throw new ComparerValidityException("The amount of files in your Directory is not a number that represents number 2 raised to some power.\nCannot procede.");
            }

            foreach (string item in objectsDirectory)
            {
                ObjectParticipator participator = new ObjectParticipator();
                participator.FileDirectory = item;
                participator.FileName      = Path.GetFileNameWithoutExtension(item);
                container.ObjectsArray.Add(participator);
            }
            return(container);
        }
Exemplo n.º 2
0
 public void InitializePreviousState(Round round, ParticipatorsContainer container)
 {
     PreviousStageRound = (Round)round.Clone();
     PreviousPContainer = (ParticipatorsContainer)container.Clone();
     ExcludedObjects.Add(round.Pairs[0].First());
     ExcludedObjects.Add(round.Pairs[0].Last());
 }
Exemplo n.º 3
0
        public void RestorePreviousState(ref Round round, ref ParticipatorsContainer container)
        {
            if (round.RoundNumber != 1)
            {
                round.RoundNumber--;

                List<List<ObjectParticipator>> tmpObjects = new List<List<ObjectParticipator>>() {
                new List<ObjectParticipator>() { ChoosedObjects.Last(), ExcludedObjects.Last() }};
                tmpObjects.AddRange(round.Pairs);

                round.Pairs = tmpObjects;

                container.RemainderOfInitialCapacity++;
                container.NextRoundObjectsArray.Remove(container.NextRoundObjectsArray.Last());
            }
            else
            {
                round = (Round)PreviousStageRound.Clone();
                container = (ParticipatorsContainer)PreviousPContainer.Clone();
            }

            ChoosedObjects.Remove(ChoosedObjects.Last());
            ExcludedObjects.Remove(ExcludedObjects.Last());

        }
Exemplo n.º 4
0
 public bool CheckWinCondition(ParticipatorsContainer container)
 {
     if (container.RemainderOfInitialCapacity == 1)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 5
0
        public ObjectParticipator MakeDecision(string fileName, Round round, ParticipatorsContainer container)
        {
            ObjectParticipator choosenObject = round.Pairs[0].Find(i => i.FileName.ToLower() == fileName.ToLower());

            round.Pairs.Remove(round.Pairs[0]);
            round.RoundNumber++;
            container.NextRoundObjectsArray.Add(choosenObject);
            container.RemainderOfInitialCapacity--;

            return(choosenObject);
        }
Exemplo n.º 6
0
        public Round CreateNewRoundIfNeeded(Round round, ParticipatorsContainer container)
        {
            if (!round.Pairs.Any())
            {
                var preparator = new ComparerPreparator();
                container.NextRoundCapacity = container.NextRoundObjectsArray.Count();
                round = preparator.CreateRound(container.NextRoundCapacity, container.NextRoundObjectsArray);

                container.NextRoundObjectsArray = new List <ObjectParticipator>();
                return(round);
            }
            else
            {
                return(round);
            }
        }
Exemplo n.º 7
0
        private void DrawInitialState(IComparerPreparator preparator)
        {
            PContainer = new ParticipatorsContainer();
            try
            {
                PContainer = preparator.Prepare(); //есть контейнер файлов, в котором есть имя и полный путь каждого файла из папки
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            PContainer.RemainderOfInitialCapacity = PContainer.Capacity;

            PRound = preparator.CreateRound(PContainer.Capacity, PContainer.ObjectsArray);

            UpdatePicturesAndLabels();

            ThePreviousState = new PreviousState();
        }
Exemplo n.º 8
0
        public Round CreateRound(ParticipatorsContainer container)
        {
            ParticipatorsContainer tmpContainer = new ParticipatorsContainer();

            foreach (ObjectParticipator item in container.ObjectsArray)
            {
                tmpContainer.ObjectsArray.Add(item);
            }

            Round round         = new Round();
            int   numberOfPairs = container.Capacity / 2;
            int   tmpCapacity   = container.Capacity;

            for (int i = 0; i < numberOfPairs; i++)
            {
                List <ObjectParticipator> pair = new List <ObjectParticipator>();
                Random rnd = new Random();

                int firstObj = rnd.Next(tmpCapacity);
                tmpCapacity--;
                pair.Add(tmpContainer.ObjectsArray[firstObj]);
                tmpContainer.ObjectsArray.Remove(tmpContainer.ObjectsArray[firstObj]);

                int secondObj = rnd.Next(tmpCapacity);
                tmpCapacity--;
                pair.Add(tmpContainer.ObjectsArray[secondObj]);
                tmpContainer.ObjectsArray.Remove(tmpContainer.ObjectsArray[secondObj]);

                round.Pairs.Add(pair);
            }
            round.RoundNumber = 1;
            round.Stage       = $"1/{numberOfPairs}";
            if (round.Stage == "1/1")
            {
                round.Stage = "Final";
            }

            return(round);
        }
Exemplo n.º 9
0
        public void Run(IDecisionManager decisionManager, IComparerPreparator preparator)
        {
            bool isWorking = true;

            while (isWorking)
            {
                Console.WriteLine("Welcome to the comparer application.");
                ParticipatorsContainer container = new ParticipatorsContainer(); //мб чтобы убрать дублирование кода можно было бы через делегат для ex.Message,
                //но особого упрощения кода в этом не вижу
                try
                {
                    container = preparator.Prepare(); //есть контейнер файлов, в котором есть имя и полный путь каждого файла из папки
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
                container.RemainderOfInitialCapacity = container.Capacity;

                Round round = preparator.CreateRound(container.Capacity, container.ObjectsArray);

                bool roundActive = true;
                while (roundActive)
                {
                    round = decisionManager.CreateNewRoundIfNeeded(round, container);

                    Console.WriteLine();
                    Console.WriteLine($"Current Stage: {round.Stage}. Round: {round.RoundNumber} out of {round.Pairs.Count + container.NextRoundObjectsArray.Count}");
                    Console.WriteLine($"Choose the best between: {round.Pairs[0].First().FileName} and {round.Pairs[0].Last().FileName}");
                    Console.WriteLine($"To make your decision write down \"{round.Pairs[0].First().FileName}\" or \"{round.Pairs[0].Last().FileName}\"");

                    string choice = Console.ReadLine().ToLower();
                    if (choice == round.Pairs[0].First().FileName.ToLower() || choice == round.Pairs[0].Last().FileName.ToLower())
                    {
                        decisionManager.MakeDecision(choice, round, container);
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Unknown input. Please, try again");
                    }

                    if (decisionManager.CheckWinCondition(container))
                    {
                        Console.WriteLine();
                        Console.WriteLine($"The winner is {container.NextRoundObjectsArray[0].FileName}!");
                        Console.WriteLine();
                        roundActive = false;
                    }
                }

                bool continueNeeded = false;
                while (!continueNeeded)
                {
                    Console.WriteLine("Would you like to restart the comparer application?\nWrite down \"yes\" or \"no\"");
                    string answer = Console.ReadLine().ToLower();
                    if (answer == "yes")
                    {
                        Console.WriteLine();
                        continueNeeded = true;
                    }
                    else if (answer == "no")
                    {
                        continueNeeded = true;
                        isWorking      = false;
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.WriteLine("Unknown command");
                    }
                }
            }
        }