public OrderDiscountCalculator(
     IRuleStrategy ruleStrategy,
     IDiscountStrategy discountStrategy
     )
 {
     this.ruleStrategy     = ruleStrategy;
     this.discountStrategy = discountStrategy;
 }
Пример #2
0
        public void MakeNextGenerations()
        {
            IRuleStrategy ruleStrategy = StrategiesFactory.GetRuleStrategy(this);

            INeighborhoodStrategy neighborhood = StrategiesFactory.GetNeighborhoodStrategy(this);

            int stepCount = Params.StepCount;

            for (int i = 0; i < stepCount; i++)
            {
                MakeNextGeneration(neighborhood, ruleStrategy, Params.Generations);
            }
        }
Пример #3
0
        public bool RuleIsSatisfied()
        {
            RuleFactory   ruleFactory  = new RuleFactory();
            IRuleStrategy ruleStrategy = ruleFactory.GetRuleStrategy(_rule);

            Context ruleContext = new Context(ruleStrategy);

            if (ruleContext.Execute(_rule, _liveEventsThatFitIntoRule.ToList()))
            {
                ClearProcessedEvents();
                return(true);
            }

            return(false);
        }
Пример #4
0
        private void MakeNextGeneration(INeighborhoodStrategy neighborhood, IRuleStrategy ruleStrategy, int maxGenerations)
        {
            foreach (var cellIndex in Area.GetCellIndexes())
            {
                var neighborhoodCount = neighborhood.GetLifeNeighborhoodCount(cellIndex);

                if (ruleStrategy.IsDyingCellPolicy(cellIndex, neighborhoodCount, maxGenerations))
                {
                    cellIndex.SetDyingCell();
                    continue;
                }

                if (ruleStrategy.IsNewCellPolicy(cellIndex, neighborhoodCount))
                {
                    cellIndex.SetNewCell();
                    continue;
                }
            }

            Area.UpdateCells();
        }
Пример #5
0
 public IRuleStrategy Decorate(IRuleStrategy strategy)
 => new PlusStrategy(strategy);
Пример #6
0
 public SameStrategy(IRuleStrategy inner, bool wall)
 {
     this.inner = inner;
     this.wall  = wall;
 }
Пример #7
0
 public IRuleStrategy Decorate(IRuleStrategy strategy)
 => new SameStrategy(strategy, this.wall);
Пример #8
0
 public Context(IRuleStrategy strategy)
 {
     _strategy = strategy;
 }
Пример #9
0
 public IRuleStrategy Decorate(IRuleStrategy strategy)
 => new ElementalStrategy(strategy);
Пример #10
0
 public PlusStrategy(IRuleStrategy inner)
 {
     this.inner = inner;
 }
Пример #11
0
 public RuleStrategyBuilder Add(IRuleStrategyDecorator strategy)
 {
     this.strategy = strategy.Decorate(this.strategy);
     return(this);
 }
Пример #12
0
 public ElementalStrategy(IRuleStrategy inner)
 {
     this.inner = inner;
 }
Пример #13
0
        public static void TestCode(string inFile, string outFile)
        {
            PlayData playData = new PlayData(File.ReadAllLines(inFile));

            Console.WriteLine($"inFile                   : {Path.GetFileName(inFile)}");
            Console.WriteLine($"Command                  : {playData.Command}");
            Console.WriteLine($"Generations              : {playData.Params.Generations}");
            Console.WriteLine($"NeighborhoodStrategyName : {playData.Params.NeighborhoodStrategyName}");
            Console.WriteLine($"RulesStrategyName        : {playData.Params.RulesStrategyName}");
            Console.WriteLine($"Count                    : {playData.Params.Count}");
            Console.WriteLine($"LifeNewBirth             : {playData.Params.LifeNewBirth.Combine(", ")}");
            Console.WriteLine($"LifeSurvivals            : {playData.Params.LifeSurvivals.Combine(", ")}");

            switch (playData.Command)
            {
            case "CalcFieldSize":
            {
                File.WriteAllText(outFile, (playData.Area.Cols * playData.Area.Rows).ToString());
                break;
            }

            case "CalcLiveCells":
            {
                int count = playData.Area.GetCellIndexes(c => c.IsLifeCell).Count();
                File.WriteAllText(outFile, count.ToString());
                break;
            }

            case "CalcParamsCount":
            {
                File.WriteAllText(outFile, playData.Params.Count.ToString());
                break;
            }

            case "CalcSurvivingCells":
            {
                IRuleStrategy         ruleStrategy         = StrategiesFactory.GetRuleStrategy(playData);
                INeighborhoodStrategy neighborhoodStrategy = StrategiesFactory.GetNeighborhoodStrategy(playData);
                var maxGenerations = playData.Params.Generations;

                int count = playData.Area.GetCellIndexes().Count(c => { return(c.IsLifeCell && !ruleStrategy.IsDyingCellPolicy(c, neighborhoodStrategy.GetLifeNeighborhoodCount(c), maxGenerations)); });

                File.WriteAllText(outFile, count.ToString());
                break;
            }

            case "CalcNewCells":
            {
                IRuleStrategy         ruleStrategy         = StrategiesFactory.GetRuleStrategy(playData);
                INeighborhoodStrategy neighborhoodStrategy = StrategiesFactory.GetNeighborhoodStrategy(playData);

                int count = playData.Area.GetCellIndexes().Count(c => ruleStrategy.IsNewCellPolicy(c, neighborhoodStrategy.GetLifeNeighborhoodCount(c)));

                File.WriteAllText(outFile, count.ToString());
                break;
            }

            case "NextGeneration":
            {
                playData.MakeNextGenerations();

                playData.Area.WriteArea(outFile, playData.Params.UseGenerations);
                break;
            }

            default:
            {
                throw new ArgumentOutOfRangeException();
            }
            }
        }