Пример #1
0
 public PlutoRover(RoverCoordinates roverCoordinates, Direction direction, int[,] grid,
                   List <RoverCoordinates> obstacles = null)
 {
     _roverCoordinates = roverCoordinates;
     _plutoSurface     = new PlutoSurface(grid);
     _direction        = direction;
     _obstacles        = obstacles ?? new List <RoverCoordinates>();
 }
Пример #2
0
        public void ShouldGetRoverStartPosition()
        {
            var coordinates = new RoverCoordinates(0, 0);

            var grid = new int[3, 3];

            var plutoRover = new PlutoRover(coordinates, Direction.N, grid);

            var roverPosition = plutoRover.Execute(string.Empty);

            roverPosition.Should().Be("0,0,N");
        }
Пример #3
0
        public void RoverSpinLeft(int x, int y, Direction direction, string command, string expectedResult)
        {
            //Given
            var coordinates = new RoverCoordinates(x, y);

            var grid = new int[3, 3];

            var plutoRover = new PlutoRover(coordinates, direction, grid);

            //When
            var roverPosition = plutoRover.Execute(command);

            //Then
            roverPosition.Should().Be(expectedResult);
        }
Пример #4
0
        public void DoNotMoveRoverBackwardWhenObstacleExists(int x, int y, Direction direction, string command, string expectedResult)
        {
            //Given
            var coordinates = new RoverCoordinates(x, y);

            var grid = new int[3, 3];

            var obstacles = new List <RoverCoordinates>
            {
                new RoverCoordinates(0, 1)
            };

            var plutoRover = new PlutoRover(coordinates, direction, grid, obstacles);

            //When
            var roverPosition = plutoRover.Execute(command);

            //Then
            roverPosition.Should().Be(expectedResult);
        }
Пример #5
0
        public string Execute(string command)
        {
            var newRoverCoordinates  = new RoverCoordinates(_roverCoordinates.X, _roverCoordinates.Y);
            var obstacleNotification = string.Empty;

            foreach (var cmd in command.ToUpper().ToCharArray())
            {
                switch (cmd)
                {
                case 'F':
                    newRoverCoordinates = _plutoSurface.MoveForward(_roverCoordinates, _direction);
                    break;

                case 'B':
                    newRoverCoordinates = _plutoSurface.MoveBackward(_roverCoordinates, _direction);
                    break;

                case 'R':
                    _direction = SpinRight(_direction);
                    break;

                case 'L':
                    _direction = SpinLeft(_direction);
                    break;
                }

                if (!_obstacles.Any(o => o.X == newRoverCoordinates.X && o.Y == newRoverCoordinates.Y))
                {
                    _roverCoordinates = newRoverCoordinates;
                }
                else
                {
                    obstacleNotification = "Obstacle found!,";
                }
            }

            return($"{obstacleNotification}{_roverCoordinates.X},{_roverCoordinates.Y},{_direction}");
        }