Skip to content

Custom Commands

m349pmma edited this page Apr 4, 2025 · 1 revision

Quick Start

The engine class has a protected property IReadOnlyDictionary<string, ICommandParser> Commands, which per default is initialized to Engine.DefaultCommands. If the engine receives a line, it is split in to an array of args and args[0] is looked up in Commands. If this key exists the ICommandParser is used else new Unknown(args) is returned Lets say, we wanted a printboard command. Now we simply create a class/record that implements ICommand, and a class that implements ICommandParser.
Note: I like record classes in this situation since i just use them as tokens

public record PrintBoard() : ICommand {
  public const string arg0 = "printboard";

  public string Serialize() {
    return arg0;
  }

  public class Parser : ICommandParser {
    public ICommand Parse(Engine engine, string[] args) {
      if (arg0 == args[0]) return new PrintBoard();
      return new Unknown(args);
    }
}

Now we need to add our new new command to the Commands dictionary in our engine.

// in YourEngine : Engine
public YourEngine() {
  var commands =  new Dictionary<string, ICommandParser>(Engine.DefaultCommands);
  commands.Add(PrintBoard.arg0, new PrintPoard.Parser());
  this.Commands = commands;
}

The go command parser works a similar way. Enjoy!

Clone this wiki locally