public static LawnDimension gridDimension(string gridSize)
        {
            LawnDimension girdSize = new LawnDimension();
            int           temp;

            string[] gridcoordinates = gridSize.Split(' ');


            if (int.TryParse(gridcoordinates[0], out temp))
            {
                girdSize.GridX = temp;
            }
            else
            {
                throw new ArgumentException("Not an integer");
            }

            if (int.TryParse(gridcoordinates[1], out temp))
            {
                girdSize.GridY = temp;
            }
            else
            {
                throw new ArgumentException("Not an integer");
            }

            return(girdSize);
        }
示例#2
0
        static void Main(string[] args)
        {
            ConsoleTitle();
            List <string> moverinput = new List <string>();

            StreamReader sr       = new StreamReader("C:\\Files\\Chandra.txt");
            string       gridSize = sr.ReadLine();
            string       input    = sr.ReadLine();

            while (!string.IsNullOrEmpty(input))
            {
                moverinput.Add(input);
                input = sr.ReadLine();
            }
            var girdsize = LawnDimension.gridDimension(gridSize);

            if (moverinput.Count > 0)
            {
                List <MowerInput> mowers = new List <MowerInput>();
                for (int i = 0; i < moverinput.Count; i++)
                {
                    try
                    {
                        var eachMower = MowerInput.ParseMowerInput(moverinput[i], moverinput[i + 1]);
                        mowers.Add(eachMower);
                    }
                    catch (ArgumentException e)
                    {
                        Console.WriteLine("Exception: " + e.Message);
                        throw e;
                    }

                    i += 1;
                }

                Engine engine = new Engine(girdsize);
                foreach (var mower in mowers)
                {
                    try
                    {
                        Console.WriteLine($"Initial Position: ({mower.X}, {mower.Y}) Direction:{mower.Direction}");
                        var mowerOutput = engine.ProcessCommands(mower);
                        Console.WriteLine($"New Position: ({mowerOutput.X}, {mowerOutput.Y}) Direction:{mowerOutput.Direction}");
                    }
                    catch (ArgumentException e)
                    {
                        Console.WriteLine("Exception: " + e.Message);
                        throw e;
                    }
                }
                Console.ReadLine();
            }
            else
            {
                throw new ArgumentException("No input line detected");
            }
            sr.Close();
            Console.ReadLine();
        }
示例#3
0
 public Engine(LawnDimension gridSize)
 {
     this.gridX = gridSize.GridX;
     this.gridY = gridSize.GridY;
 }