Example #1
0
        /// <summary>
        /// Parses all the commands from the inputLiteral
        /// </summary>
        /// <param name="inputLiteral">The required command literal</param>
        /// <param name="roverDirection">The required direction</param>
        /// <param name="roverLocation">The required location</param>
        /// <returns>A list of calculated points</returns>
        private static List <Point> ParseMovements(string inputLiteral, RoverDirection roverDirection, Point roverLocation)
        {
            List <Point> pointList = new List <Point>();
            Point        location  = new Point(roverLocation.X, roverLocation.Y);

            foreach (char c in inputLiteral.ToCharArray())
            {
                switch (c)
                {
                case 'L':
                    roverDirection = Rotate(Rotation.Left, roverDirection);
                    break;

                case 'R':
                    roverDirection = Rotate(Rotation.Right, roverDirection);
                    break;

                case 'M':
                    location = Move(roverDirection, location);
                    pointList.Add(location);
                    break;

                default:
                    break;
                }
            }

            return(pointList);
        }
Example #2
0
        /// <summary>
        /// Computes a rover roation
        /// </summary>
        /// <param name="rotation">The required rotation</param>
        /// <param name="direction">The required direction</param>
        /// <returns>The final RoverDirection calculated</returns>
        private static RoverDirection Rotate(Rotation rotation, RoverDirection direction)
        {
            RoverDirection directionToRet = RoverDirection.North;

            switch (direction)
            {
            case RoverDirection.North:
                directionToRet = (rotation == Rotation.Left ? RoverDirection.West : RoverDirection.East);
                break;

            case RoverDirection.East:
                directionToRet = (rotation == Rotation.Left ? RoverDirection.North : RoverDirection.South);
                break;

            case RoverDirection.South:
                directionToRet = (rotation == Rotation.Left ? RoverDirection.East : RoverDirection.West);
                break;

            case RoverDirection.West:
                directionToRet = (rotation == Rotation.Left ? RoverDirection.South : RoverDirection.North);
                break;

            default:
                break;
            }

            return(directionToRet);
        }
Example #3
0
        /// <summary>
        /// Performs a rotation on the rover
        /// </summary>
        /// <param name="rotation">The required rotation's value</param>
        /// <returns>Returns the Rover object for fluent interface operations</returns>
        public Rover Rotate(Rotation rotation)
        {
            switch (this.Direction)
            {
            case RoverDirection.North:
                this.Direction = (rotation == Rotation.Left ? RoverDirection.West : RoverDirection.East);
                break;

            case RoverDirection.East:
                this.Direction = (rotation == Rotation.Left ? RoverDirection.North : RoverDirection.South);
                break;

            case RoverDirection.South:
                this.Direction = (rotation == Rotation.Left ? RoverDirection.East : RoverDirection.West);
                break;

            case RoverDirection.West:
                this.Direction = (rotation == Rotation.Left ? RoverDirection.South : RoverDirection.North);
                break;

            default:
                break;
            }

            return(this);
        }
Example #4
0
        /// <summary>
        /// Computes a rover movement
        /// </summary>
        /// <param name="direction">The required direction</param>
        /// <param name="location">The required location</param>
        /// <returns>The new location calculated</returns>
        private static Point Move(RoverDirection direction, Point location)
        {
            Point newPoint = new Point();

            switch (direction)
            {
            case RoverDirection.North:
                newPoint = new Point(location.X, location.Y + 1);
                break;

            case RoverDirection.East:
                newPoint = new Point(location.X + 1, location.Y);
                break;

            case RoverDirection.South:
                newPoint = new Point(location.X, location.Y - 1);
                break;

            case RoverDirection.West:
                newPoint = new Point(location.X - 1, location.Y);
                break;

            default:
                break;
            }

            return(newPoint);
        }
        public static RoverDirection TurnLeft(RoverDirection roverDirection)
        {
            RoverDirection currentRoverDirection = roverDirection;

            switch (roverDirection)
            {
            case RoverDirection.N:
                currentRoverDirection = RoverDirection.W;
                break;

            case RoverDirection.E:
                currentRoverDirection = RoverDirection.N;
                break;

            case RoverDirection.S:
                currentRoverDirection = RoverDirection.E;
                break;

            case RoverDirection.W:
                currentRoverDirection = RoverDirection.S;
                break;

            default:
                throw new InvalidOperationException();
            }
            return(currentRoverDirection);
        }
Example #6
0
        /// <summary>
        /// Gets the RoverDirection by a char value
        /// </summary>
        /// <param name="c">The char to parse</param>
        /// <returns>The calculated RoverDirection enum value</returns>
        public static RoverDirection GetDirectionByChar(char c)
        {
            RoverDirection directionToReturn = RoverDirection.North;

            switch (c)
            {
            case 'N':
                directionToReturn = RoverDirection.North;
                break;

            case 'E':
                directionToReturn = RoverDirection.East;
                break;

            case 'S':
                directionToReturn = RoverDirection.South;
                break;

            case 'W':
                directionToReturn = RoverDirection.West;
                break;

            default:
                break;
            }
            return(directionToReturn);
        }
Example #7
0
 public void SetPosition(int x, int y, RoverDirection facing)
 {
     this.x = x;
     this.y = y;
     this.facing = facing;
     CheckIfOutOfBounds();
 }
Example #8
0
        /// <summary>
        /// Sets a Rover object on the board
        /// </summary>
        /// <param name="location">The required location for the Rover</param>
        /// <param name="direction">The initial direction of the Rover</param>
        /// <returns>The Rover object set on the board</returns>
        public Rover SetRoverOnBoard(Point location, RoverDirection direction = RoverDirection.North)
        {
            RoverBoardSquare square = GetSquareByLocation(location);

            square.IsSet = true;
            square.Rover = new Rover
            {
                Direction = direction,
                Location  = location
            };
            square.Rover.OnLocationChanged += new EventHandler <RoverLocationEventArgs>(Rover_OnLocationChanged);

            return(square.Rover);
        }
        public static IRoverState GetRoverState(RoverDirection direction)
        {
            switch (direction)
            {
            case RoverDirection.North:
                return(new RoverStateFacingNorth());

            case RoverDirection.East:
                return(new RoverStateFacingEast());

            case RoverDirection.West:
                return(new RoverStateFacingWest());

            case RoverDirection.South:
                return(new RoverStateFacingSouth());

            default:
                return(null);
            }
        }
Example #10
0
 private void TurnRight()
 {
     switch (facing)
     {
         case RoverDirection.North:
             facing = RoverDirection.East;
             break;
         case RoverDirection.South:
             facing = RoverDirection.West;
             break;
         case RoverDirection.East:
             facing = RoverDirection.South;
             break;
         case RoverDirection.West:
             facing = RoverDirection.North;
             break;
         default:
             break;
     }
 }
Example #11
0
        public string Execute(string input)
        {
            foreach (var command in input)
            {
                if (command == 'L' || command == 'R')
                {
                    _direction = Turn(command);
                }

                if (command == 'M')
                {
                    _coordinate = Move();
                    if (_obstacleHit)
                    {
                        return($"O:{_coordinate.X}:{_coordinate.Y}:{Direction()}");
                    }
                }
            }

            return($"{_coordinate.X}:{_coordinate.Y}:{Direction()}");
        }
Example #12
0
        public void GeneratePositionAndRightCommand(
            int positionX,
            int positionY,
            RoverDirection roverDirection,
            int expectedX,
            int expectedY,
            RoverDirection expectedRoverDirection)
        {
            var position = new Position()
            {
                PositionX        = positionX,
                PositionY        = positionY,
                HeadingDirection = roverDirection
            };

            Commands.Right(position);

            Assert.NotNull(position);
            Assert.Equal(expectedX, position.PositionX);
            Assert.Equal(expectedY, position.PositionY);
            Assert.Equal(expectedRoverDirection, position.HeadingDirection);
        }
Example #13
0
        private void RightRotation()
        {
            switch (this.Direction)
            {
            case RoverDirection.N:
                this.Direction = RoverDirection.E;
                break;

            case RoverDirection.S:
                this.Direction = RoverDirection.W;
                break;

            case RoverDirection.E:
                this.Direction = RoverDirection.S;
                break;

            case RoverDirection.W:
                this.Direction = RoverDirection.N;
                break;

            default:
                break;
            }
        }
Example #14
0
 public Position()
 {
     X         = Y = 0;
     Direction = RoverDirection.N;
     FlowPath  = new List <RoverPoints>();
 }
Example #15
0
 public void SetCurrentDirection(RoverDirection newDirection)
 {
     this.state = RoverStateFactory.GetRoverState(newDirection);
 }
Example #16
0
 public Rover(IPosition roverPosition, RoverDirection roverDirection, IPlateau marsPlateau)
 {
     RoverPosition    = roverPosition;
     DirectionOfRover = roverDirection;
     MarsPlateau      = marsPlateau;
 }
Example #17
0
 public RoverPosition(RoverDirection direction = RoverDirection.N, int x = 0, int y = 0)
 {
     this.X         = x;
     this.Y         = y;
     this.Direction = direction;
 }