public void ParseMowerCommand_WhenPassingValidCommand()
        {
            char         ch         = 'M';
            MowerInput   mowerInput = new MowerInput();
            MowerCommand command    = MowerInput.ParseMowerCommand(ch);

            Assert.AreEqual(MowerCommand.Move, command);
        }
Beispiel #2
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");
            }
        }
Beispiel #3
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);
        }