[英]How to get certain elements of an array using an inline condition 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.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 循環來做到這一點,但我想知道,為了更清晰的代碼和編寫更少的代碼,是否有辦法做到這一點內聯。 我見過人們使用 ? 操作符,但我不確定如何使用它,或者它是否適用於這里。
如果您能幫助我使用內聯運算符(如果確實存在),我將不勝感激。
謝謝,本
如果你需要它,我的代碼:
程序.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");
}
}
}
戰士.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);
}
}
}
}
}
派系.cs
namespace Game
{
enum Faction
{
GoodGuy, BadGuy
}
}
武器.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;
}
}
}
}
盔甲.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 方法。 我在那里看不到 switch 的原因,所以它會很簡單:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.