Ejemplo n.º 1
0
        /// <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;
        }
Ejemplo n.º 2
0
        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;
            }
        }
Ejemplo n.º 3
0
 /// <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();
         }
     }
 }
Ejemplo n.º 4
0
 protected bool Equals(RobotCommand other)
 {
     return Distance == other.Distance && Direction == other.Direction;
 }