Exemplo n.º 1
0
        public ExplorationPlan DeserializeExplorationPlan(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                throw new ArgumentException();
            }

            List <string> inputLineList = new List <string>(input.Trim().Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None));

            if (!(inputLineList.Count >= MinNumberOfInputLines && ((inputLineList.Count - NumberOfInputLinesForPlateau) % NumberOfInputLinesForEachRoverNavigationPlan == 0)))
            {
                throw new InputInvalidLineCountException();
            }

            Plateau plateau = DeserializePlateauInputLine(inputLineList);
            List <NavigationPlan> roverNavigationPlanList = DeserializeRoverNavigationPlanInputLineList(inputLineList);

            ExplorationPlan explorationPlan = new ExplorationPlan
            {
                Plateau = plateau,
                RoverNavigationPlanList = roverNavigationPlanList,
            };

            return(explorationPlan);
        }
        public void ExplorationPlanDeserializer_DeserializeExplorationPlanWithInvalidArgument_ThrowsInputInvalidPlateauLineException()
        {
            string testInput = "5 E\n" + // the plateau Y coordinate is invalid.
                               "1 2 N\n" +
                               "LMLMLMLMM\n" +
                               "3 3 E\n" +
                               "MMRMMRMRRM\n";

            Assert.Throws <InputInvalidPlateauLineException>(() => { ExplorationPlan explorationPlan = _explorationPlanDeserializer.DeserializeExplorationPlan(testInput); });
        }
        public void ExplorationPlanDeserializer_DeserializeExplorationPlanWithInvalidArgument_ThrowsInputInvalidLineCountException()
        {
            string testInput = "5 5\n" +
                               "1 2 N\n" +
                               "LMLMLMLMM\n" +
                               "3 3 E\n";

            // the series of instructions of the second rover is missing.

            Assert.Throws <InputInvalidLineCountException>(() => { ExplorationPlan explorationPlan = _explorationPlanDeserializer.DeserializeExplorationPlan(testInput); });
        }
Exemplo n.º 4
0
        public FinalStatus ExecuteExplorationPlan(ExplorationPlan explorationPlan)
        {
            List <IRoverController> roverControllerList = new List <IRoverController>();

            foreach (NavigationPlan roverNavigationPlan in explorationPlan.RoverNavigationPlanList)
            {
                IRoverController roverController = new RoverController();
                roverControllerList.Add(roverController);

                roverController.ExecuteNavigationPlan(explorationPlan.Plateau, roverNavigationPlan);
            }

            FinalStatus finalStatus = new FinalStatus
            {
                FinalRoverPositionList = roverControllerList.Select(roverController => roverController.Position).ToList()
            };

            return(finalStatus);
        }
Exemplo n.º 5
0
        public string ExecuteExplorationPlan(string input)
        {
            if (_explorationPlanDeserializer == null)
            {
                throw new ArgumentNullException("explorationPlanDeserializer");
            }

            if (_finalStatusSerializer == null)
            {
                throw new ArgumentNullException("finalStatusSerializer");
            }

            ExplorationPlan explorationPlan = _explorationPlanDeserializer.DeserializeExplorationPlan(input);

            FinalStatus finalStatus = this.ExecuteExplorationPlan(explorationPlan);

            string output = _finalStatusSerializer.SerializeFinalStatus(finalStatus);

            return(output);
        }
Exemplo n.º 6
0
        public void MarsRoverService_ExecuteExplorationPlan_ValidExplorationPlanInput()
        {
            ExplorationPlan explorationPlan = new ExplorationPlan
            {
                Plateau = new Plateau
                {
                    LowerLefttCoordinate = new Coordinate {
                        X = 0, Y = 0
                    },
                    UpperRightCoordinate = new Coordinate {
                        X = 5, Y = 5
                    }
                },
                RoverNavigationPlanList = new List <NavigationPlan>
                {
                    new NavigationPlan
                    {
                        InitialPosition = new Position  {
                            Coordinate = new Coordinate {
                                X = 1, Y = 2
                            }, DirectionType = DirectionType.N
                        },
                        InstructionTypeSequence = new List <InstructionType>
                        {
                            InstructionType.L,
                            InstructionType.M,
                            InstructionType.L,
                            InstructionType.M,
                            InstructionType.L,
                            InstructionType.M,
                            InstructionType.L,
                            InstructionType.M,
                            InstructionType.M,
                        }
                    },
                    new NavigationPlan
                    {
                        InitialPosition = new Position  {
                            Coordinate = new Coordinate {
                                X = 3, Y = 3
                            }, DirectionType = DirectionType.E
                        },
                        InstructionTypeSequence = new List <InstructionType>
                        {
                            InstructionType.M,
                            InstructionType.M,
                            InstructionType.R,
                            InstructionType.M,
                            InstructionType.M,
                            InstructionType.R,
                            InstructionType.M,
                            InstructionType.R,
                            InstructionType.R,
                            InstructionType.M,
                        }
                    }
                }
            };

            FinalStatus expectedFinalStatus = new FinalStatus
            {
                FinalRoverPositionList = new List <Position>
                {
                    new Position {
                        Coordinate = new Coordinate {
                            X = 1, Y = 3
                        }, DirectionType = DirectionType.N
                    },
                    new Position {
                        Coordinate = new Coordinate {
                            X = 5, Y = 1
                        }, DirectionType = DirectionType.E
                    }
                }
            };

            IMarsRoverService marsRoverService  = new MarsRoverService();
            FinalStatus       actualFinalStatus = marsRoverService.ExecuteExplorationPlan(explorationPlan);

            Assert.Equal(expectedFinalStatus, actualFinalStatus);
        }
        public void ExplorationPlanDeserializer_DeserializeExplorationPlanWithInvalidArgument_ThrowsInputInvalidRoverInitialPositionLineException()
        {
            string testInput = "5 5\n" +
                               "1 2 X\n" + // the direction of the first rover is invalid.
                               "LMLMLMLMM\n" +
                               "3 3 E\n" +
                               "MMRMMRMRRM\n";

            Assert.Throws <InputInvalidRoverInitialPositionLineException>(() => { ExplorationPlan explorationPlan = _explorationPlanDeserializer.DeserializeExplorationPlan(testInput); });
        }