Exemplo n.º 1
0
        public bool Move(Plateu plateu)
        {
            bool res = false;

            switch (currentDir)
            {
            case OrientationEnum.N:
                res = incrementY(plateu);
                break;

            case OrientationEnum.S:
                res = decrementY(plateu);
                break;

            case OrientationEnum.E:
                res = incrementX(plateu);
                break;

            case OrientationEnum.W:
                res = decrementX(plateu);
                break;
            }

            return(res);
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            List <string> commands = new List <string>();

            Console.WriteLine("Enter input (type END to stop):");
            string singleCommand = string.Empty;

            do
            {
                singleCommand = Console.ReadLine();

                if (singleCommand.ToUpper() != "END")
                {
                    commands.Add(singleCommand.ToUpper());
                }
                else
                {
                    break;
                }
            } while (true);

            List <Rover> listRovers   = null;
            Plateu       plateu       = null;
            bool         commandValid = CommandValidator(commands, out listRovers, out plateu);
        }
Exemplo n.º 3
0
        private bool decrementY(Plateu plateu)
        {
            Point newCoordinates = new Point(coordinates.X, coordinates.Y - 1);

            if (plateu.OccupiedCoordinates.Contains(newCoordinates))
            {
                Console.WriteLine("Position not changed. Another Rover at: (" + newCoordinates.X + ", " + newCoordinates.Y + ")");
                return(false);
            }
            int newY = coordinates.Y - 1;

            if (newY < 0)
            {
                newY      = 0;
                ErrorFlag = true;
            }

            coordinates = new Point(coordinates.X, newY);
            return(true);
        }
Exemplo n.º 4
0
        private bool incrementX(Plateu plateu)
        {
            Point newCoordinates = new Point(coordinates.X + 1, coordinates.Y);

            if (plateu.OccupiedCoordinates.Contains(newCoordinates))
            {
                Console.WriteLine("Position not changed. Another Rover at: (" + newCoordinates.X + ", " + newCoordinates.Y + ")");
                return(false);
            }

            int newX = coordinates.X + 1;

            if (newX > plateu.MaxWidth)
            {
                newX      = plateu.MaxWidth;
                ErrorFlag = true;
            }

            coordinates = new Point(newX, coordinates.Y);
            return(true);
        }
Exemplo n.º 5
0
        public static bool CommandValidator(List <string> listToValidate, out List <Rover> listRovers, out Plateu plateu)
        {
            Regex regexRoverCoordinates = new Regex(@"^[0-9]{1}\s[0-9]{1}\s[NWSE]{1}$");
            Regex regexRoverCommand     = new Regex(@"^[LMR]*$");
            Regex regexPlateuSize       = new Regex(@"^[0-9]{1}\s[0-9]{1}$");

            listRovers = new List <Rover>();

            if (listToValidate.Count < 3 || listToValidate.Count % 2 == 0)
            {
                Console.WriteLine("Invalid number of commands - " + listToValidate.Count);
                Console.ReadKey();
                listRovers = null;
                plateu     = null;
                return(false);
            }
            else
            {
                if (!regexPlateuSize.Match(listToValidate[0]).Success)
                {
                    Console.WriteLine("Invalid plateu dimensions. Press key to terminate");
                    Console.ReadKey();
                    listRovers = null;
                    plateu     = null;
                    return(false);
                }

                plateu = new Plateu(Convert.ToInt32(listToValidate[0].Substring(0, 1)), Convert.ToInt32(listToValidate[0].Substring(2, 1)));

                for (int i = 1; i < listToValidate.Count; i++)
                {
                    Console.WriteLine("i: " + i.ToString());

                    if (!regexRoverCoordinates.Match(listToValidate[i]).Success)
                    {
                        Console.WriteLine("Invalid rover position. Press key to terminate");
                        Console.ReadKey();
                        listRovers = null;
                        plateu     = null;
                        return(false);
                    }

                    if (!regexRoverCommand.Match(listToValidate[i + 1]).Success)
                    {
                        Console.WriteLine("Invalid rover command. Press key to terminate");
                        Console.ReadKey();
                        listRovers = null;
                        plateu     = null;
                        return(false);
                    }

                    if (!plateu.OccupiedCoordinates.Contains(new Point(Convert.ToInt32(listToValidate[i].Substring(0, 1)), Convert.ToInt32(listToValidate[i].Substring(2, 1)))))
                    {
                        OrientationEnum c = (OrientationEnum)Enum.Parse(typeof(OrientationEnum), listToValidate[i].Substring(4, 1), true);

                        Rover temp = new Rover(listToValidate[i + 1], Convert.ToInt32(listToValidate[i].Substring(0, 1)), Convert.ToInt32(listToValidate[i].Substring(2, 1)));
                        plateu.OccupiedCoordinates.Add(new Point(Convert.ToInt32(listToValidate[i].Substring(0, 1)), Convert.ToInt32(listToValidate[i].Substring(2, 1))));

                        listRovers.Add(temp);
                    }
                    i++;
                }
            }

            return(true);
        }