Exemplo n.º 1
0
        //юниты атакут друг друга по-очереди: сначала первый второго, затем второй - первого
        private IEnumerable <ActionResult> Attack(IUnit first, IUnit second, IArmy firstArmy, IArmy secondArmy)
        {
            List <ActionResult> results = new List <ActionResult>();

            IUnit resultUnit = second.TakeDamage(first.Power);

            if (resultUnit != second)
            {
                secondArmy.ReplaceUnit(resultUnit, 0);
            }
            if (first.Power > 0)
            {
                results.Add(new ActionResult(first, second, ActionResultType.Attack));
            }

            second = secondArmy.GetUnit(0);
            if (second.Health > 0)
            {
                resultUnit = first.TakeDamage(second.Power);
                if (resultUnit != first)
                {
                    firstArmy.ReplaceUnit(resultUnit, 0);
                }

                if (second.Power > 0)
                {
                    results.Add(new ActionResult(second, first, ActionResultType.Attack));
                }
            }

            return(results);
        }
Exemplo n.º 2
0
        public ActionResult DoSpecialAction(int position, IArmy myArmy, IArmy enemyArmy, Random random = null)
        {
            if (random == null)
            {
                random = new Random();
            }

            double probability = random.NextDouble();

            //для выстрела сгенерированная вероятность должна принадлежать промежутку [0, ArcheryProbability)
            if (probability >= ArcheryProbability)
            {
                return(ActionResult.Empty);
            }

            int range = Range - (int)position / myArmy.Width;
            int start = position % myArmy.Width;

            if (range <= 0)
            {
                return(ActionResult.Empty);
            }

            List <IUnit> availibleForAttack = GetAvailibleForAttackUnits(start, range, enemyArmy);

            if (availibleForAttack.Count == 0)
            {
                return(ActionResult.Empty);
            }

            int unitForAttack = random.Next(0, availibleForAttack.Count);

            IUnit resultUnit = availibleForAttack[unitForAttack].TakeDamage(ArcheryPower);

            if (resultUnit != availibleForAttack[unitForAttack])
            {
                enemyArmy.ReplaceUnit(resultUnit, availibleForAttack[unitForAttack]);
            }

            return(new ActionResult(this, availibleForAttack[unitForAttack], ActionResultType.Shot));
        }
Exemplo n.º 3
0
        public ActionResult DoSpecialAction(int position, IArmy myArmy, IArmy enemyArmy, Random random = null)
        {
            if (random == null)
            {
                random = new Random();
            }

            double probability = random.NextDouble();

            //для улучшения сгенерированная вероятность должна принадлежать промежутку [0, ImprovementHeavyUnitProbability)
            if (probability >= ImprovementHeavyUnitProbability)
            {
                return(ActionResult.Empty);
            }

            int[] availibleForImprovement = GetAvailibleForImprovement(position, myArmy).ToArray();

            if (availibleForImprovement.Length == 0)
            {
                return(ActionResult.Empty);
            }

            int index  = availibleForImprovement[random.Next(0, availibleForImprovement.Length)];
            int action = random.Next(0, 4);

            IHeavyInfantry unit = myArmy.GetUnit(index) as IHeavyInfantry;

            switch (action)
            {
            case 0:
                if (unit.ContainsImprovement(typeof(HelmetDecorator)))
                {
                    return(ActionResult.Empty);
                }
                unit = new HelmetDecorator(unit);
                myArmy.ReplaceUnit(unit, index);
                break;

            case 1:
                if (unit.ContainsImprovement(typeof(HorseDecorator)))
                {
                    return(ActionResult.Empty);
                }
                unit = new HorseDecorator(unit);
                myArmy.ReplaceUnit(unit, index);
                break;

            case 2:
                if (unit.ContainsImprovement(typeof(ShieldDecorator)))
                {
                    return(ActionResult.Empty);
                }
                unit = new ShieldDecorator(unit);
                myArmy.ReplaceUnit(unit, index);
                break;

            case 3:
                if (unit.ContainsImprovement(typeof(SwordDecorator)))
                {
                    return(ActionResult.Empty);
                }
                unit = new SwordDecorator(unit);
                myArmy.ReplaceUnit(unit, index);
                break;

            default:
                return(ActionResult.Empty);
            }
            return(new ActionResult(this, unit, ActionResultType.Improvement));
        }