예제 #1
0
        private Instruction GetInstruction(string command, ref PlaceInstructionArguments 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);
        }
예제 #2
0
        private bool TryParsePlaceArgs(string argString, ref PlaceInstructionArguments 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);
        }
예제 #3
0
        public string Command(string command)
        {
            string response = "";
            PlaceInstructionArguments 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 = "Done.";
                }
                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);
        }