Пример #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 GetStateAfterRightTurn_ShouldReturnCorrectNewState()
        {
            var originalState = new LawnMowerState(new Position(5, 10), Direction.East);

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

            var newState = originalState.GetStateAfterRightTurn();

            newState
                .Should()
                .Be(expectedNewState);
        }
Пример #3
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);
        }
 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();
     }
 }
Пример #5
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 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);
        }
Пример #8
0
 protected bool Equals(LawnMowerState other)
 {
     return Position.Equals(other.Position) && Equals(Direction, other.Direction);
 }
Пример #9
0
 protected bool Equals(LawnMowerState other)
 {
     return(Position.Equals(other.Position) && Equals(Direction, other.Direction));
 }