示例#1
0
        private void ExecuteMovementCommands(RoverState roverState, string movementCommands)
        {
            var moveResult = MoveResult.Ok;

            foreach (var command in movementCommands)
            {
                switch (command)
                {
                case 'M':
                    moveResult = MoveForward(roverState);
                    break;

                case 'R':
                    RotateRight(roverState);
                    break;

                case 'L':
                    RotateLeft(roverState);
                    break;

                default:
                    throw new InvalidCommandException();
                }

                if (moveResult != MoveResult.Ok)
                {
                    roverState.MoveErrors.Add(moveResult);
                    break;
                }
            }
        }
示例#2
0
        private void RotateRight(RoverState roverState)
        {
            switch (roverState.CurrentDirection)
            {
            case Direction.Undefined:
                throw new InitialDirectionNotDefinedException();

            case Direction.North:
                roverState.CurrentDirection = Direction.East;
                break;

            case Direction.East:
                roverState.CurrentDirection = Direction.South;
                break;

            case Direction.South:
                roverState.CurrentDirection = Direction.West;
                break;

            case Direction.West:
                roverState.CurrentDirection = Direction.North;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(roverState.CurrentDirection));
            }
        }
示例#3
0
        public RoverState CreateCopy()
        {
            var roverState = new RoverState();

            roverState.CurrentLocation.X = CurrentLocation.X;
            roverState.CurrentLocation.Y = CurrentLocation.Y;
            roverState.CurrentDirection  = CurrentDirection;
            roverState.MaxExtent.X       = MaxExtent.X;
            roverState.MaxExtent.Y       = MaxExtent.Y;

            return(roverState);
        }
示例#4
0
        private MoveResult MoveForward(RoverState roverState)
        {
            var moveResult = roverState.ValidateMove();

            if (moveResult != MoveResult.Ok)
            {
                return(moveResult);
            }

            switch (roverState.CurrentDirection)
            {
            case Direction.Undefined:
                throw new InitialDirectionNotDefinedException();

            case Direction.North:
                roverState.CurrentLocation.Y++;
                break;

            case Direction.East:
                roverState.CurrentLocation.X++;
                break;

            case Direction.South:
                roverState.CurrentLocation.Y--;
                break;

            case Direction.West:
                roverState.CurrentLocation.X--;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(moveResult));
            }

            return(moveResult);
        }