有没有办法让它更有效率

本文关键字:有效率 有没有 | 更新日期: 2023-09-27 17:51:18

所以我正在制作一款文本冒险游戏,以提高我的编程技能(只是一个初学者),我正在为它制作一个新的战斗系统,因为旧的系统真的很无聊。所以我遇到了一个石头剪刀布系统,但我想要的是像石头剪刀布这样的系统,让玩家有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(); }
        }
    }

有没有办法让它更有效率

首先创建一个Move enum,而不是使用字符串:

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)部分工作,因为您需要按值进行比较,而不是通过引用。