Пример #1
0
        /// <summary>
        /// This method checks if the command line has a valid insert command at the valid position.
        /// </summary>
        /// <param name="commandLine">The command line the user has written.</param>
        /// <exception cref="ArgumentNullException">
        /// If the commandLine is null.
        /// </exception>
        /// <returns>An instanced insert command if the command is valid or null if the command is not valid.</returns>
        public static IEditorCommand Parse(string commandLine)
        {
            if (commandLine == null)
            {
                throw new ArgumentNullException();
            }

            string[] possibleCommands = commandLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (possibleCommands.Length < 3)
            {
                return(null);
            }

            string value = possibleCommands[1];
            int    editorValue;

            try
            {
                editorValue = int.Parse(value);
            }
            catch
            {
                return(null);
            }

            string[] newpossibleCommands = new string[possibleCommands.Length - 1];
            newpossibleCommands[0] = possibleCommands[0];

            if (newpossibleCommands.Length == 2)
            {
                newpossibleCommands[1] = possibleCommands[2];
            }
            else if (newpossibleCommands.Length == 3)
            {
                newpossibleCommands[1] = possibleCommands[2];
                newpossibleCommands[2] = possibleCommands[3];
            }

            commandLine = string.Join(" ", newpossibleCommands);
            string         command       = newpossibleCommands[1].ToLower();
            ITurtleCommand turtleCommand = null;

            switch (command)
            {
            case "move":
                turtleCommand = MoveCommand.Parse(commandLine);
                break;

            case "rotate":
                turtleCommand = RotateCommand.Parse(commandLine);
                break;

            case "sleep":
                turtleCommand = SleepCommand.Parse(commandLine);
                break;

            case "penup":
                turtleCommand = PenUpCommand.Parse(commandLine);
                break;

            case "pendown":
                turtleCommand = PenDownCommand.Parse(commandLine);
                break;

            case "changecolor":
                turtleCommand = ChangeColorCommand.Parse(commandLine);
                break;

            case "changetracksymbol":
                turtleCommand = ChangeTrackSymbolCommand.Parse(commandLine);
                break;

            case "changeturtlesymbol":
                turtleCommand = ChangeTurtleSymbolCommand.Parse(commandLine);
                break;
            }

            if (turtleCommand != null && editorValue > 0)
            {
                turtleValue = turtleCommand.GetValue();
                return(new InsertCommand(editorValue, turtleCommand));
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// This method checks if the command line has a valid add command at the valid position.
        /// </summary>
        /// <param name="commandLine">The command line the user has written.</param>
        /// <exception cref="ArgumentNullException">
        /// If the commandLine is null.
        /// </exception>
        /// <returns>An instanced add command if the command is valid or null if the command is not valid.</returns>
        public static IEditorCommand Parse(string commandLine)
        {
            if (commandLine == null)
            {
                throw new ArgumentNullException();
            }

            string[] possibleCommands = commandLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (possibleCommands.Length <= 1 || possibleCommands.Length >= 4)
            {
                return(null);
            }

            string         command       = possibleCommands[1].ToLower();
            ITurtleCommand turtleCommand = null;

            switch (command)
            {
            case "move":
                turtleCommand = MoveCommand.Parse(commandLine);
                break;

            case "rotate":
                turtleCommand = RotateCommand.Parse(commandLine);
                break;

            case "sleep":
                turtleCommand = SleepCommand.Parse(commandLine);
                break;

            case "penup":
                turtleCommand = PenUpCommand.Parse(commandLine);
                break;

            case "pendown":
                turtleCommand = PenDownCommand.Parse(commandLine);
                break;

            case "changecolor":
                turtleCommand = ChangeColorCommand.Parse(commandLine);
                break;

            case "changetracksymbol":
                turtleCommand = ChangeTrackSymbolCommand.Parse(commandLine);
                break;

            case "changeturtlesymbol":
                turtleCommand = ChangeTurtleSymbolCommand.Parse(commandLine);
                break;
            }

            if (turtleCommand != null)
            {
                return(new AddCommand(turtleCommand));
            }
            else
            {
                return(null);
            }
        }