Exemplo n.º 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");
            }
        }
Exemplo n.º 2
0
        public void TwoDifferentLawnSizesShouldNotBeEqual()
        {
            var lawnSize1 = new LawnSize(5, 10);
            var lawnSize2 = new LawnSize(10, 10);

            (lawnSize1.Equals(lawnSize2))
            .Should()
            .BeFalse();
        }
Exemplo n.º 3
0
        public void TwoTheSameLawnSizesShouldBeEqual()
        {
            var lawnSize1 = new LawnSize(5, 10);
            var lawnSize2 = new LawnSize(5, 10);

            (lawnSize1.Equals(lawnSize2))
            .Should()
            .BeTrue();
        }
Exemplo n.º 4
0
        public async Task PostLawn([FromBody] LawnSize lawnData)
        {
            if (!ModelState.IsValid)
            {
                throw new InvalidOperationException("Invalid lawn data provided.");
            }

            await Task.Run(() => _lawnCommands.SetSize(lawnData.StartX, lawnData.StartX, lawnData.SizeX, lawnData.SizeY)).ConfigureAwait(false);
        }
Exemplo n.º 5
0
        public void IsPositionWithinLawn_ShouldReturnFalseWhenPositionIsNotWithin(
            int positionX,
            int positionY)
        {
            var lawnSize = new LawnSize(5, 10);
            var position = new Position(positionX, positionY);

            lawnSize
            .IsPositionWithinLawn(position)
            .Should()
            .BeFalse();
        }
Exemplo n.º 6
0
        public IHttpActionResult GetLawn()
        {
            Direction orient   = _lawnQuey.MachineDirection;
            ILocation loc      = _lawnQuey.MachineLocation;
            int       height   = _lawnQuey.LawnHeight;
            int       width    = _lawnQuey.LawnWidth;
            LawnSize  lawnInfo = new LawnSize()
            {
                SizeX = width, SizeY = height, StartX = loc.X, StartY = loc.Y
            };

            return(Ok(lawnInfo));
        }
Exemplo n.º 7
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);
        }
        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));
        }