private void Forward(IRobot robot, IGrid grid, Point initialPosition) { robot.MoveForward(); var position = robot.GetPosition(); if (grid.IsOutside(position)) { robot.Status = RobotStatus.LOST; grid.AddScent(initialPosition); } }
//TODO: the "object[] args" parameter feels super gross and wrong. Needs to be changed at some point. //Perhaps declaring some sort of IInstructionArguments (generic?) would make more sense and simplify //the code in this method. //REFACTOR: It works for now, though, so I'll (hopefully) have time to come back to this. public void InterpretInstruction(Instruction instruction, object[] args) { switch (instruction) { case Instruction.PLACE: if (TryParsePlaceArguments(args, out int xArg, out int yArg, out Orientation orientationArg)) { //TODO: Create IPositionFactory, or perhaps better, output a IVector directionalPlacement from the above //TryParsePlaceArguments, which can contain Position and Orientation data for us. _robot.Place(new Position(xArg, yArg), orientationArg); } break; case Instruction.MOVE: if (_robot.IsPlaced) { _robot.Move(); } break; case Instruction.LEFT: if (_robot.IsPlaced) { _robot.Rotate(SpinDirection.LEFT); } break; case Instruction.RIGHT: if (_robot.IsPlaced) { _robot.Rotate(SpinDirection.RIGHT); } break; case Instruction.REPORT: if (_robot.IsPlaced) { _reporter.Report(_robot.GetPosition(), _robot.GetOrientation().Value); } break; default: return; } }
public Task ExecuteAsync(IGrid grid, IRobot robot) { if (grid == null) { throw new ArgumentNullException(nameof(grid)); } if (robot == null) { throw new ArgumentNullException(nameof(robot)); } var initialPosition = robot.GetPosition(); if (grid.IsScent(initialPosition)) { ScentForward(robot, grid); } else { Forward(robot, grid, initialPosition); } return(Task.FromResult(0)); }