###疑問に思うこと
エクセルでモンスターの行動パターンを作ろうと思っています。
行動条件のIdをエクセル内で決めて、プログラム内でそのIdで行動するのかしないのかを判定するメソッドを書いているのですが、Idが多いためSwitch文が長くなってしまいます。
メソッドの行数は長くても30行程度が好ましいと聞いたことがあるのですが、やはり何か工夫をして短くするべきなのでしょうか?
ご指摘お願いします。
public class EN_AI { public string Id { get; private set; } //AIの名前. public List<EN_AI_Method> AI_Method { get; private set; } //AIの条件パターンのList. public EN_AI(string _id, List<EN_AI_Method> _ai_Method) { Id = _id; AI_Method = _ai_Method; } private bool isUsedOnlyOnce = false; private int conditionsValue = 0; public ICharaStatus_FVRPG MyStatus { private get; set; } public List<ICharaStatus_FVRPG> PlStatusList { private get; set; } public List<ICharaStatus_FVRPG> EnStatusList { private get; set; } private bool Check_Conditions(string conditionsId) { bool result = false; switch (conditionsId) { case "": result = true; break; //指定なしならtrue. case "UNC": result = true; break; //UNC==無条件でtrue. case "OnlyOnce": if (isUsedOnlyOnce == false) { isUsedOnlyOnce = true; result = true; } break; //一度きりの行動. case "Val_0": if (conditionsValue == 0) { result = true; } break; case "Val_1": if (conditionsValue == 1) { result = true; } break; case "Val_2": if (conditionsValue == 2) { result = true; } break; case "Val_3": if (conditionsValue == 3) { result = true; } break; case "Val_4": if (conditionsValue == 4) { result = true; } break; case "Val_5": if (conditionsValue == 5) { result = true; } break; case "Val_6": if (conditionsValue == 6) { result = true; } break; case "Val_7": if (conditionsValue == 7) { result = true; } break; case "Val_8": if (conditionsValue == 8) { result = true; } break; case "Rnd_1": if (MyRandom.Judgment_SettingParcent(1.0f)) { result = true; } break; //1%の確立でtrue. case "Rnd_3": if (MyRandom.Judgment_SettingParcent(3.0f)) { result = true; } break; case "Rnd_5": if (MyRandom.Judgment_SettingParcent(5.0f)) { result = true; } break; case "Rnd_10": if (MyRandom.Judgment_SettingParcent(10.0f)) { result = true; } break; case "Rnd_20": if (MyRandom.Judgment_SettingParcent(20.0f)) { result = true; } break; case "Rnd_30": if (MyRandom.Judgment_SettingParcent(30.0f)) { result = true; } break; case "Rnd_50": if (MyRandom.Judgment_SettingParcent(50.0f)) { result = true; } break; case "Rnd_80": if (MyRandom.Judgment_SettingParcent(80.0f)) { result = true; } break; case "Rnd_99": if (MyRandom.Judgment_SettingParcent(99.0f)) { result = true; } break; case "HP_Max": if (MyStatus.Hp_Now == MyStatus.Hp) { result = true; } break; case "HP99_&L": if(Calc_HpParcent(MyStatus) <= 0.99f) { result = true; } break; //HP99%以下ならtrue. case "HP95_O": if (Calc_HpParcent(MyStatus) > 0.95f) { result = true; } break; //HP95%より上ならtrue. case "HP95_&L": if (Calc_HpParcent(MyStatus) <= 0.95f) { result = true; } break; case "HP90_O": if (Calc_HpParcent(MyStatus) > 0.9f) { result = true; } break; case "HP90_&L": if (Calc_HpParcent(MyStatus) <= 0.90f) { result = true; } break; case "HP80_O": if (Calc_HpParcent(MyStatus) > 0.8f) { result = true; } break; case "HP80_&L": if (Calc_HpParcent(MyStatus) <= 0.8f) { result = true; } break; case "HP70_O": if (Calc_HpParcent(MyStatus) > 0.7f) { result = true; } break; case "HP70_&L": if (Calc_HpParcent(MyStatus) <= 0.7f) { result = true; } break; case "HP60_O": if (Calc_HpParcent(MyStatus) > 0.6f) { result = true; } break; case "HP60_&L": if (Calc_HpParcent(MyStatus) <= 0.6f) { result = true; } break; case "HP50_O": if (Calc_HpParcent(MyStatus) > 0.5f) { result = true; } break; case "HP50_&L": if (Calc_HpParcent(MyStatus) <= 0.5f) { result = true; } break; case "HP40_O": if (Calc_HpParcent(MyStatus) > 0.4f) { result = true; } break; case "HP40_&L": if (Calc_HpParcent(MyStatus) <= 0.4f) { result = true; } break; case "HP30_O": if (Calc_HpParcent(MyStatus) > 0.3f) { result = true; } break; case "HP30_&L": if (Calc_HpParcent(MyStatus) <= 0.3f) { result = true; } break; case "HP20_O": if(Calc_HpParcent(MyStatus) > 0.2f) { result = true; } break; //以下略。味方の属性が○○か、敵の属性が○○か、○○という名のキャラはいるのかなど、まだまた書かないといけない. } return result; } private float Calc_HpParcent(ICharaStatus_FVRPG checkStatus) { return checkStatus.Hp_Now / (float)checkStatus.Hp; } }
回答5件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2017/01/25 12:30
2017/01/25 12:34
2017/01/25 12:46
2017/01/25 13:00