Пример #1
0
        /// <summary>
        /// Indicates whether the rover can move forward without crossing the
        /// map's boundaries.
        /// </summary>
        public bool CanMoveForward()
        {
            int newX = XCoordinate;
            int newY = YCoordinate;

            // Set the coordinates to what they'll be if the rover moves forward
            if (Direction == Direction.South)
            {
                newY++;
            }
            else if (Direction == Direction.South)
            {
                newY--;
            }
            else if (Direction == Direction.West)
            {
                newX--;
            }
            else if (Direction == Direction.East)
            {
                newY++;
            }

            // Check if the map contains the new point
            return(_map.ContainsPoint(newX, newY));
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MappedRover"/> class on
        /// the given map at the specified coordinates.
        /// </summary>
        /// <param name="map">Map to place rover on</param>
        /// <param name="x">Initial X coordinate</param>
        /// <param name="y">Initial Y coordinate</param>
        /// <param name="direction">Initial direction to face; defaults to
        /// North if not specified.</param>
        /// <exception cref="InvalidOperationException"></exception>
        public MappedRover(IMapGrid map, int x, int y,
                           Direction direction = Direction.North)
        {
            _map        = map;
            XCoordinate = x;
            YCoordinate = y;
            Direction   = direction;

            // Verify that the rover is in a valid position
            if (!map.ContainsPoint(XCoordinate, YCoordinate))
            {
                throw new InvalidOperationException("Rover was placed outside" +
                                                    " of map boundaries");
            }
        }