如何使用内联条件C#获取数组的某些元素?

我目前正在学习C#中的面向对象编程,因此,我只是在玩尝试创建游戏的类。

在我目前正在制作的游戏中,我有一个Warrior类,该类具有一个Attack()方法,该方法具有两个重载public void Attack(Warrior enemy)public void Attack(params Warrior[] enemies)

使用第一个重载很好,我在Program.cs中实例化了两个Warrior对象,并使用while循环,直到一个战士死了,然后根据随机数生成器选择每次攻击的人。

Random random = new Random();

Warrior warrior1 = new Warrior("Ben",Faction.GoodGuy);
Warrior warrior2 = new Warrior("Alfie",Faction.BadGuy);

while (warrior1.IsAlive && warrior2.IsAlive)
{
    if (random.Next(9) < 5)
    {
        warrior1.Attack(warrior2);
    }
    else
    {
        warrior2.Attack(warrior1);
    }
}

但是,当我尝试使用第二重载且敌人众多时,就会出现问题。

我创建了四个敌人,并开始了while循环,如下所示。

Random random = new Random();
Warrior warrior1 = new Warrior("Ben",Faction.BadGuy);
Warrior warrior3 = new Warrior("Joel",Faction.BadGuy);
Warrior warrior4 = new Warrior("Ruben",Faction.GoodGuy);

while(warrior1.IsAlive || warrior2.IsAlive || warrior3.IsAlive || warrior4.IsAlive)
{
    Warrior[] warriors = new Warrior[] { warrior1,warrior2,warrior3,warrior4 };

    switch (random.Next(4))
    {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        default:
            break;
    }
}

在每种情况下,我都希望一个战士攻击当前还活着的其他每个战士。

所以在案例0:战士1攻击所有其他活着的战士

第一种情况:战士2攻击所有其他战士

案例2:战士3攻击所有其他战士

案例3:战士4攻击所有其他战士

我可以很容易地通过一个foreach循环来做到这一点,但是我想知道,是否有一种方法可以内联地进行,以简化代码并减少编写代码。我见过有人用吗?运算符,但是我不确定如何使用它,甚至在这里也不能使用。

如果您可以帮助内联运算符(如果确实有)的话,我将不胜感激。

谢谢,本

\ /我的代码(如果需要)\ /

Program.Cs

using System;

namespace Game
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();

            Warrior warrior1 = new Warrior("Ben",Faction.GoodGuy);
            Warrior warrior2 = new Warrior("Alfie",Faction.BadGuy);

            /*while (warrior1.IsAlive && warrior2.IsAlive)
            {
                if (random.Next(9) < 5)
                {
                    warrior1.Attack(warrior2);
                }
                else
                {
                    warrior2.Attack(warrior1);
                }
            }*/

            Warrior warrior3 = new Warrior("Joel",Faction.BadGuy);
            Warrior warrior4 = new Warrior("Ruben",Faction.GoodGuy);

            while(warrior1.IsAlive || warrior2.IsAlive || warrior3.IsAlive || warrior4.IsAlive)
            {
                Warrior[] warriors = new Warrior[] { warrior1,warrior4 };

                switch (random.Next(4))
                {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
            }

            Console.WriteLine("End of Code");
        }
    }
}

Warrior.cs

namespace Game
{
    class Warrior
    {
        private const double GOOD_GUY_HEALTH = 100;
        private const double BAD_GUY_HEALTH = 100;

        public double Health { get; set; }
        public bool IsAlive { get; set; } = true;

        private string name;
        private Faction faction;
        private Weapon weapon;
        private Armour armour;

        public Warrior(string name,Faction faction)
        {
            this.name = name;
            this.faction = faction;

            switch (faction)
            {
                case Faction.GoodGuy:
                    Health = GOOD_GUY_HEALTH;
                    weapon = new Weapon(faction);
                    armour = new Armour(faction);
                    break;
                case Faction.BadGuy:
                    Health = BAD_GUY_HEALTH;
                    weapon = new Weapon(faction);
                    armour = new Armour(faction);
                    break;
                default:
                    break;
            }
        }

        public void Attack(Warrior enemy)
        {
            System.Random random = new System.Random();
            double attackDamage = random.Next((int)System.Math.Ceiling(weapon.Damage)) - random.Next((int)System.Math.Ceiling(enemy.armour.ArmourPoints));

            if (attackDamage < 0)
            {
                attackDamage = 0;
            }

            enemy.Health -= attackDamage;

            if (enemy.Health <= 0)
            {
                enemy.Health = 0;
                enemy.IsAlive = false;
            }

            System.Console.WriteLine("{0} deals {1} damage. {2} is on {3} health",this.name,attackDamage,enemy.name,enemy.Health);

            if (enemy.IsAlive == false)
            {
                System.Console.WriteLine("{0} is dead,{1} is victorious!",this.name);
            }
        }

        public void Attack(params Warrior[] enemies)
        {
            System.Random random = new System.Random();
            double damageLeft = weapon.Damage;

            for (int i = 0; i < enemies.Length; i++)
            {
                if (damageLeft <= 0)
                {
                    return;
                }

                double damageBeforeShield = random.NextDouble() * System.Math.Ceiling(damageLeft / enemies.Length);
                double attackDamage = damageBeforeShield - (random.NextDouble() * enemies[i].armour.ArmourPoints);

                damageLeft -= attackDamage;

                if (enemies[i].Health < 0)
                {
                    enemies[i].Health = 0;
                    enemies[i].IsAlive = false;
                }

                System.Console.WriteLine("{0} deals {1} damage. {2} is on {3} helath",enemies[i].name,enemies[i].Health);

                if (enemies[i].IsAlive == false)
                {
                    System.Console.WriteLine("{0} is dead,this.name);
                }




            }
        }

    }
}

Faction.cs

namespace Game
{
    enum Faction
    {
        GoodGuy,BadGuy
    }
}

Weapon.cs

namespace Game
{
    class Weapon
    {
        private double damage;

        public double Damage
        {
            get
            {
                return damage;
            }
        }

        public Weapon(Faction faction)
        {
            switch (faction)
            {
                case Faction.GoodGuy:
                    damage = 20;
                    break;
                case Faction.BadGuy:
                    damage = 20;
                    break;
                default:
                    break;
            }
        }

    }
}

Armour.cs

namespace Game
{
    class Armour
    {
        private readonly double armourPoints;

        public double ArmourPoints
        {
            get
            {
                return armourPoints;
            }
        }

        public Armour(Faction faction)
        {
            switch (faction)
            {
                case Faction.GoodGuy:
                    armourPoints = 20;
                    break;
                case Faction.BadGuy:
                    armourPoints = 20;
                    break;
                default:
                    break;
            }
        }
    }
}
liulity5201314 回答:如何使用内联条件C#获取数组的某些元素?

非常好的学习目的应用​​程序,您做得很好!

您可以使用List的RemoveAt方法。我看不到在那儿切换的理由,所以这很简单:

while(warrior1.IsAlive || warrior2.IsAlive || warrior3.IsAlive || warrior4.IsAlive)
{
    var warriors = new List<Warrior> { warrior1,warrior2,warrior3,warrior4 };
    ProcessAttacks(warriors,random.Next(4));
}

这是ProcessAttacks函数:

private void ProcessAttacks(List<Warrior> warriors,int currentWarrior)
{
    var warrior = warriors[currentWarrior];
    warriors.RemoveAt(currentWarrior);
    warrior.Attack(warriors);
}

还需要将您的签名从public void Attack(params Warrior[] enemies)更改为public void Attack(List<Warrior> enemies)

本文链接:https://www.f2er.com/2860425.html

大家都在问