Esempio n. 1
0
 internal void ValidateCommands(MowerInput mower)
 {
     if (mower.Commands == null || mower.Commands.Count == 0)
     {
         throw new ArgumentNullException("Commands set to null");
     }
 }
Esempio n. 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();
        }
Esempio n. 3
0
 internal void ValidatePosition(MowerInput mower)
 {
     if (mower.X < 0 ||
         mower.X > gridX ||
         mower.Y < 0 ||
         mower.Y > gridY)
     {
         throw new ArgumentException("Invalid Grid Coordinates");
     }
 }
Esempio n. 4
0
        internal MowerInput ProcessCommands(MowerInput mower)
        {
            ValidatePosition(mower);
            ValidateCommands(mower);
            foreach (var command in mower.Commands)
            {
                ProcessNextPosition(mower, command);

                ValidatePosition(mower);
            }

            return(mower);
        }
Esempio n. 5
0
        internal void ProcessNextPosition(MowerInput mower, MowerCommand command)
        {
            switch (mower.Direction)
            {
            case CompassDirection.North:
                if (command == MowerCommand.Move)
                {
                    mower.Y = mower.Y + 1;
                }

                else if (command == MowerCommand.Right)
                {
                    mower.Direction = CompassDirection.East;
                }

                else if (command == MowerCommand.Left)
                {
                    mower.Direction = CompassDirection.West;
                }
                break;

            case CompassDirection.South:
                if (command == MowerCommand.Move)
                {
                    mower.Y = mower.Y - 1;
                }
                else if (command == MowerCommand.Right)
                {
                    mower.Direction = CompassDirection.West;
                }

                else if (command == MowerCommand.Left)
                {
                    mower.Direction = CompassDirection.East;
                }
                break;

            case CompassDirection.East:
                if (command == MowerCommand.Move)
                {
                    mower.X = mower.X + 1;
                }

                else if (command == MowerCommand.Right)
                {
                    mower.Direction = CompassDirection.South;
                }

                else if (command == MowerCommand.Left)
                {
                    mower.Direction = CompassDirection.North;
                }
                break;

            case CompassDirection.West:
                if (command == MowerCommand.Move)
                {
                    mower.X = mower.X - 1;
                }
                else if (command == MowerCommand.Right)
                {
                    mower.Direction = CompassDirection.North;
                }

                else if (command == MowerCommand.Left)
                {
                    mower.Direction = CompassDirection.South;
                }
                break;

            default:
                throw new ArgumentException("Not a valid direction");
            }
        }
Esempio n. 6
0
        public static MowerInput ParseMowerInput(string gridDimensionsAndStartingDirection, string commands)
        {
            MowerInput parsedMoverInput = new MowerInput();
            int        temp;

            string[] data = gridDimensionsAndStartingDirection.Split(' ');
            if (data != null)
            {
                if (int.TryParse(data[0], out temp))
                {
                    parsedMoverInput.X = temp;
                }
                else
                {
                    throw new ArgumentException("Not an integer");
                }

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

                if (data[2] != null)
                {
                    string cdirection = data[2];
                    char   cc;
                    cc = cdirection.ToCharArray()[0];
                    parsedMoverInput.Direction = ParseCompassDirection(cc);
                }
                else
                {
                    throw new System.ArgumentOutOfRangeException("index parameter is out of range");
                }
            }
            else
            {
                throw new System.ArgumentException("Grid dimention cannot be null", "data");
            }

            List <MowerCommand> mowerCommands = new List <MowerCommand>();


            if (commands != null)
            {
                var chars = commands.ToCharArray();
                foreach (char c in chars)
                {
                    MowerCommand Commands = ParseMowerCommand(c);
                    mowerCommands.Add(Commands);
                }
            }
            else
            {
                throw new System.ArgumentException("Commands cannot be null", "data");
            }
            parsedMoverInput.Commands = mowerCommands;
            return(parsedMoverInput);
        }