Пример #1
0
 public Rover(Plane plane, string command, RoverLocation startLocation)
 {
     StartLocation   = startLocation;
     CurrentPlane    = plane;
     CommandToFollow = command;
     roverNavigation = new RoverNavigation(startLocation.Direction);
 }
Пример #2
0
        private Rover GetRover(MarsArea marsArea, string locationText)
        {
            RoverLocation roverLocation = GetRoverLocation(locationText);

            IDirectionState directionState = GetDirectionState(locationText);
            ILocation       location       = new Location(marsArea, roverLocation);

            return(new Rover(directionState, location));
        }
Пример #3
0
        /// <summary>
        /// Start executing the Mission Orders
        /// </summary>
        public bool ExecuteOrders()
        {
            // Assumptions: The Mission Orders have already been parsed by Regex to only contain 'M', 'L' or 'R' characters.

            bool          orderCarriedOut       = false;
            RoverLocation previousRoverLocation = null;

            // Log the start of the mission
            AddToEventLog(string.Format("{0}: Starting Mission", Name));

            foreach (char currentOrder in this.m_MissionOrders.ToCharArray())
            {
                // Remember the current location, for comparison with the new location.
                previousRoverLocation = m_currentLocation.Clone();

                // Log the next order
                AddToEventLog(string.Format("{0}: Processing Order:{1}", Name, currentOrder));

                switch (currentOrder)
                {
                case Constants.INPUT_ORDER_MOVE:
                    orderCarriedOut = MoveForward();
                    break;

                case Constants.INPUT_ORDER_LEFT:
                    orderCarriedOut = TurnLeft();
                    break;

                case Constants.INPUT_ORDER_RIGHT:
                    orderCarriedOut = TurnRight();
                    break;

                default:
                    throw new Exceptions.UnknownRoverOrderException(currentOrder);
                }

                // If an order was unsuccesful, stop processing further mission orders.
                if (!orderCarriedOut)
                {
                    AddToEventLog(string.Format("{0}: Mission Failed!", Name));
                    return(false);
                }

                // Log the succesful movement
                AddToEventLog(string.Format("{0}: Succesfully changed position from '{1}' to '{2}'", Name, previousRoverLocation, m_currentLocation));
            }

            // Nothing has gone wrong, mark the mission as a success
            AddToEventLog(string.Format("{0}: Mission Completed!", Name));
            m_currentStatus = RoverStatus.MissionComplete;
            return(true);
        }
Пример #4
0
        public RoverLocation Operate()
        {
            var x         = StartLocation.Coordinates.X;
            var y         = StartLocation.Coordinates.Y;
            var direction = StartLocation.Direction;

            foreach (var command in CommandToFollow)
            {
                RoverCommand cmd;
                Enum.TryParse(command.ToString(CultureInfo.InvariantCulture), out cmd);
                switch (cmd)
                {
                case RoverCommand.M:
                    switch (direction)
                    {
                    case CompassDirection.East:
                        x++;
                        break;

                    case CompassDirection.North:
                        y++;
                        break;

                    case CompassDirection.South:
                        y--;
                        break;

                    case CompassDirection.West:
                        x--;
                        break;
                    }
                    x = SanitizeXCoordinate(x);
                    y = SanitizeYCoordinate(y);
                    break;

                case RoverCommand.L:
                    direction = roverNavigation.TurnLeft();
                    break;

                case RoverCommand.R:
                    direction = roverNavigation.TurnRight();
                    break;

                default:
                    throw new Exception("Unknown rover command");
                }
            }
            EndLocation = new RoverLocation(new Point2D(x, y), direction);
            return(EndLocation);
        }
Пример #5
0
        /// <summary>
        /// Tell the rover that it has landed and is about to start wondering about.
        /// </summary>
        /// <returns></returns>
        public bool LandOnPlateau()
        {
            if (m_plateauXDimension == 0 || m_plateauYDimension == 0)
            {
                throw new Exceptions.NotReadyForLandingException("This Rover doesn't know the size of the plateau to land on.");
            }

            // Set the current location to the pre-programmed starting location
            m_currentLocation = m_startingLocation.Clone();

            // Update the status of the rover
            m_currentStatus = RoverStatus.Roving;

            return(true);
        }
Пример #6
0
 public Location(MarsArea marsArea, RoverLocation roverLocation)
 {
     _marsArea      = marsArea;
     _roverLocation = roverLocation;
 }