示例#1
0
        public InputInfo GetValidatedCommand(string cmdStr, ref bool isFirstCmd)
        {
            // Convert cmdVal to InputInfo for easier binding
            var result = new InputInfo();

            // Validate Command Keyword
            if (String.IsNullOrEmpty(cmdStr)) throw new Exception("Invalid command. Please use:\n PLACE X,Y,F \n MOVE \n LEFT \n RIGHT \n REPORT");

            // Parse the cmdStr
            var inVals = cmdStr.Trim().ToUpper().Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);

            // Validate command key
            if (!Enum.IsDefined(typeof(Commands), inVals[0])) throw new Exception("Invalid command. Please use any of the keywords: PLACE, MOVE, LEFT, RIGHT, REPORT");

            // Return a valid Commands Enum
            result.CmdKey = (Commands)Enum.Parse(typeof(Commands), inVals[0]);

            // Check a valid first command
            if (isFirstCmd && result.CmdKey != Commands.PLACE) throw new Exception("Invalid first command. Please start with PLACE X,Y,F");

            // To check consecutive PLACE command any times after the very first entry is done
            if (result.CmdKey == Commands.PLACE)
            {
                // validate cmd
                if (inVals.Length != 4) throw new Exception("Invalid PLACE command, Please start with PLACE X, Y, F");
                // validate X value
                int xval = 0;
                if (!int.TryParse(inVals[1], out xval)) throw new Exception("X value must be a number");
                if (xval < minVal || xval > maxVal) throw new Exception("X value must be between " + minVal + " and " + maxVal);
                // validate Y value
                int yval = 0;
                if (!int.TryParse(inVals[2], out yval)) throw new Exception("Y value must be a number");
                if (yval < minVal || yval > maxVal) throw new Exception("Y value must be between " + minVal + " and " + maxVal);
                // validate F value
                if (!Enum.IsDefined(typeof(Directions), inVals[3])) throw new Exception("F value must be of " + string.Join(", ", Enum.GetNames(typeof(Directions))));

                result.XVal = xval;
                result.YVal = yval;
                result.FVal = (Directions)Enum.Parse(typeof(Directions), inVals[3]);

                // If all passed then set it to false
                if(isFirstCmd)
                    isFirstCmd = false;
            }

            return result;
        }
示例#2
0
        private RobotPosition Turning(InputInfo inputInfo, Turns turnTo)
        {
            // get currently facing configs
            var conf = FindPlacementConfig(_robotAt.Facing);
            // Get the current facing value and +1 to it
            var newFacingVal = (int)conf.FacingVal + (int)turnTo;

            // Check for valid turn since can keep turning the same direction 360 again and again
            // if keep turing RIGHT +1
            if (newFacingVal > 4)
                newFacingVal = 1;
            // if keep turning LEFT -1
            if (newFacingVal <= 0)
                newFacingVal = 4;

            // Get the Directions of newFacingVal
            string newDirectionName = Enum.GetName(typeof(Directions), newFacingVal);
            Directions newDirection;
            Enum.TryParse(newDirectionName, out newDirection);

            // Update the current Robot facing value
            _robotAt.Facing = newDirection;
            return _robotAt;
        }
示例#3
0
        private RobotPosition Move(InputInfo inputInfo)
        {
            var conf = FindPlacementConfig(_robotAt.Facing);
            // Validate the new coordinates before assigning to RoboNow
            var newX = _robotAt.XVal + conf.XVal;
            var newY = _robotAt.YVal + conf.YVal;
            if (IsValidXY(newX, newY))
            {
                // Assign new values to RoboNow
                _robotAt.XVal = newX;
                _robotAt.YVal = newY;
            }

            return _robotAt;
        }
示例#4
0
        private RobotPosition Place(InputInfo inputInfo)
        {
            if (_robotAt == null) throw new Exception("Robot hasnt been initialize yet.");
            _robotAt.XVal = inputInfo.XVal.GetValueOrDefault();
            _robotAt.YVal = inputInfo.YVal.GetValueOrDefault();
            _robotAt.Facing = inputInfo.FVal;

            return _robotAt;
        }
示例#5
0
        public string PerformAction(InputInfo inputInfo)
        {
            switch (inputInfo.CmdKey)
            {
                case Commands.PLACE:
                    _robotAt = Place(inputInfo);
                    break;
                case Commands.MOVE:
                    _robotAt = Move(inputInfo);
                    break;
                case Commands.LEFT:
                    _robotAt = Turning(inputInfo, Turns.Left);
                    break;
                case Commands.RIGHT:
                    _robotAt = Turning(inputInfo, Turns.Right);
                    break;
                case Commands.REPORT:
                    return GetRobotAt(_robotAt);
                    break;
                default:
                    return("Oops something gone wrong!");
                    break;
            }

            return string.Empty;
        }