Exemplo n.º 1
0
        public IEnumerable <ActionResult> Fight(IArmy firstArmy, IArmy secondArmy, Random random = null)
        {
            firstArmy.Width  = 1;
            secondArmy.Width = 1;

            random = random ?? new Random();

            IUnit first  = firstArmy.GetUnit(0);
            IUnit second = secondArmy.GetUnit(0);

            List <ActionResult> results = new List <ActionResult>();

            int turnRound = random.Next(0, 2); //чей ход должен быть следующим: 0 - 1-го игрока, 1 - 2-го игрока

            if (turnRound == 0)
            {
                results.AddRange(Attack(first, second, firstArmy, secondArmy));
            }
            else if (turnRound == 1)
            {
                results.AddRange(Attack(second, first, secondArmy, firstArmy));
            }

            results.AddRange(DoSpecialActions(firstArmy, secondArmy, random));

            return(results);
        }
Exemplo n.º 2
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.º 3
0
        public IEnumerable <ActionResult> Fight(IArmy firstArmy, IArmy secondArmy, Random random = null)
        {
            if (random == null)
            {
                random = new Random();
            }

            firstArmy.Width  = firstArmy.Count;
            secondArmy.Width = secondArmy.Count;

            List <ActionResult> results = new List <ActionResult>();

            int count = Math.Min(firstArmy.Count, secondArmy.Count);

            for (int i = 0; i < count; i++)
            {
                IUnit first  = firstArmy.GetUnit(i);
                IUnit second = secondArmy.GetUnit(i);

                int turnRound = random.Next(0, 2); //чей ход должен быть следующим: 0 - 1-го игрока, 1 - 2-го игрока

                if (turnRound == 0)
                {
                    results.AddRange(Attack(i, first, second, firstArmy, secondArmy));
                }
                else if (turnRound == 1)
                {
                    results.AddRange(Attack(i, second, first, secondArmy, firstArmy));
                }
            }

            results.AddRange(DoSpecialActions(count, firstArmy, secondArmy, random));

            return(results);
        }
Exemplo n.º 4
0
        List <ICanBeHealed> GetAvailibleForHealing(int position, IArmy myArmy)
        {
            List <ICanBeHealed> availibleForHealing = new List <ICanBeHealed>(5);

            availibleForHealing.Add(this);

            ICanBeHealed unit;

            if (position % myArmy.Width > 0 && position > 0) //есть ли юниты вверху шеренги
            {
                unit = myArmy.GetUnit(position - 1) as ICanBeHealed;
                if (unit != null)
                {
                    availibleForHealing.Add(unit);
                }
            }

            if ((position + 1) % myArmy.Width > 0 && position < myArmy.Count - 1) //есть ли юниты внизу шеренги
            {
                unit = myArmy.GetUnit(position + 1) as ICanBeHealed;
                if (unit != null)
                {
                    availibleForHealing.Add(unit);
                }
            }

            if (position + myArmy.Width < myArmy.Count) //есть ли юниты по бокам
            {
                unit = myArmy.GetUnit(position + myArmy.Width) as ICanBeHealed;
                if (unit != null)
                {
                    availibleForHealing.Add(unit);
                }
            }

            if (position - myArmy.Width >= 0)
            {
                unit = myArmy.GetUnit(position - myArmy.Width) as ICanBeHealed;
                if (unit != null)
                {
                    availibleForHealing.Add(unit);
                }
            }

            return(availibleForHealing);
        }
Exemplo n.º 5
0
        //специальные действия выполняют все юниты, начиная с первого (нулевой в дуэли сражается)
        private IEnumerable <ActionResult> DoSpecialActionsForArmy(IArmy myArmy, IArmy enemyArmy, Random random)
        {
            List <ActionResult> results = new List <ActionResult>();

            for (int i = 1; i < myArmy.Count; i++)
            {
                if (myArmy.GetUnit(i).IsAlive())
                {
                    ISpecialAbility unit = myArmy.GetUnit(i) as ISpecialAbility;
                    if (unit != null)
                    {
                        results.Add(unit.DoSpecialAction(i, myArmy, enemyArmy, random));
                    }
                }
            }
            return(results);
        }
Exemplo n.º 6
0
        List <int> GetAvailibleForImprovement(int position, IArmy myArmy)
        {
            List <int> availibleForImprovement = new List <int>(4);

            IHeavyInfantry unit;

            if (position % myArmy.Width > 0 && position > 0) //есть ли юниты вверху шеренги
            {
                unit = myArmy.GetUnit(position - 1) as IHeavyInfantry;
                if (unit != null)
                {
                    availibleForImprovement.Add(position - 1);
                }
            }

            if ((position + 1) % myArmy.Width > 0 && position < myArmy.Count - 1) //есть ли юниты внизу шеренги
            {
                unit = myArmy.GetUnit(position + 1) as IHeavyInfantry;
                if (unit != null)
                {
                    availibleForImprovement.Add(position + 1);
                }
            }

            if (position + myArmy.Width < myArmy.Count) //есть ли юниты по бокам
            {
                unit = myArmy.GetUnit(position + myArmy.Width) as IHeavyInfantry;
                if (unit != null)
                {
                    availibleForImprovement.Add(position + myArmy.Width);
                }
            }

            if (position - myArmy.Width >= 0)
            {
                unit = myArmy.GetUnit(position - myArmy.Width) as IHeavyInfantry;
                if (unit != null)
                {
                    availibleForImprovement.Add(position - myArmy.Width);
                }
            }

            return(availibleForImprovement);
        }
Exemplo n.º 7
0
 private void DisplayArmy(IArmy army)
 {
     Console.ForegroundColor = ConsoleColor.White;
     Console.WriteLine("\n " + army.Name);
     Console.ResetColor();
     OutputHeaderInDisplayUnitTable();
     for (int i = 0; i < army.Count; i++)
     {
         OutputUnit(army.GetUnit(i));
     }
 }
Exemplo n.º 8
0
        List <IUnit> GetAvailibleForAttackUnits(int start, int range, IArmy enemyArmy)
        {
            List <IUnit> availibleForAttack = new List <IUnit>(Range);

            //обойдем текущий ряд и выше
            int currentRange = range;

            for (int i = start; i >= 0 && currentRange > 0; i--)
            {
                for (int j = 0; j < currentRange; j++)
                {
                    int pos = i + j * enemyArmy.Width;
                    if (pos >= enemyArmy.Count)
                    {
                        break;
                    }
                    availibleForAttack.Add(enemyArmy.GetUnit(pos));
                }

                currentRange--;
            }

            //обойдем нижние ряды
            currentRange = range - 1;
            for (int i = start + 1; i < enemyArmy.Width && currentRange > 0; i++)
            {
                for (int j = 0; j < currentRange; j++)
                {
                    int pos = i + j * enemyArmy.Width;
                    if (pos >= enemyArmy.Count)
                    {
                        break;
                    }
                    availibleForAttack.Add(enemyArmy.GetUnit(pos));
                }

                currentRange--;
            }

            return(availibleForAttack);
        }
Exemplo n.º 9
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));
        }
Exemplo n.º 10
0
 public virtual IUnit GetTarget(IArmy fromArmy)
 {
     return(fromArmy.GetUnit());
 }