Beispiel #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";
            }
        }
Beispiel #2
0
        public void TwoTheSameLawnSizesShouldBeEqual()
        {
            var lawnSize1 = new LawnSize(5, 10);
            var lawnSize2 = new LawnSize(5, 10);

            (lawnSize1.Equals(lawnSize2))
                .Should()
                .BeTrue();
        }
Beispiel #3
0
        public void TwoDifferentLawnSizesShouldNotBeEqual()
        {
            var lawnSize1 = new LawnSize(5, 10);
            var lawnSize2 = new LawnSize(10, 10);

            (lawnSize1.Equals(lawnSize2))
                .Should()
                .BeFalse();
        }
Beispiel #4
0
        public void IsPositionWithinLawn_ShouldReturnTrueWhenPositionIsWithin(
            int positionX,
            int positionY)
        {
            var lawnSize = new LawnSize(5, 10);
            var position = new Position(positionX, positionY);

            lawnSize
                .IsPositionWithinLawn(position)
                .Should()
                .BeTrue();
        }
        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);
        }
Beispiel #7
0
 protected bool Equals(LawnSize other)
 {
     return RightmostCoordinate == other.RightmostCoordinate && TopmostCoordinate == other.TopmostCoordinate;
 }
Beispiel #8
0
        public void ParseLawnSize_ShouldReturnCorrectLawnSizeWhenInputIsValid(
            string input,
            int expectedTopmostCoordinate,
            int expectedRightmostCoordinate)
        {
            var inputParser = new InputParser();

            var lawnSize = inputParser.ParseLawnSize(input);

            var expectedLawnSize = new LawnSize(
                expectedRightmostCoordinate,
                expectedTopmostCoordinate);

            lawnSize
                .Should()
                .Be(expectedLawnSize);
        }
Beispiel #9
0
 protected bool Equals(LawnSize other)
 {
     return(RightmostCoordinate == other.RightmostCoordinate && TopmostCoordinate == other.TopmostCoordinate);
 }