//parsing the command into argument and command then return the instruction
        private Instruction getInstruction(string command, ref InstructionArguments args)
        {
            Instruction result;
            int         argstringposindex = command.IndexOf(" ");
            string      argstring         = "";

            if (argstringposindex > 0)
            {
                //split the command name and paramaeter
                argstring = command.Substring(argstringposindex + 1);
                command   = command.Substring(0, argstringposindex);
            }
            command = command.ToUpper();
            //parse the command
            if (Enum.TryParse(command, true, out result))
            {
                if (result == Instruction.Place)
                {
                    //parse the place command arguments
                    if (!TryParsePlaceArgs(argstring, ref args))
                    {
                        result = Instruction.Invalid;
                    }
                }
            }
            else
            {
                result = Instruction.Invalid;
            }

            return(result);
        }
Exemple #2
0
        private Instruction GetInstruction(string command, ref InstructionArguments args)

        {
            Instruction result;
            string      argString = "";

            int argsSeperatorPosition = command.IndexOf(" ");

            if (argsSeperatorPosition > 0)
            {
                argString = command.Substring(argsSeperatorPosition + 1);
                command   = command.Substring(0, argsSeperatorPosition);
            }
            command = command.ToUpper();

            if (!Enum.TryParse <Instruction>(command, true, out result))
            {
                result = Instruction.Invalid;
            }
            else
            {
                if (result == Instruction.Place)
                {
                    if (!TryParsePlaceArgs(argString, ref args))
                    {
                        result = Instruction.Invalid;
                    }
                }
            }
            return(result);
        }
Exemple #3
0
        public string Command(string command)
        {
            string response           = "";
            InstructionArguments args = null;
            var instruction           = (command.ToLower().Contains("output") == true)? Instruction.Output: GetInstruction(command, ref args);

            switch (instruction)
            {
            case Instruction.Place:
                var placeArgs = (PlaceInstructionArguments)args;
                if (!Robot.Place(placeArgs.X, placeArgs.Y, placeArgs.Facing))
                {
                    command = Robot.LastError;
                }

                break;

            case Instruction.Move:
                if (!Robot.Move())
                {
                    command = Robot.LastError;
                }

                break;

            case Instruction.Left:
                if (!Robot.Left())
                {
                    command = Robot.LastError;
                }

                break;

            case Instruction.Right:
                if (!Robot.Right())
                {
                    command = Robot.LastError;
                }

                break;

            case Instruction.Report:
                command = command + "\n Output : " + Robot.Report();
                break;

            case Instruction.Output:
                command = "";
                break;

            default:
                command = "Invalid command.";
                break;
            }
            return(command);
        }
Exemple #4
0
        public Command GetInstruction(string line)
        {
            Command command        = new Command();
            string  argumentString = "";

            int argumentSeperatorPosition = line.IndexOf(" ");

            if (argumentSeperatorPosition > 0)
            {
                argumentString = line.Substring(argumentSeperatorPosition + 1);
                line           = line.Substring(0, argumentSeperatorPosition);
            }
            line = line.ToUpper();

            Instruction instruction;

            if (Enum.TryParse(line, true, out instruction))
            {
                if (instruction == Instruction.Place)
                {
                    InstructionArguments arguments = new InstructionArguments();
                    if (!ParserInstructionUtility.TryParsePlaceArgs(argumentString, ref arguments))
                    {
                        command.Instruction = Instruction.Invalid;
                    }
                    else
                    {
                        command.Instruction          = instruction;
                        command.InstructionArguments = arguments;
                    }
                }
                else
                {
                    command.Instruction = instruction;
                }
            }
            else
            {
                command.Instruction = Instruction.Invalid;
            }
            return(command);
        }
Exemple #5
0
        private bool TryParsePlaceArgs(string argString, ref InstructionArguments args)
        {
            var    argParts = argString.Split(',');
            int    x, y;
            Facing facing;

            if (argParts.Length == 3 &&
                TryGetCoordinate(argParts[0], out x) &&
                TryGetCoordinate(argParts[1], out y) &&
                TryGetFacingDirection(argParts[2], out facing))
            {
                args = new PlaceInstructionArguments
                {
                    X      = x,
                    Y      = y,
                    Facing = facing,
                };
                return(true);
            }
            return(false);
        }
Exemple #6
0
        public static bool TryParsePlaceArgs(string argumentString, ref InstructionArguments arguments)
        {
            var         argumentParts = argumentString.Split(',');
            int         x;
            int         y;
            Orientation facing;

            if (argumentParts.Length == 3 &&
                TryGetCoordinate(argumentParts[0], out x) &&
                TryGetCoordinate(argumentParts[1], out y) &&
                TryGetFacingDirection(argumentParts[2], out facing))
            {
                arguments = new InstructionArguments
                {
                    X      = x,
                    Y      = y,
                    Facing = facing,
                };

                return(true);
            }

            return(false);
        }
Exemple #7
0
        public string Execute(string command)
        {
            string response           = "";
            InstructionArguments args = null;
            var instruction           = GetInstruction(command, ref args);

            switch (instruction)
            {
            case Instruction.PLACE:
                var placeArgs = (PlaceInstructionArguments)args;
                if (_robot.Place(placeArgs.X, placeArgs.Y, placeArgs.Facing))
                {
                    response = "Success";
                }
                else
                {
                    response = _robot.LastError;
                }
                break;

            case Instruction.MOVE:
                if (_robot.Move())
                {
                    response = "Done.";
                }
                else
                {
                    response = _robot.LastError;
                }
                break;

            case Instruction.LEFT:
                if (_robot.Left())
                {
                    response = "Done.";
                }
                else
                {
                    response = _robot.LastError;
                }
                break;

            case Instruction.RIGHT:
                if (_robot.Right())
                {
                    response = "Done.";
                }
                else
                {
                    response = _robot.LastError;
                }
                break;

            case Instruction.REPORT:
                response = _robot.Report();
                break;

            default:
                response = "Invalid command.";
                break;
            }
            return(response);
        }
Exemple #8
0
        public string Command(string command)
        {
            string response           = "";
            InstructionArguments args = null;
            var instruction           = GetInstruction(command, ref args);

            switch (instruction)
            {
            case Instruction.Place:
                var placeArgs = (PlaceInstructionArguments)args;
                if (Turtle.Place(placeArgs.X, placeArgs.Y, placeArgs.Facing))
                {
                    response = "Command Executed.";
                }
                else
                {
                    response = Turtle.LastError;
                }
                break;

            case Instruction.Move:
                if (Turtle.Move())
                {
                    response = "Command Executed.";
                }
                else
                {
                    response = Turtle.LastError;
                }
                break;

            case Instruction.Left:
                if (Turtle.Left())
                {
                    response = "Command Executed.";
                }
                else
                {
                    response = Turtle.LastError;
                }
                break;

            case Instruction.Right:
                if (Turtle.Right())
                {
                    response = "Command Executed.";
                }
                else
                {
                    response = Turtle.LastError;
                }
                break;

            case Instruction.Report:
                response = Turtle.Report();
                break;

            default:
                response = "Invalid command.";
                break;
            }
            return(response);
        }
Exemple #9
0
        private Instruction GetInstruction(string command, ref InstructionArguments args)
        {
            Instruction result;
            string argString = "";

            int argsSeperatorPosition = command.IndexOf(" ");
            if (argsSeperatorPosition > 0)
            {
                argString = command.Substring(argsSeperatorPosition + 1);
                command = command.Substring(0, argsSeperatorPosition);
            }
            command = command.ToUpper();

            if (Enum.TryParse<Instruction>(command, true, out result))
            {
                if (result == Instruction.Place)
                {
                    if (!TryParsePlaceArgs(argString, ref args))
                    {
                        result = Instruction.Invalid;
                    }
                }
            }
            else
            {
                result = Instruction.Invalid;
            }
            return result;
        }
Exemple #10
0
        private bool TryParsePlaceArgs(string argString, ref InstructionArguments args)
        {
            var argParts = argString.Split(',');
            int x, y;
            Facing facing;

            if (argParts.Length == 3 &&
                TryGetCoordinate(argParts[0], out x) &&
                TryGetCoordinate(argParts[1], out y) &&
                TryGetFacingDirection(argParts[2], out facing))
            {
                args = new PlaceInstructionArguments
                {
                    X = x,
                    Y = y,
                    Facing = facing,
                };
                return true;
            }
            return false;
        }
        //execute command for robot
        public string executecommand(string command)
        {
            string response           = "";
            InstructionArguments args = null;
            //get instruction name from command and arguments to check valid instruction has been placed.
            var instruction = getInstruction(command, ref args);

            switch (instruction)
            {
            case Instruction.Place:
                var placearg = (PlaceInstructionArguments)args;
                if (this.Robot.place(placearg.X, placearg.Y, placearg.Facing))
                {
                    response = "Done.";
                }
                else
                {
                    response = Robot.LastError;
                }
                break;

            case Instruction.Move:
                if (this.Robot.move())
                {
                    response = "Done.";
                }
                else
                {
                    response = Robot.LastError;
                }
                break;

            case Instruction.Left:
                if (this.Robot.left())
                {
                    response = "Done.";
                }
                else
                {
                    response = Robot.LastError;
                }
                break;

            case Instruction.Right:
                if (this.Robot.right())
                {
                    response = "Done.";
                }
                else
                {
                    response = Robot.LastError;
                }
                break;

            case Instruction.Report:
                response = Robot.Report();
                break;

            default:
                response = "Invalid command";
                break;
            }
            return(response);
        }