/// <summary> /// Interpret a command set. /// </summary> public void Interpret() { // Read instruction set from where they are stored. var rawInstructions = reader.Read(); // Read the individual instructions line by line. using (var reader = new StringReader(rawInstructions)) { // Only need to read the establish grid command once, since it will be applied to all rovers. var line = reader.ReadLine(); var establishGridCommand = new EstablishGridCommand(line); // Read as many position confirmation/movement instructions as are in the instruction set. while (true) { line = reader.ReadLine(); // Check for end of the instructions. if (line == null) { break; } var confirmPositionCommand = new ConfirmPositionCommand(line); line = reader.ReadLine(); // Check for end of the instructions. if (line == null) { throw new InvalidOperationException("Expected instructions for move command."); } var moveCommand = new MoveCommand(line); var dispatcher = dispatcherFactory.Create(); // Dispatch the command set to a rover. dispatcher.Dispatch(new CommandSet(establishGridCommand, confirmPositionCommand, moveCommand)); } } }
/// <summary> /// Constructor. /// </summary> /// <param name="establishGrid">The establish grid command.</param> /// <param name="confirmPosition">The confirm position command.</param> /// <param name="move">The move command.</param> public CommandSet(EstablishGridCommand establishGrid, ConfirmPositionCommand confirmPosition, MoveCommand move) { this.EstablishGrid = establishGrid ?? throw new ArgumentNullException(nameof(establishGrid)); this.ConfirmPosition = confirmPosition ?? throw new ArgumentNullException(nameof(confirmPosition)); this.Move = move ?? throw new ArgumentNullException(nameof(move)); }