コード例 #1
0
ファイル: Program.cs プロジェクト: jakubka/LawnMowers
        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
ファイル: LawnSizeTests.cs プロジェクト: jakubka/LawnMowers
        public void TwoTheSameLawnSizesShouldBeEqual()
        {
            var lawnSize1 = new LawnSize(5, 10);
            var lawnSize2 = new LawnSize(5, 10);

            (lawnSize1.Equals(lawnSize2))
                .Should()
                .BeTrue();
        }
コード例 #3
0
ファイル: LawnSizeTests.cs プロジェクト: jakubka/LawnMowers
        public void TwoDifferentLawnSizesShouldNotBeEqual()
        {
            var lawnSize1 = new LawnSize(5, 10);
            var lawnSize2 = new LawnSize(10, 10);

            (lawnSize1.Equals(lawnSize2))
                .Should()
                .BeFalse();
        }
コード例 #4
0
ファイル: LawnSizeTests.cs プロジェクト: jakubka/LawnMowers
        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();
        }
コード例 #5
0
        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;
            }
        }
コード例 #6
0
        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);
        }
コード例 #7
0
ファイル: LawnSize.cs プロジェクト: jakubka/LawnMowers
 protected bool Equals(LawnSize other)
 {
     return RightmostCoordinate == other.RightmostCoordinate && TopmostCoordinate == other.TopmostCoordinate;
 }
コード例 #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);
        }
コード例 #9
0
 protected bool Equals(LawnSize other)
 {
     return(RightmostCoordinate == other.RightmostCoordinate && TopmostCoordinate == other.TopmostCoordinate);
 }