Ejemplo n.º 1
0
        public async Task <IActionResult> Post()
        {
            InputData inputData;

            try
            {
                string inputDataRaw = string.Empty;
                using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
                {
                    inputDataRaw = await reader.ReadToEndAsync();
                }
                inputData = InputHelper.ReadInputData(inputDataRaw);
                var missionControl = new MissionControlService(inputData.UpperRightCoordinate);
                var results        = missionControl.ExecuteMission(inputData.RobotInstructions);

                return(Ok(OutputHelper.FormatOutput(results)));
            }
            catch (ArgumentException ex)
            {
                var message = "Invalid input. Check log for more details";
                _logger.LogError($"Invalid input: {ex}");

                return(BadRequest(message));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Unexpected error: {ex}");

                return(StatusCode(500, "Unexpected error. Check log for more details"));
            }
        }
Ejemplo n.º 2
0
        public void WhenRobotsFallsOffTheGridAScentIsCreatedInTheLastPosition()
        {
            var instructions = InputHelper.ReadInputData(@"5 3
3 2 N
FRRFLLFFRRFLL");
            MissionControlService service = new MissionControlService(instructions.UpperRightCoordinate);

            var results = service.ExecuteMission(instructions.RobotInstructions);

            Assert.True(service.Surface.PositionHasScent(new Coordinates(3, 3)));
        }
Ejemplo n.º 3
0
        public void WhenRobotMovesOffTheGridItsStatusIsLost()
        {
            var instructions = InputHelper.ReadInputData(@"5 3
3 2 N
FRRFLLFFRRFLL");
            MissionControlService service = new MissionControlService(instructions.UpperRightCoordinate);

            var results = service.ExecuteMission(instructions.RobotInstructions);

            Assert.Equal(RobotStatus.Lost, results.First().Status);
        }
Ejemplo n.º 4
0
        public void WhenRobotMovesOnTheGridTheEndPositionIsCorrect()
        {
            var instructions = InputHelper.ReadInputData(@"5 3
1 1 E
RFRFRFRF");
            MissionControlService service = new MissionControlService(instructions.UpperRightCoordinate);

            var results = service.ExecuteMission(instructions.RobotInstructions);

            Assert.Equal(RobotStatus.Ok, results.First().Status);
            Assert.Equal(1, results.First().Position.Coordinates.X);
            Assert.Equal(1, results.First().Position.Coordinates.Y);
            Assert.Equal(Orientation.E, results.First().Position.Orientation);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            InputData inputData;

            while (true)
            {
                StringBuilder sb          = new StringBuilder();
                var           currentLine = string.Empty;
                do
                {
                    currentLine = Console.ReadLine();
                    if (!string.IsNullOrEmpty(currentLine))
                    {
                        sb.AppendLine(currentLine);
                    }
                }while (!string.IsNullOrEmpty(currentLine));

                try
                {
                    inputData = InputHelper.ReadInputData(sb.ToString().Trim());
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine($"Invalid input: {ex}");
                    return;
                }

                try
                {
                    var missionControl = new MissionControlService(inputData.UpperRightCoordinate);
                    var results        = missionControl.ExecuteMission(inputData.RobotInstructions);

                    Console.WriteLine(OutputHelper.FormatOutput(results));
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Unexpected error: {ex}");
                }
            }
        }
Ejemplo n.º 6
0
        public void WhenMovingOffTheGridFromPositionWithScentRobotIgnoresTheInstruction()
        {
            MissionControlService service = new MissionControlService(new Coordinates(5, 3));
            var instructionsLost          = InputHelper.ReadInputData(@"5 3
0 3 W
LLFFFLFLFL");

            var results = service.ExecuteMission(instructionsLost.RobotInstructions);

            Assert.Equal(RobotStatus.Lost, results.First().Status);

            var instructionsWithScent = InputHelper.ReadInputData(@"5 3
0 3 W
LLFFFLFLFL");

            service = new MissionControlService(new Coordinates(5, 3));
            service.Surface.AddScent(new Coordinates(3, 3));
            var resultsIgnore = service.ExecuteMission(instructionsWithScent.RobotInstructions);

            Assert.Equal(RobotStatus.Ok, resultsIgnore.Last().Status);
            Assert.Equal(2, resultsIgnore.Last().Position.Coordinates.X);
            Assert.Equal(3, resultsIgnore.Last().Position.Coordinates.Y);
            Assert.Equal(Orientation.S, resultsIgnore.Last().Position.Orientation);
        }