示例#1
0
        public void Navigate(Models.Rover rover)
        {
            Position finalPosition = new Position();

            char[] instructions = rover.Instructions.ToCharArray();

            foreach (var instruction in instructions)
            {
                ProcessInstruction(rover, Char.ToUpper(instruction));
            }
        }
示例#2
0
        private bool CheckRoverPositionAllowed(Models.Rover rover)
        {
            if (_roverModel.PlateauSizeLongitude <= rover.Position.Longitude)
            {
                throw new Exception(string.Format("Rover Longitude ({0}) is out of plateau (max {1})"
                                                  , rover.Position.Longitude.ToString()
                                                  , _roverModel.PlateauSizeLongitude - 1));
            }
            if (_roverModel.PlateauSizeLatitude <= rover.Position.Latitude)
            {
                throw new Exception(string.Format("Rover Latitude ({0}) is out of plateau (max {1})"
                                                  , rover.Position.Longitude.ToString()
                                                  , _roverModel.PlateauSizeLatitude - 1));
            }

            return(true);
        }
示例#3
0
        private void ProcessInstruction(Models.Rover rover, char instruction)
        {
            Heading heading;

            heading = ConvertToHeading(instruction);

            switch (heading)
            {
            case Heading.M:
                Move(rover);
                if (!CheckRoverPositionAllowed(rover))
                {
                    throw new Exception("The Rover has moved out of the plateau");
                }
                break;

            default:
                rover.Position.Heading = heading;
                break;
            }
        }
示例#4
0
        private void Move(Models.Rover rover)
        {
            switch (rover.Position.Heading)
            {
            case Heading.N:
                rover.Position.Latitude++;
                break;

            case Heading.E:
                rover.Position.Longitude++;
                break;

            case Heading.S:
                rover.Position.Latitude--;
                break;

            case Heading.W:
                rover.Position.Longitude--;
                break;

            default:
                throw new Exception("The current heading does not exist");
            }
        }