/// <summary> /// Parses robot command. First expected parameter is first capital letter of cardinal direction, and second /// is a distance which robot should move. Points should be divided by single space. /// </summary> /// <param name="command"></param> /// <returns></returns> public static RobotCommand ParseRobotCommand(string command) { var splitted = command.Split(' '); var robotCommand = new RobotCommand(); robotCommand.Distance = Int32.Parse(splitted[1]); var direction = splitted[0]; switch (direction) { case "W": robotCommand.Direction = Direction.West; break; case "E": robotCommand.Direction = Direction.East; break; case "N": robotCommand.Direction = Direction.North; break; case "S": robotCommand.Direction = Direction.South; break; } return robotCommand; }
private void Move(RobotCommand command) { switch (command.Direction) { case 'N': Position.Y += command.Steps; break; case 'E': Position.X += command.Steps; break; case 'S': Position.Y -= command.Steps; break; case 'W': Position.X -= command.Steps; break; } }
/// <summary> /// Executes single robot command. /// </summary> /// <param name="command">Robot command for execution</param> public void ExecuteCommand(RobotCommand command) { var startLocation = new Location(LastLocation.X, LastLocation.Y); for (int steps = 1; steps <= command.Distance; steps++) { switch (command.Direction) { case Direction.West: CleanLocation(new Location(startLocation.X - steps, startLocation.Y)); break; case Direction.East: CleanLocation(new Location(startLocation.X + steps, startLocation.Y)); break; case Direction.North: CleanLocation(new Location(startLocation.X, startLocation.Y + steps)); break; case Direction.South: CleanLocation(new Location(startLocation.X, startLocation.Y - steps)); break; default: throw new ArgumentOutOfRangeException(); } } }
protected bool Equals(RobotCommand other) { return Distance == other.Distance && Direction == other.Direction; }