如何使用内联条件 C# 获取数组的某些元素?
How to get certain elements of an array using an inline condition C#?
我目前正在学习 C# 中的面向对象编程,因此,我只是在玩 classes 尝试创建游戏。
在我目前制作的游戏中,我有一个 Warrior class,它有一个方法 Attack(),它有两个重载,public void Attack(Warrior enemy)
以及 public void Attack(params Warrior[] enemies)
.
使用第一个重载没问题,我在 Program.cs 中实例化了两个 Warrior 对象,并使用 while 循环一直持续到其中一个 warrior 死亡,根据随机数选择每次攻击的对象发电机。
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.GoodGuy);
Warrior warrior2 = new Warrior("Alfie", 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 攻击所有其他活着的战士
案例 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, warrior2, warrior3, 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!", enemy.name, 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", this.name, attackDamage, enemies[i].name, enemies[i].Health);
if (enemies[i].IsAlive == false)
{
System.Console.WriteLine("{0} is dead, {1} is victorious!", enemies[i].name, 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;
}
}
}
}
非常好的学习应用,你做得很好!
您可以使用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)
我目前正在学习 C# 中的面向对象编程,因此,我只是在玩 classes 尝试创建游戏。
在我目前制作的游戏中,我有一个 Warrior class,它有一个方法 Attack(),它有两个重载,public void Attack(Warrior enemy)
以及 public void Attack(params Warrior[] enemies)
.
使用第一个重载没问题,我在 Program.cs 中实例化了两个 Warrior 对象,并使用 while 循环一直持续到其中一个 warrior 死亡,根据随机数选择每次攻击的对象发电机。
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.GoodGuy);
Warrior warrior2 = new Warrior("Alfie", 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 攻击所有其他活着的战士
案例 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, warrior2, warrior3, 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!", enemy.name, 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", this.name, attackDamage, enemies[i].name, enemies[i].Health);
if (enemies[i].IsAlive == false)
{
System.Console.WriteLine("{0} is dead, {1} is victorious!", enemies[i].name, 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;
}
}
}
}
非常好的学习应用,你做得很好!
您可以使用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)