コード例 #1
0
ファイル: Rover.cs プロジェクト: sbrown-nw1/MarsRover
        public PositionWithDirection Move(string moves)
        {
            var movesArray = moves.ToCharArray();

            foreach (var c in movesArray)
            {
                if (c == 'M')
                {
                    MoveOne();
                }
                else if (c == 'R')
                {
                    RotateRight();
                }
                else if (c == 'L')
                {
                    RotateLeft();
                }
                else
                {
                    throw new IllegalMoveInstruction($"Illegal move instruction: {c.ToString()} - aborting {Name}");
                }
            }

            this.PositionWithDirection = new PositionWithDirection
            {
                CompassDirection   = currentCompassDirection,
                LocationCoordinate = currentLocationCoordinate
            };

            return(this.PositionWithDirection);
        }
コード例 #2
0
ファイル: Rover.cs プロジェクト: sbrown-nw1/MarsRover
        public Rover(LocationCoordinate startLocationCoordinate, CompassDirection compassStartDirection, IGrid grid)
        {
            if (startLocationCoordinate == null ||
                startLocationCoordinate.X < 0 ||
                startLocationCoordinate.Y < 0)
            {
                throw new ArgumentException($"Rover X, Y start coordinates must be greater than or equal to 0 - aborting {Name}");
            }

            this.grid = grid ?? throw new ArgumentException($"Rover grid argument cannot be null - aborting {Name}");
            Name      = $"Rover {Count + 1}";
            currentLocationCoordinate = startLocationCoordinate;
            currentCompassDirection   = compassStartDirection;
            PositionWithDirection     = new PositionWithDirection
            {
                CompassDirection   = currentCompassDirection,
                LocationCoordinate = currentLocationCoordinate
            };

            rovers.Add(this);
        }