예제 #1
0
        private static string PerformSimulation(
            LawnMowerState initialState,
            LawnSize lawnSize,
            LawnMowerCommand[] commands)
        {
            var simulator = new LawnMowerMovementSimulator();

            try
            {
                var lawnMowerSteps =
                    simulator.SimulateMovement(
                        initialState,
                        lawnSize,
                        commands);

                var finalState = lawnMowerSteps
                                 .Last();

                return($"{finalState.Position.X} {finalState.Position.Y} {finalState.Direction.Sign}");
            }
            catch (CannotMoveOutsideLawnBoundariesException)
            {
                return("Mower tried to cut rare plants");
            }
        }
예제 #2
0
        public void GetStateAfterMoveForward_ShouldReturnCorrectNewState()
        {
            var originalState = new LawnMowerState(new Position(5, 10), Direction.East);

            var expectedNewState = new LawnMowerState(new Position(6, 10), Direction.East);

            var newState = originalState.GetStateAfterMoveForward();

            newState
            .Should()
            .Be(expectedNewState);
        }
예제 #3
0
        public void GetStateAfterLeftTurn_ShouldReturnCorrectNewState()
        {
            var originalState = new LawnMowerState(new Position(5, 10), Direction.East);

            var expectedNewState = new LawnMowerState(new Position(5, 10), Direction.North);

            var newState = originalState.GetStateAfterLeftTurn();

            newState
            .Should()
            .Be(expectedNewState);
        }
예제 #4
0
        public void ParseLawnMowerState_ShouldReturnCorrectStateWhenInputIsValid(
            string input,
            int expectedX,
            int expectedY,
            char expectedDirectionSign)
        {
            var inputParser = new InputParser();

            var parsedState = inputParser.ParseLawnMowerState(input);

            var expectedState = new LawnMowerState(
                new Position(expectedX, expectedY),
                Direction.GetBySign(expectedDirectionSign));

            parsedState
            .Should()
            .Be(expectedState);
        }
        private LawnMowerState PerformCommand(
            LawnMowerState state,
            LawnMowerCommand command)
        {
            switch (command)
            {
            case LawnMowerCommand.TurnRight:
                return(state.GetStateAfterRightTurn());

            case LawnMowerCommand.TurnLeft:
                return(state.GetStateAfterLeftTurn());

            case LawnMowerCommand.MoveForward:
                return(state.GetStateAfterMoveForward());

            default:
                throw new UnknownLawnMowerCommandException();
            }
        }
        private IEnumerable <LawnMowerState> SimulateMovementImpl(
            LawnMowerState initialState,
            LawnSize lawnSize,
            IEnumerable <LawnMowerCommand> commands)
        {
            var currentState = initialState;

            foreach (var command in commands)
            {
                currentState = PerformCommand(currentState, command);

                if (!lawnSize.IsPositionWithinLawn(currentState.Position))
                {
                    throw new CannotMoveOutsideLawnBoundariesException();
                }

                yield return(currentState);
            }
        }
        public IEnumerable <LawnMowerState> SimulateMovement(
            LawnMowerState initialState,
            LawnSize lawnSize,
            IEnumerable <LawnMowerCommand> commands)
        {
            if (initialState == null)
            {
                throw new ArgumentNullException(nameof(initialState));
            }
            if (lawnSize == null)
            {
                throw new ArgumentNullException(nameof(lawnSize));
            }
            if (commands == null)
            {
                throw new ArgumentNullException(nameof(commands));
            }

            // actual implementation is in separate method to force execution of arguments checks before iterating the result
            return(SimulateMovementImpl(initialState, lawnSize, commands));
        }