コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InsertCommand"/> class.
        /// </summary>
        /// <param name="editorValue">The position at where the turtle command should be inserted.</param>
        /// <param name="turtleCommand">The specified turtle command that should be inserted.</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If the editor value is less than zero.
        /// </exception>
        public InsertCommand(int editorValue, ITurtleCommand turtleCommand)
        {
            if (editorValue < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            this.editorValue   = editorValue;
            this.TurtleCommand = turtleCommand;
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AddCommand"/> class.
        /// </summary>
        /// <param name="turtleCommand">The turtle command that should be added to the last added turtle.</param>
        /// /// <exception cref="ArgumentNullException">
        /// If the turtle command is null.
        /// </exception>
        public AddCommand(ITurtleCommand turtleCommand)
        {
            if (turtleCommand == null)
            {
                throw new ArgumentNullException();
            }

            this.TurtleValue   = turtleCommand.GetValue();
            this.TurtleCommand = turtleCommand;
        }
コード例 #3
0
        public TurtleState Evaluate(ITurtleCommand command)
        {
            var currentState = CheckTurtleState();

            if (currentState != TurtleState.InDanger)
            {
                return(currentState);
            }

            command.Execute(_board.Turtle);
            return(CheckTurtleState());
        }
コード例 #4
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);
            }
        }
コード例 #5
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);
            }
        }