2015-05-31 88 views
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(); } 
     } 

    } 
+1

有很多方法可以提高效率。例如,如果你使用枚舉而不是字符串,你會更緊湊。你也可以創建函數指針表(C#中的委託),只需在字典中查找下一個動作並在一行代碼中執行即可。我會讓更多的人在C#中使用代碼來說明。 –

+0

這可能會更好地放置在程序員交換中。 –

+0

@Makoto:它可能是C++,它的'std :: string class'支持使用'=='進行比較。 OP可以請你添加一個語言標籤嗎? –

回答

1

首先創建一個Move枚舉,而不是使用字符串:

public enum Moves 
{ 
    Charge, 
    Dodge, 
    Heavy, 
    Steath, 
    Block 
} 

接下來,使用Dictionary來確定移動:

var moveResolution = new Dictionary<Tuple<Moves, Moves>, Action> 
{ 
    { new Tuple<Moves, Moves>(Moves.Dodge, Moves.Heavy), MonsterHeavyAttack }, 
    { new Tuple<Moves, Moves>(Moves.Dodge, Moves.Steath), MonsterStealthAttack }, 
    { new Tuple<Moves, Moves>(Moves.Charge, Moves.Dodge), MonsterDodge }, 
    ... 
}; 

然後才能確定合適的舉動,只是做:

var moveCombination = new Tuple<Moves, Moves>(playerMove, monsterMove); 
if (moveResolution.ContainsKey(moveCombination)) 
{ 
    moveResolution[moveCombination](); 
} 

然後可以通過用MoveCombination結構替換惰性Tuple<Moves, Moves>來進一步改進此代碼。 注意,使用struct以確保moveResolution.ContainsKey(moveCombination)部分工作,因爲您需要按值比較,而不是通過引用。

相關問題