Ejemplo n.º 1
0
        private static void Main()
        {
            string[] input = { "1", "2", "N", "LMLMLMLMM", "3", "3", "E", "MMRMMRMRRM" };

            var serviceProvider = CreateContainerBuilder();
            var plateau         = serviceProvider.GetService <IPlateau>();

            plateau.SetCoordinate(new Coordinate(5, 5));

            for (var i = 0; i < input.Length - 1; i += 4)
            {
                var rover          = serviceProvider.GetService <IRover>();
                var initCoordinate = new Coordinate(Convert.ToInt32(input[i]), Convert.ToInt32(input[i + 1]));
                Enum.TryParse(input[i + 2], out DirectionType initDirection);
                rover.SetCoordinate(initCoordinate);
                rover.SetDirection(initDirection);

                // Interpreter Pattern
                var instruction = input[i + 3];
                var context     = new Context(instruction, rover);
                RoverRunner.RunExpression(context);
            }

            Console.ReadKey();
        }
Ejemplo n.º 2
0
        public void RoverRunner_RunWithInValidInstruction_ShouldBeThrowEx()
        {
            var instruction = "BLMMRMMM";
            var direction   = DirectionType.N;
            var coordinate  = new Coordinate(4, 1);

            _rover.SetCoordinate(coordinate);
            _rover.SetDirection(direction);

            var context = new Context(instruction, _rover);

            Assert.Throws <Exception>(() => RoverRunner.RunExpression(context));
        }
Ejemplo n.º 3
0
        public void Test_Run_Should_Move_To_13N_When_Given_Input()
        {
            //Given
            RoverRunner roverRunner = new RoverRunner();

            roverRunner.Add(RoverFactory.Create(1, 2, 'N', "Rover", new Plateau(5, 5)), "LMLMLMLMM");

            //When
            var result = roverRunner.Run();

            //Then
            Assert.Equal("1 3 N", result[0]);
        }
Ejemplo n.º 4
0
        public void Test_Run_Should_Move_To_51E_When_Given_Input()
        {
            //Given
            RoverRunner roverRunner = new RoverRunner();

            roverRunner.Add(RoverFactory.Create(3, 3, 'E', "Rover", new Plateau(5, 5)), "MMRMMRMRRM");

            //When
            var result = roverRunner.Run();

            //Then
            Assert.Equal("5 1 E", result[0]);
        }
Ejemplo n.º 5
0
        public void Test_Run_Should_Move_All_Rovers_Sequentially_When_Given_Input()
        {
            //Given
            RoverRunner roverRunner = new RoverRunner();

            roverRunner.Add(RoverFactory.Create(1, 2, 'N', "Rover-1", new Plateau(5, 5)), "LMLMLMLMM");
            roverRunner.Add(RoverFactory.Create(3, 3, 'E', "Rover-2", new Plateau(5, 5)), "MMRMMRMRRM");

            //When
            var result = roverRunner.Run();

            //Then
            Assert.Equal("1 3 N", result[0]);
            Assert.Equal("5 1 E", result[1]);
        }
Ejemplo n.º 6
0
        public void RoverRunner_RunWithValidInstruction_ShouldBeSuccess()
        {
            var instruction = "LMMRMMM";
            var direction   = DirectionType.N;
            var coordinate  = new Coordinate(2, 1);

            _rover.SetCoordinate(coordinate);
            _rover.SetDirection(direction);

            var context = new Context(instruction, _rover);

            RoverRunner.RunExpression(context);

            var result = _rover.GetCurrentPosition();

            result.Should().NotBeNullOrEmpty();
            result.Should().Be("0 4 N");
        }