public bool TryGetSimpleCommand(string command, out BaseCommand returnCommand)
    {
        returnCommand = null;
        string[] arguments  = null;
        string   methodName = GetMethodSingature(command, ref arguments);

        CommandsMethods commandEnum = _availableCommands.FirstOrDefault(com => com.ToString() == methodName);

        if (commandEnum == 0)
        {
            return(false);
        }

        switch (commandEnum)
        {
        case (CommandsMethods.Forward):
            if (arguments == null || arguments.Length != 1 || arguments[0] != string.Empty)
            {
                return(false);
            }

            returnCommand = new MoveCommand(_implementator, MoveSides.FORWARD);
            return(true);

        case (CommandsMethods.Backward):
            if (arguments == null || arguments.Length != 1 || arguments[0] != string.Empty)
            {
                return(false);
            }

            returnCommand = new MoveCommand(_implementator, MoveSides.BACKWARD);
            return(true);

        case (CommandsMethods.Turn):
        {
            if (arguments == null || arguments.Length == 0)
            {
                return(false);
            }

            var turnSide = arguments[0].ToLower();

            if (turnSide == TurnArguments.Right.ToString().ToLower())
            {
                returnCommand = new RotateCommand(_implementator, TurnArguments.Right);
            }
            else if (turnSide == TurnArguments.Left.ToString().ToLower())
            {
                returnCommand = new RotateCommand(_implementator, TurnArguments.Left);
            }
            return(true);
        }

        case (CommandsMethods.Do):
        {
            int count = 0;
            if (arguments == null || arguments.Length == 0 || !int.TryParse(arguments[0], out count))
            {
                return(false);
            }

            returnCommand = new DoCommand(_implementator, count);
            return(true);
        }

        case (CommandsMethods.While):
        {
            if (arguments == null || arguments.Length == 0)
            {
                return(false);
            }

            WhileConditions argument = _allWhileConditions.FirstOrDefault(com => com.ToString() == arguments[0]);

            if (argument == 0)
            {
                return(false);
            }

            returnCommand = new WhileCommand(_implementator, argument);
            return(true);
        }
        }

        return(false);
    }
Beispiel #2
0
 public WhileCommand(Character implementator, WhileConditions condition) : base(implementator)
 {
     _currentCommandIndex = -1;
 }