예제 #1
0
 public Rover(IPlanet planet)
 {
     _planet          = planet;
     _currentPosition = new Position(new Point(0, 0), Direction.North, planet);
     _commandLookup   = new Dictionary <char, Func <IMovePostion> >
     {
         { 'F', () => _currentPosition.Forward() },
         { 'B', () => _currentPosition.Backward() },
         { 'L', () => _currentPosition.TurnLeft() },
         { 'R', () => _currentPosition.TurnRight() }
     };
 }
예제 #2
0
        public MoveResult Move(string commands)
        {
            if (string.IsNullOrWhiteSpace(commands))
            {
                throw new ArgumentException($"Commands can't empty. {nameof(commands)}");
            }
            if (commands.ToCharArray().Any(c => !_validCommands.Contains(c)))
            {
                throw new InvalidMoveCommandException("Invalid command detected");
            }

            foreach (var command in commands)
            {
                var nextPosition = _commandLookup[command]();
                if (IsObstacle(nextPosition.Coordinate))
                {
                    return(MoveResult.CreateObstacleResult(_currentPosition.Coordinate, nextPosition.Coordinate.Point));
                }

                _currentPosition = nextPosition;
            }

            return(MoveResult.CreateMoveResult(_currentPosition.Coordinate));
        }