Exemplo n.º 1
0
        /// <summary>
        /// Sets the robot's new poistion by command
        /// </summary>
        /// <param name="robot"></param>
        /// <returns></returns>
        private Robots SetNewPosition(Robots robot)
        {
            int PositionX = robot.PositionX;
            int PositionY = robot.PositionY;

            switch (robot.Direction)
            {
            case Directions.N:
                PositionY++;
                break;

            case Directions.E:
                PositionX++;
                break;

            case Directions.S:
                PositionY--;
                break;

            case Directions.W:
                PositionX--;
                break;
            }

            if (PositionX > Area.X || PositionY > Area.Y)
            {
                robot.IsSuccses    = false;
                robot.ErrorMessage = "Values are out of Area!";
            }
            else
            {
                robot.PositionX = PositionX;
                robot.PositionY = PositionY;
            }

            return(robot);
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            try
            {
                List <Robots> robots     = new List <Robots>();
                int           robotCount = 0;

                Console.WriteLine("Please Enter Number of Robots");
                string value = Console.ReadLine();
                while (!Validation.IsNumber(value))
                {
                    Console.WriteLine("Value is not valid! Please Enter Number of Robots");
                    value = Console.ReadLine();
                }
                robotCount = Convert.ToInt32(value);


                Console.WriteLine("Please Enter Area of The Plateau");
                value = Console.ReadLine();
                while (!Validation.IsAreaValid(value))
                {
                    value = "";
                    Console.WriteLine("Area is not valid! Please Enter Area of The Plateau");
                    value = Console.ReadLine();
                }
                string[] areas = value.Split(' ');
                Area     area  = new Area(Convert.ToInt32(areas[0]), Convert.ToInt32(areas[1]));


                for (int i = 0; i < robotCount; i++)
                {
                    Console.WriteLine($"Please Enter {(i + 1).ToString()}. Robot's Current Possition");
                    value = Console.ReadLine();
                    while (!Validation.IsCoordinatesValid(area, value))
                    {
                        value = "";
                        Console.WriteLine($"Robot's Current Possition is not valid! Please Enter {(i + 1).ToString()}. Robot's Current Possition");
                        value = Console.ReadLine();
                    }
                    string[] positions = value.Split(' ');


                    Console.WriteLine($"Please Enter {(i + 1).ToString()}. Robot's Commands");
                    value = Console.ReadLine();
                    while (!Validation.IsCommandsValid(value))
                    {
                        Console.WriteLine($"Commands are not valid! Please Enter {(i + 1).ToString()}. Robot's Commands");
                        value = Console.ReadLine();
                    }
                    string commands = value;

                    Robots robot = new Robots();
                    robot.PositionX = Convert.ToInt32(positions[0]);
                    robot.PositionY = Convert.ToInt32(positions[1]);
                    robot.Direction = (Directions)System.Enum.Parse(typeof(Directions), positions[2]);
                    robot.Commands  = commands;

                    robots.Add(robot);
                }

                RoverCommands rc = new RoverCommands(robots, area);
                robots = rc.RedirectRobots();

                foreach (var item in robots)
                {
                    if (item.IsSuccses)
                    {
                        Console.WriteLine($"{(robots.IndexOf(item)+1).ToString()}. Robot's new Position is {item.NewPosition}");
                    }
                    else
                    {
                        Console.WriteLine($"{(robots.IndexOf(item)+1).ToString()}. Robot's redirection is failed {item.ErrorMessage}");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error : {CommonExtensions.GetInnerException(ex).Message}");
            }
            Console.ReadLine();
        }