/// <summary>
        /// Parses the raw instructions of the command.
        /// </summary>
        /// <param name="instructions">Raw instructions of the command.</param>
        protected override void ParseInstructions(string instructions)
        {
            string[] values;
            var      count = 3;
            var      x     = 0;
            var      y     = 0;

            try
            {
                values = InstructionHelper.SplitLine(instructions, count);
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"Expected {count} values for the confirm position command.", nameof(instructions), ex);
            }

            try
            {
                x = InstructionHelper.ConvertToInt(values[0]);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("X axis position for the confirm position command could not be converted to an integer.", nameof(instructions), ex);
            }

            try
            {
                y = InstructionHelper.ConvertToInt(values[1]);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Y axis position for the confirm position command could not be converted to an integer.", nameof(instructions), ex);
            }

            this.Coordinates = new Point
            {
                X = x,
                Y = y
            };

            if (x < 0)
            {
                throw new ArgumentException("X axis position for the confirm position command cannot be less than zero.", nameof(instructions));
            }

            if (y < 0)
            {
                throw new ArgumentException("Y axis position for the confirm position command cannot be less than zero.", nameof(instructions));
            }

            try
            {
                this.Heading = InstructionHelper.ConvertStringToHeading(values[2]);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Heading for the confirm position command could not be converted to a string.", nameof(instructions), ex);
            }
        }
예제 #2
0
        /// <summary>
        /// Parses the raw instructions of the command.
        /// </summary>
        /// <param name="instructions">Raw instructions of the command.</param>
        protected override void ParseInstructions(string instructions)
        {
            string[] values;
            var      count = 2;
            var      w     = 0;
            var      h     = 0;

            try
            {
                values = InstructionHelper.SplitLine(instructions, count);
            }
            catch (Exception ex)
            {
                throw new ArgumentException($"Expected {count} values for the establish grid command.", nameof(instructions), ex);
            }

            try
            {
                w = InstructionHelper.ConvertToInt(values[0]);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("X axis length for the establish grid command could not be converted to an integer.", nameof(instructions), ex);
            }

            try
            {
                h = InstructionHelper.ConvertToInt(values[1]);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Y axis length for the establish grid command could not be converted to an integer.", nameof(instructions), ex);
            }

            if (w < 0)
            {
                throw new ArgumentException("X axis length for the establish grid command cannot be less than zero.", nameof(instructions));
            }

            if (h < 0)
            {
                throw new ArgumentException("Y axis length for the establish grid command cannot be less than zero.", nameof(instructions));
            }

            this.Grid = new Size
            {
                Width  = w,
                Height = h
            };
        }
        /// <summary>
        /// Dispatches commands to be executed.
        /// </summary>
        /// <param name="commandSet">Command set to dispatch.</param>
        public void Dispatch(CommandSet commandSet)
        {
            if (commandSet == null)
            {
                throw new ArgumentNullException(nameof(commandSet));
            }

            this.guidance.Move(commandSet);

            this.listener?.Transmit($"{this.guidance.Coordinates.X} {this.guidance.Coordinates.Y} {InstructionHelper.ConvertHeadingToString(this.guidance.Heading)}");
        }