Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var roversInputModel = new RoversInputModel()
            {
                RoversInput = new List <RoverInputModel>()
            };

            Console.WriteLine("Enter the size of the plateau: ");
            string sizeOfPlateau = Console.ReadLine();

            roversInputModel.Plateau = sizeOfPlateau;
            int  index = 1;
            bool confirmed;

            do
            {
                Console.WriteLine($"Enter coordinate for {index}. Rover: ");
                string roverPosition = Console.ReadLine();

                Console.WriteLine($"Enter route for {index}. Rover: ");
                string roverRoute = Console.ReadLine();

                var rover = new RoverInputModel
                {
                    Index    = index,
                    Position = roverPosition,
                    Route    = roverRoute
                };

                roversInputModel.RoversInput.Add(rover);

                ConsoleKey response;
                do
                {
                    Console.Write("Do you want to create new rover? [y/n] ");
                    response = Console.ReadKey(false).Key;   // true is intercept key (dont show), false is show
                    if (response != ConsoleKey.Enter)
                    {
                        Console.WriteLine();
                    }
                } while (response != ConsoleKey.N && response != ConsoleKey.Y);

                confirmed = response == ConsoleKey.N;
                index++;
            } while (!confirmed);

            var rovers = Manager.Manage(roversInputModel);

            Console.WriteLine("Output :");

            foreach (var rover in rovers)
            {
                Console.WriteLine($"{rover.Position.PositionX} " +
                                  $"{rover.Position.PositionY} " +
                                  $"{rover.Position.Direction}");
            }

            Console.Write("Thanks for joining Mars Rover !");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public void ToRoverTrip_ShouldAssertEqual_WhenConsoleInput(int index, string sizeOfPlateau, string roverPosition, string roverRoute, int expectedPositionX, int expectedPositionY, DirectionEnum expectedDirection)
        {
            //Arrange Step
            var roversTestInputModel = new RoversInputModel()
            {
                Plateau     = sizeOfPlateau,
                RoversInput = new List <RoverInputModel>()
            };

            var roverTestModel = new RoverInputModel()
            {
                Index    = index,
                Position = roverPosition,
                Route    = roverRoute
            };

            roversTestInputModel.RoversInput.Add(roverTestModel);

            //Act Step
            var rovers = Manager.Manage(roversTestInputModel);

            //Assert Step
            Assert.NotNull(rovers.FirstOrDefault(a => a.Id == index));
            Assert.Equal(expectedPositionX, rovers.FirstOrDefault(a => a.Id == index).Position.PositionX);
            Assert.Equal(expectedPositionY, rovers.FirstOrDefault(a => a.Id == index).Position.PositionY);
            Assert.Equal(expectedDirection, rovers.FirstOrDefault(a => a.Id == index).Position.Direction);
        }
        public static RoverInputModel ExecuteCommand(RoverInputModel inputLocation, string moveDirection)
        {
            var commands = moveDirection.ToCharArray();

            foreach (char command in commands)
            {
                switch (command)
                {
                case 'L':
                    switch (inputLocation.Position.Heading)
                    {
                    case "E":
                        inputLocation.Position.Heading = "N";
                        break;

                    case "N":
                        inputLocation.Position.Heading = "W";
                        break;

                    case "W":
                        inputLocation.Position.Heading = "S";
                        break;

                    case "S":
                        inputLocation.Position.Heading = "E";
                        break;
                    }
                    break;

                case 'R':
                    switch (inputLocation.Position.Heading)
                    {
                    case "E":
                        inputLocation.Position.Heading = "S";
                        break;

                    case "N":
                        inputLocation.Position.Heading = "E";
                        break;

                    case "W":
                        inputLocation.Position.Heading = "N";
                        break;

                    case "S":
                        inputLocation.Position.Heading = "W";
                        break;
                    }
                    break;

                case 'M':
                    inputLocation = Move(inputLocation);
                    break;
                }

                Console.WriteLine("OutPut : " + inputLocation.Position.PositionX + " " + inputLocation.Position.PositionY + " " + inputLocation.Position.Heading);
            }
            return(inputLocation);
        }
        static void Main(string[] args)
        {
            RoverInputModel inputLocation = new RoverInputModel();

            //inputLocation.Position.PositionX = 1;
            //inputLocation.Position.PositionY = 2;
            //inputLocation.Position.Heading = "N";
            //string moveDirection = "LMLMLMLMM";

            inputLocation.Position.PositionX = 3;
            inputLocation.Position.PositionY = 3;
            inputLocation.Position.Heading   = "E";
            string moveDirection = "MMRMMRMRRM";


            var output = ExecuteCommand(inputLocation, moveDirection);

            Console.Read();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Calls the Api endpoint to get the final coordinates
        /// </summary>
        /// <param name="RoverInputModel"></param>
        /// <returns></returns>
        public List <Rover.Model.RoverOutputModel> GetRoverCoordinates(string RoverInputModel)
        {
            string[] stringSeparators          = new string[] { "\r\n" };
            var      commands                  = RoverInputModel.TrimEnd().Split(stringSeparators, StringSplitOptions.None);
            List <RoverInputModel> inputModels = new List <RoverInputModel>();
            var gridDimensions                 = commands[0].Split(' '); // First line sets the plateaus dimensoins

            RoverControlModel RoverControlModel = new RoverControlModel();

            RoverControlModel.GridTopX = int.Parse(gridDimensions[0]);
            RoverControlModel.GridTopY = int.Parse(gridDimensions[1]);

            for (int i = 1; i < commands.Length; i += 2)
            {
                RoverInputModel model   = new RoverInputModel();
                var             postion = commands[i].Split(' ');
                model.Position.PositionX = int.Parse(postion[0]);
                model.Position.PositionY = int.Parse(postion[1]);
                model.Position.Heading   = postion[2];
                model.Instructions       = commands[i + 1];

                RoverControlModel.RoverInputs.Add(model);
            }
            string inputJson = JsonConvert.SerializeObject(RoverControlModel);

            List <RoverOutputModel> FinalRoverCoordinates = new List <RoverOutputModel>();

            try
            {
                FinalRoverCoordinates = ExecuteRoverNavigation(RoverControlModel);
            }
            catch (Exception ex)
            {
                //Log Exception
                Console.WriteLine(ex.Message);
                throw new Exception("Error occured while accessing the service API");
            }
            return(FinalRoverCoordinates);
        }
Ejemplo n.º 6
0
        public IActionResult Explore(RoverInputModel roverInputModel)
        {
            var resultModel = new ResultModel();

            try
            {
                var coordinates = SplitCoordinate(roverInputModel.Plateau);


                var firstRoverPos = SplitCoordinate(roverInputModel.FirstRoverPos);
                //Yönü ayırdık
                var firtRoverNavigate = firstRoverPos.LastOrDefault();
                var firstIndex        = firstRoverPos.Count() - 1;
                //x ve y aldık kaldı
                firstRoverPos.RemoveAt(firstIndex);
                var firstRoverMove = SplitCoordinate(roverInputModel.FirstRoverMove);


                var secondRoverPos = SplitCoordinate(roverInputModel.SecondRoverPos);
                //Yönü ayırdık
                var secondRoverNavigate = secondRoverPos.LastOrDefault();
                var secondIndex         = secondRoverPos.Count() - 1;
                //x ve y aldık kaldı
                secondRoverPos.RemoveAt(secondIndex);


                var secondRoverMove = SplitCoordinate(roverInputModel.SecondRoverMove);


                resultModel.FirstRoverResult  = FindDestination(coordinates, firstRoverPos, firtRoverNavigate, firstRoverMove);
                resultModel.SecondRoverResult = FindDestination(coordinates, secondRoverPos, secondRoverNavigate, secondRoverMove);
            }
            catch (Exception ex)
            {
                throw new Exception("hata" + ex);
            }
            return(View(resultModel));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Mthod to move the x,y coordinates of the Rover
        /// </summary>
        /// <param name="inputLocation"></param>
        /// <returns></returns>
        private RoverInputModel Move(RoverInputModel inputLocation)
        {
            string direction        = inputLocation.Position.Heading;
            string move_direction   = "";
            bool   can_move         = true;
            var    currentPositionX = inputLocation.Position.PositionX;
            var    currentPositionY = inputLocation.Position.PositionY;

            if (direction == "E" || direction == "W")
            {
                move_direction = "X";
            }
            else if (direction == "S" || direction == "N")
            {
                move_direction = "Y";
            }

            if (direction == "E" || direction == "N")
            {
                if (direction == "E")
                {
                    if ((currentPositionX < 0 || (currentPositionX) > GridTopX))
                    {
                        can_move = false;
                    }
                }

                if (direction == "N")
                {
                    if (currentPositionY < 0 || (currentPositionY) > GridTopY)
                    {
                        can_move = false;
                    }
                }

                if (can_move)
                {
                    if (move_direction == "X")
                    {
                        inputLocation.Position.PositionX += 1;
                    }
                    else
                    {
                        inputLocation.Position.PositionY += 1;
                    }
                }
            }
            else if (direction == "W" || direction == "S")
            {
                if (move_direction == "X" && currentPositionX > 0)
                {
                    inputLocation.Position.PositionX -= 1;
                }
                else if (move_direction == "Y" && currentPositionY > 0)
                {
                    inputLocation.Position.PositionY -= 1;
                }
            }

            return(inputLocation);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Processes the navigation instructions
        /// </summary>
        /// <param name="RoverInputCommand"></param>
        /// <returns></returns>
        private RoverOutputModel ExecuteCommand(RoverInputModel RoverInputCommand)
        {
            var commands = RoverInputCommand.Instructions.ToCharArray();
            RoverOutputModel finalCoodinates = new RoverOutputModel();

            foreach (char command in commands)
            {
                switch (command)
                {
                case 'L':
                    switch (RoverInputCommand.Position.Heading)
                    {
                    case "E":
                        RoverInputCommand.Position.Heading = "N";
                        break;

                    case "N":
                        RoverInputCommand.Position.Heading = "W";
                        break;

                    case "W":
                        RoverInputCommand.Position.Heading = "S";
                        break;

                    case "S":
                        RoverInputCommand.Position.Heading = "E";
                        break;
                    }
                    break;

                case 'R':
                    switch (RoverInputCommand.Position.Heading)
                    {
                    case "E":
                        RoverInputCommand.Position.Heading = "S";
                        break;

                    case "N":
                        RoverInputCommand.Position.Heading = "E";
                        break;

                    case "W":
                        RoverInputCommand.Position.Heading = "N";
                        break;

                    case "S":
                        RoverInputCommand.Position.Heading = "W";
                        break;
                    }
                    break;

                case 'M':
                    RoverInputCommand = Move(RoverInputCommand);
                    break;
                }
            }

            finalCoodinates.PositionX = RoverInputCommand.Position.PositionX;
            finalCoodinates.PositionY = RoverInputCommand.Position.PositionY;
            finalCoodinates.Heading   = RoverInputCommand.Position.Heading;

            return(finalCoodinates);
        }