如何为文本冒险游戏实现调度表?

4
我是一个使用C#制作文字冒险游戏的人,有人建议我使用分派表而不是switch语句。

这是switch语句的代码:

        #region Public Methods
        public static void Do(string aString)
        {
                if(aString == "")
                        return;

                string verb = "";
                string noun = "";

                if (aString.IndexOf(" ") > 0)
                {
                        string[] temp = aString.Split(new char[] {' '}, 2);
                        verb = temp[0].ToLower();
                        noun = temp[1].ToLower();
                }
                else
                {
                        verb = aString.ToLower();
                }

                switch(Program.GameState)
                {
                        case Program.GameStates.Playing:
                                if (IsValidInput(Commands, verb, true))
                                {
                                        switch(verb) //this is the switch statement
                                        {
                                                case "help":
                                                case "?":
                                                        WriteCommands();
                                                        break;
                                                case "exit":
                                                case "quit":
                                                        Program.GameState = Program.GameStates.Quit;
                                                        break;
                                                case "move":
                                                case "go":
                                                        MoveTo(noun);
                                                        break;
                                                case "examine":
                                                        Examine(noun);
                                                        break;
                                                case "take":
                                                case "pickup":
                                                        Pickup(noun);
                                                        break;
                                                case "drop":
                                                case "place":
                                                        Place(noun);
                                                        break;
                                                case "use":
                                                        Use(noun);
                                                        break;
                                                case "items":
                                                case "inventory":
                                                case "inv":
                                                        DisplayInventory();
                                                        break;
                                                case "attack":
                                                        //attack command
                                                        break;
                                        }
                                }
                                break;

                        case Program.GameStates.Battle:
                                if(IsValidInput(BattleCommands, verb, true))
                                {
                                        switch(verb) //this is the other switch statement
                                        {
                                                case "attack":
                                                        //attack command
                                                        break;
                                                case "flee":
                                                case "escape":
                                                        //flee command
                                                        break;
                                                case "use":
                                                        //use command
                                                        break;
                                                case "items":
                                                case "inventory":
                                                case "inv":
                                                        //items command
                                                        break;
                                        }
                                }
                                break;
                }
        }
        #endregion

如何重构以使用调度表?


2
你应该看一下这个问题,其中一个答案展示了一个C#的例子,我认为非常正确。 - psycho
2
在我开始担心那个之前,我会先专注于编写更小、更模块化的函数;-) - user166390
@psycho 这个回答完美地解答了我的问题。你应该把它发布为一个答案,这样我就可以接受它了。 - rshea0
@pst,你能给我一个需要修复的例子吗? - rshea0
@ryansworld10 我倾向于将每个游戏状态逻辑放在自己的函数中,这样分支(开关或调度等)就不会嵌套。 - user166390
3个回答

12

最简单的方法是使用委托字典。

例如:

Dictionary<string, Action> dispatch = new Dictionary<string, Action>();

dispatch["help"] = new Action(() => Console.WriteLine("Hello"));
dispatch["dosomething"] = new Action(() =>
{
    // Do something else
    Console.WriteLine("Do Something");
});

// Call the 'help' command
dispatch["help"]();

对于多个不同的参数,最简单的方法可能是使用基本委托,并使用动态调用。

Dictionary<string, Delegate> dispatch = new Dictionary<string, Delegate>();

dispatch["help"] = new Action(() => Console.WriteLine("Hello"));
dispatch["dosomething"] = new Action<string>(s => Console.WriteLine(s));

dispatch["help"].DynamicInvoke();
dispatch["dosomething"].DynamicInvoke("World");

如果使用 .NET 4,您还可以使用动态类型在运行时解析,以稍微减少动态调用的混乱。

Dictionary<string, dynamic> dispatch = new Dictionary<string, dynamic>();

dispatch["help"] = new Action(() => Console.WriteLine("Hello"));
dispatch["dosomething"] = new Action<string>(s => Console.WriteLine(s));

dispatch["help"]();
dispatch["dosomething"]("World");

这很有帮助,但我如何将参数传递给在操作中调用的函数? - rshea0
@ryansworld10 请看 Action<T>, Action<T1, T2> 等等。(ActionFunc 都有许多参数化的变体,它们是单独但相关的类型。) - user166390
好的,我会选择这个。这似乎是最简单的方法。 - rshea0
我仍然不太明白如何将参数传递给存储在字典中的语句(以便我可以告诉被调用的方法我的名词)。 - rshea0
我已经为您添加了几个变量。 - tyranid

1
也许他指的是“双重分派”,或者访问者模式
您可以将代码拆分为更好的“调度程序”风格,如下所示:
public interface IGameState{
  void Help();
  void Question();
  void Attack();
}

public interface ICommand{
  bool IsValidFor(PlayingState state);
  bool IsValidFor(BattleState state);
  void Execute(IGameState state);
}

public class PlayingState : IGameState {
   public void Help(){ // Do Nothing }
   public void Question() { WriteCommands(); }

   private void WriteCommands(){ }
}

public class Battle : IGameState{
   public void Help(){ // Do Nothing }
   public void Question() { WriteCommands(); }
   public void Attack() { Roll(7); }

   private void Roll(int numDice){ }
}

public class CommandBuilder{
  public ICommand Parse(string verb){
    switch(verb){
       case "help":
         return new HelpCommand();
       case "?":
         return new QuestionCommand();
       case "attack":
         return new AttackCommand();
       default:
         return new UnknownCommand();
    }
  }
}

public class QuestionCommand(){
  bool IsValidFor(PlayingState  state){
     return true;
  }

  bool IsValidFor(BattleState state){
     return false;
  }

  void Execute(IGameState state){
     state.Question();
  }
}

public static void Do(string aString){
  var command = CommandBuilder.Parse(aString);
  if(command.IsValidFor(Program.GameStates))
     command.Execute(Program.Gamestates);
}

1
我不知道那是什么意思。而且这并没有回答我的问题,即如何实现它。 - rshea0
我有所怀疑。调度表通常是类似于Dictionary<string,Action>这样的东西,至少在我谈论它时是这样的。访问者模式只是为了面向对象而颠倒了一切。 - user166390
我已经更新了我的答案,并解释了如何以更双重分派的方式实现。 - Sheldon Warkentin
嗯,这更多是一种面向对象的方法。使用这种模式,你可以将动词切换都放在一个地方,当你的接口发生变化时——你的代码将在构建时而不是运行时出现错误。 - Sheldon Warkentin

1

撤回 @Jon Ericson 的介绍:

调度表是一种数据结构,将索引(或键,参见 @pst 的评论)值与操作相关联。它是一个相当优雅的替代开关语句。

关于实现部分,请查看 此问题,特别是 此答案,我认为它非常正确,但易于理解。


“索引”可能并非整数,因为它经常给人的印象是这样的。 “键”是一个更通用的术语,但不常用于可以制作适当调度表的数组... - user166390

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接