0
因此,我正在進行文字冒險,以提高我的編程技能(只是一個初學者),我正在爲它制定一個新的戰鬥系統,因爲舊的系統非常無聊。所以我遇到了一個搖滾紙剪刀系統,但我想要一些使用岩石剪刀的系統,有5個選項供玩家選擇,而敵人或怪物攻擊玩家。有沒有辦法讓這個更高效?
我用了很多if語句,其實並沒有太長的時間,但我想知道是否有更好的方法來做到這一點,以便我的代碼更有效率,而不是太大。
public static void ResultsOfMoves(string PlayerMove, string MonsterMove, Monster CurrentMonster, Weapon CurrentWeapon, Armor CurrentArmor, Player CurrentPlayer)
{
//Monster Responses to Player
if (PlayerMove == "dodge" && MonsterMove == "heavy"||MonsterMove == "stealth")
{
if (MonsterMove == "heavy") { MonsterHeavyAttack(); }
if (MonsterMove == "stealth") { MonsterStealthAttack(); }
}
else if (PlayerMove == "charge" && MonsterMove == "dodge"||MonsterMove == "stealth")
{
if (MonsterMove == "dodge") { MonsterDodge(); }
if (MonsterMove == "stealth") { MonsterStealthAttack(); }
}
else if (PlayerMove == "block" && MonsterMove == "charge" || MonsterMove == "dodge")
{
if (MonsterMove == "charge") { MonsterChargeAttack(); }
if (MonsterMove == "dodge") { MonsterDodge(); }
}
else if (PlayerMove == "heavy" && MonsterMove == "block" || MonsterMove == "charge")
{
if (MonsterMove == "block") { MonsterBlock(); }
if (MonsterMove == "charge") { MonsterChargeAttack(); }
}
else if (PlayerMove == "stealth" && MonsterMove == "heavy" || MonsterMove == "block")
{
if (MonsterMove == "heavy") { MonsterHeavyAttack(); }
if (MonsterMove == "block") { MonsterBlock(); }
}
//Players Responses To Monster
if (MonsterMove == "dodge" && PlayerMove == "heavy" || PlayerMove == "stealth")
{
if (PlayerMove == "heavy") { MonsterHeavyAttack(); }
if (PlayerMove == "stealth") { MonsterStealthAttack(); }
}
else if (MonsterMove == "charge" && PlayerMove == "dodge" || PlayerMove == "stealth")
{
if (PlayerMove == "dodge") { MonsterDodge(); }
if (PlayerMove == "stealth") { MonsterStealthAttack(); }
}
else if (MonsterMove == "block" && PlayerMove == "charge" || PlayerMove == "dodge")
{
if (PlayerMove == "charge") { MonsterChargeAttack(); }
if (PlayerMove == "dodge") { MonsterDodge(); }
}
else if (MonsterMove == "heavy" && PlayerMove == "block" || PlayerMove == "charge")
{
if (PlayerMove == "block") { MonsterBlock(); }
if (PlayerMove == "charge") { MonsterChargeAttack(); }
}
else if (MonsterMove == "stealth" && PlayerMove == "heavy" || PlayerMove == "block")
{
if (PlayerMove == "heavy") { MonsterHeavyAttack(); }
if (PlayerMove == "block") { MonsterBlock(); }
}
}
有很多方法可以提高效率。例如,如果你使用枚舉而不是字符串,你會更緊湊。你也可以創建函數指針表(C#中的委託),只需在字典中查找下一個動作並在一行代碼中執行即可。我會讓更多的人在C#中使用代碼來說明。 –
這可能會更好地放置在程序員交換中。 –
@Makoto:它可能是C++,它的'std :: string class'支持使用'=='進行比較。 OP可以請你添加一個語言標籤嗎? –