Ejemplo n.º 1
0
        public static ToyRobotInputModel ExecuteCommand(ToyRobotInputModel inputLocation, string[] moveDirection)
        {
            foreach (string command in moveDirection)
            {
                switch (command)
                {
                case "LEFT":
                    switch (inputLocation.Position.Heading)
                    {
                    case "EAST":
                        inputLocation.Position.Heading = "NORTH";
                        break;

                    case "NORTH":
                        inputLocation.Position.Heading = "WEST";
                        break;

                    case "WEST":
                        inputLocation.Position.Heading = "SOUTH";
                        break;

                    case "SOUTH":
                        inputLocation.Position.Heading = "EAST";
                        break;
                    }
                    break;

                case "RIGHT":
                    switch (inputLocation.Position.Heading)
                    {
                    case "EAST":
                        inputLocation.Position.Heading = "SOUTH";
                        break;

                    case "NORTH":
                        inputLocation.Position.Heading = "EAST";
                        break;

                    case "WEST":
                        inputLocation.Position.Heading = "NORTH";
                        break;

                    case "SOUTH":
                        inputLocation.Position.Heading = "WEST";
                        break;
                    }
                    break;

                case "MOVE":
                    inputLocation = Move(inputLocation);
                    break;
                }

                Console.WriteLine("OutPut : " + inputLocation.Position.PositionX + " " + inputLocation.Position.PositionY + " " + inputLocation.Position.Heading);
            }
            return(inputLocation);
        }
Ejemplo n.º 2
0
        public string ValidateInput(string inputCommands)
        {
            ToyRobotOutputViewModel    outputModel      = new ToyRobotOutputViewModel();
            List <ToyRobotOutputModel> finalCoordinates = new List <ToyRobotOutputModel>();
            ToyRobotControlModel       controlModel     = new ToyRobotControlModel();
            string validationError = string.Empty;

            string[] commandSeparator = new string[] { "PLACE" };
            string[] inputs           = inputCommands.Split(commandSeparator, StringSplitOptions.None);
            string[] stringSeparators = new string[] { "\r\n" };
            foreach (string commands in inputs)
            {
                ToyRobotInputModel inputModel = new ToyRobotInputModel();
                if (!string.IsNullOrEmpty(commands))
                {
                    var command = commands.Split(stringSeparators, StringSplitOptions.None);

                    if (command.Length > 1)
                    {
                        string[] startingCoordinate = command[0].Split(',');
                        if (startingCoordinate.Length > 2)
                        {
                            inputModel.Position.PositionX = Convert.ToInt32(startingCoordinate[0]);
                            inputModel.Position.PositionY = Convert.ToInt32(startingCoordinate[1]);
                            inputModel.Position.Heading   = startingCoordinate[2];
                        }
                        for (int i = 1; i < command.Length; i++)
                        {
                            if (!string.IsNullOrEmpty(command[i]))
                            {
                                inputModel.InstructionSet.Add(command[i]);
                            }
                        }
                    }
                    else
                    {
                        validationError = "First ";
                    }
                    controlModel.RobotInputs.Add(inputModel);
                }
            }

            if (!inputCommands.Contains("PLACE"))
            {
                validationError = "At least one PLACE command should be present";
            }
            if (!inputCommands.Contains("REPORT"))
            {
                validationError += Environment.NewLine + "At least one REPORT command should be present";
            }
            return(validationError);
        }
Ejemplo n.º 3
0
        public ActionResult Index(string robotInputModel)
        {
            ToyRobotOutputViewModel    outputModel      = new ToyRobotOutputViewModel();
            List <ToyRobotOutputModel> finalCoordinates = new List <ToyRobotOutputModel>();
            ToyRobotControlModel       controlModel     = new ToyRobotControlModel();

            string[] commandSeparator = new string[] { "PLACE" };
            string[] inputs           = robotInputModel.Split(commandSeparator, StringSplitOptions.None);
            string[] stringSeparators = new string[] { "\r\n" };

            //Validates the inputs
            outputModel.ErrorMessage = ValidateInput(robotInputModel);

            if (string.IsNullOrEmpty(outputModel.ErrorMessage))
            {
                foreach (string commands in inputs)
                {
                    ToyRobotInputModel inputModel = new ToyRobotInputModel();
                    if (!string.IsNullOrEmpty(commands))
                    {
                        var command = commands.Split(stringSeparators, StringSplitOptions.None);

                        if (command.Length > 1)
                        {
                            string[] startingCoordinate = command[0].Split(',');
                            if (startingCoordinate.Length > 2)
                            {
                                inputModel.Position.PositionX = Convert.ToInt32(startingCoordinate[0]);
                                inputModel.Position.PositionY = Convert.ToInt32(startingCoordinate[1]);
                                inputModel.Position.Heading   = startingCoordinate[2];
                            }
                            for (int i = 1; i < command.Length; i++)
                            {
                                if (!string.IsNullOrEmpty(command[i]))
                                {
                                    inputModel.InstructionSet.Add(command[i]);
                                }
                            }
                        }
                        controlModel.RobotInputs.Add(inputModel);
                    }
                }
            }
            finalCoordinates            = robotMovementServices.ExecuteRobotNavigation(controlModel);
            outputModel.ToyRobotOutputs = finalCoordinates;
            //outputModel.ErrorMessage = robotInputModel;
            return(View(outputModel));
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            ToyRobotInputModel inputLocation = new ToyRobotInputModel();

            inputLocation.Position.PositionX = 1;
            inputLocation.Position.PositionY = 2;
            inputLocation.Position.Heading   = "EAST";


            //inputLocation.Position.PositionX = 0;
            //inputLocation.Position.PositionY = 0;
            //inputLocation.Position.Heading = "NORTH";
            //string[] instructionSet = { "MOVE", "REPORT" };
            string[] instructionSet = { "MOVE", "MOVE", "LEFT", "MOVE", "REPORT" };

            var output = ExecuteCommand(inputLocation, instructionSet);

            Console.Read();
        }
Ejemplo n.º 5
0
        public static ToyRobotInputModel Move(ToyRobotInputModel 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 == "EAST" || direction == "WEST")
            {
                move_direction = "X";
            }
            else if (direction == "SOUTH" || direction == "NORTH")
            {
                move_direction = "Y";
            }

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

                if (direction == "NORTH")
                {
                    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 == "WEST" || direction == "SOUTH")
            {
                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.º 6
0
        private ToyRobotOutputModel ExecuteCommand(ToyRobotInputModel robotInputModel)
        {
            ToyRobotOutputModel finalCoordinates = new ToyRobotOutputModel();

            foreach (string command in robotInputModel.InstructionSet)
            {
                switch (command)
                {
                case "LEFT":
                    switch (robotInputModel.Position.Heading)
                    {
                    case "EAST":
                        robotInputModel.Position.Heading = "NORTH";
                        break;

                    case "NORTH":
                        robotInputModel.Position.Heading = "WEST";
                        break;

                    case "WEST":
                        robotInputModel.Position.Heading = "SOUTH";
                        break;

                    case "SOUTH":
                        robotInputModel.Position.Heading = "EAST";
                        break;
                    }
                    break;

                case "RIGHT":
                    switch (robotInputModel.Position.Heading)
                    {
                    case "EAST":
                        robotInputModel.Position.Heading = "SOUTH";
                        break;

                    case "NORTH":
                        robotInputModel.Position.Heading = "EAST";
                        break;

                    case "WEST":
                        robotInputModel.Position.Heading = "NORTH";
                        break;

                    case "SOUTH":
                        robotInputModel.Position.Heading = "WEST";
                        break;
                    }
                    break;

                case "MOVE":
                    robotInputModel = Move(robotInputModel);
                    break;

                case "REPORT":
                    finalCoordinates.PositionX = robotInputModel.Position.PositionX;
                    finalCoordinates.PositionY = robotInputModel.Position.PositionY;
                    finalCoordinates.Heading   = robotInputModel.Position.Heading;
                    break;
                }

                // Console.WriteLine("OutPut : " + robotInputModel.Position.PositionX + " " + robotInputModel.Position.PositionY + " " + robotInputModel.Position.Heading);
            }

            return(finalCoordinates);
        }