Пример #1
0
        public void MoveWheels(RoverCommand command)
        {
            int x = _commandableRover.CurrentXPos;

            if (command.Rotation == RoverRotates.Left)
            {
                x = _commandableRover.CurrentXPos + command.Steps;
                _commandableRover.SetPosition(x, _commandableRover.CurrentYPos, RoverDirections.East);
            }
            else if (command.Rotation == RoverRotates.Right)
            {
                x = _commandableRover.CurrentXPos - command.Steps;
                _commandableRover.SetPosition(x, _commandableRover.CurrentYPos, RoverDirections.West);
            }
            else
            {
                throw new ApplicationException($"Southbound rover unable move {command.Steps} steps towards {command.Rotation}");
            }
        }
Пример #2
0
        private IList <RoverCommand> ParseCommands(string commands)
        {
            List <RoverCommand> roverCommands = new List <RoverCommand>();
            string stepInCommand = string.Empty;

            for (int i = 0; i < commands.Length; i++)
            {
                char character = commands[i];
                if (character == (char)RoverRotates.Left || character == (char)RoverRotates.Right)
                {
                    if (roverCommands.Count > 0 && !string.IsNullOrEmpty(stepInCommand))
                    {
                        roverCommands.Last().Steps = int.Parse(stepInCommand);
                        stepInCommand = string.Empty;
                    }

                    var roverCommand = new RoverCommand {
                        Rotation = (RoverRotates)Enum.ToObject(typeof(RoverRotates), character)
                    };
                    roverCommands.Add(roverCommand);
                    continue;
                }

                if (char.IsDigit(character))
                {
                    stepInCommand = string.Concat(stepInCommand, character);
                }
            }

            if (roverCommands.Count > 0 && !string.IsNullOrEmpty(stepInCommand))
            {
                roverCommands.Last().Steps = int.Parse(stepInCommand);
            }

            return(roverCommands);
        }