Example #1
0
        public CuttedPizza Cut(Pizza pizza)
        {
            var cuttedPizza = new CuttedPizza(pizza);

            for (var col = 0; col < pizza.Width; col++)
            {
                for (var row = 0; row < pizza.Height; row++)
                {
                    if (!PizzaValidator.PieceValidate(row, col, cuttedPizza))
                    {
                        continue;
                    }
                    var slices = GetAllSugestSlices(pizza, row, col);
                    var slice  = ChooseSlice(slices, cuttedPizza);
                    if (slice != null)
                    {
                        cuttedPizza.AddSlice(slice);
                        row = slice.Coords.RowEnd;
                    }
                }
                var progress = (float)col / pizza.Width * 100;
                Console.WriteLine($"Pregress {progress}%");
            }
            return(cuttedPizza);
        }
Example #2
0
        private Slice ChooseSlice(IEnumerable <Slice> slices, CuttedPizza cuttedPizza)
        {
            slices = slices.Where(s => _sliceValidator.Validate(s, cuttedPizza));

            if (!slices.Any())
            {
                return(null);
            }
            var slice = slices
                        .Aggregate((s1, s2) =>
            {
                var s1DiffMushroom = cuttedPizza.CountMushrooms - s1.CountMushrooms;
                var s1DiffTomatoe  = cuttedPizza.CountTomatoes - s1.CountTomatoes;
                if (s1DiffMushroom == 0 || s1DiffTomatoe == 0)
                {
                    return(s2);
                }
                var s2DiffMushroom = cuttedPizza.CountMushrooms - s2.CountMushrooms;
                var s2DiffTomatoe  = cuttedPizza.CountTomatoes - s2.CountTomatoes;
                if (s2DiffMushroom == 0 || s2DiffTomatoe == 0)
                {
                    return(s1);
                }

                var s1Diff = s1DiffMushroom > s1DiffTomatoe
                        ? s1DiffMushroom / s1DiffTomatoe
                        : s1DiffTomatoe / s1DiffMushroom;
                var s2Diff = s2DiffMushroom > s2DiffTomatoe
                        ? s2DiffMushroom / s2DiffTomatoe
                        : s2DiffTomatoe / s2DiffMushroom;
                if (s1Diff > s2Diff)
                {
                    return(s2);
                }
                if (s1Diff == s2Diff)
                {
                    return(s1.CountPieces > s2.CountPieces ? s2 : s1);
                }
                return(s1);
            });

            return(slice);
        }