所以我正在进行一次文本冒险,以提高我的编程技能(刚开始),我正在为它开发一个新的战斗系统,因为旧版本真的很无聊。所以我遇到了一个石头剪刀系统,但我想要一些使用岩石剪刀的系统,有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(); }
}
}
答案 0 :(得分: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]();
}
通过将Tuple<Moves, Moves>
替换为MoveCombination
结构,可以进一步改进此代码。 注意,使用struct
确保moveResolution.ContainsKey(moveCombination)
部分有效,因为您需要按值进行比较,而不是按参考进行比较。