public void Handle(GridAndRoverMovementsCommand command)
 {
     foreach (var roverCommands in command.RoverMovementCommands.OrderBy(x => x.RoverCreationOrder))
     {
         foreach (var roverMovementCommand in roverCommands.RoverMovementCommands.OrderBy(
                      x => x.ActionExecutionOrder))
         {
             var rover = _roverProvider.GetRoverStateFor(roverCommands.RoverId);
             var roverStateChangeCommand =
                 _roverActonToCoordinateTranslationService.Translate(rover, roverMovementCommand);
             try
             {
                 _marsRoverGrid.MoveRoverTo(rover, roverStateChangeCommand.YCoordinate,
                                            roverStateChangeCommand.XCoordinate);
                 _roverProvider.SaveRoverMovement(rover.RoverId, roverStateChangeCommand);
             }
             catch (RoverWillDriveOffGridException)
             {
                 _roverProvider.UpdateRoverStatus(roverCommands.RoverId, RoverStatus.HelpRequired);
             }
             catch (GridSpaceOccupiedException)
             {
                 _roverProvider.UpdateRoverStatus(roverCommands.RoverId, RoverStatus.HelpRequired);
             }
         }
     }
 }
Beispiel #2
0
        public GridAndRoverMovementsCommand CreateCommandsFrom(string roverCommand)
        {
            var result = new GridAndRoverMovementsCommand();
            var movementCommandsIncludingHeaderArray = ValidateCommandStructure(roverCommand);
            var gridSizeArray = movementCommandsIncludingHeaderArray[0].Split(' ');

            CreateRoverGridSizeCommand(gridSizeArray, result);
            CreateRoverStartPositionAndMovementCommands(movementCommandsIncludingHeaderArray, result);
            return(result);
        }
Beispiel #3
0
        private static void CreateRoverStartPositionAndMovementCommands(string[] movementCommandsIncludingHeaderArray,
                                                                        GridAndRoverMovementsCommand result)
        {
            var roverCreationOrder = 1;

            for (var i = 1; i < movementCommandsIncludingHeaderArray.Length; i += 2)
            {
                CreateRoverStartPositionAndMovementCommand(movementCommandsIncludingHeaderArray, i, result, roverCreationOrder);
                roverCreationOrder++;
            }
        }
Beispiel #4
0
        private static void CreateRoverStartPositionAndMovementCommand(string[] movementCommandsIncludingHeaderArray,
                                                                       int i,
                                                                       GridAndRoverMovementsCommand result,
                                                                       int roverCreationOrder)
        {
            var roverStartPositionCommand = RoverStartPositionCommand(movementCommandsIncludingHeaderArray, i);
            var roverActionCommands       = MovementCharsCommand(movementCommandsIncludingHeaderArray, i);

            result.RoverMovementCommands.Add(
                new RoverStartPositionAndMovementCommands
            {
                RoverId = Guid.NewGuid(),
                RoverStartPositionCommand = roverStartPositionCommand,
                RoverMovementCommands     = roverActionCommands,
                RoverCreationOrder        = roverCreationOrder
            });
        }
Beispiel #5
0
        private static void CreateRoverGridSizeCommand(string[] gridSizeArray, GridAndRoverMovementsCommand result)
        {
            var coordinatesHolder = 0;

            if (!int.TryParse(gridSizeArray[0], out coordinatesHolder))
            {
                throw new InvalidCommandStructureException(
                          "The xCoordinate of the grid structure is not a valid integer.");
            }

            result.GridSize.Y = coordinatesHolder;

            if (!int.TryParse(gridSizeArray[1], out coordinatesHolder))
            {
                throw new InvalidCommandStructureException(
                          "The xCoordinate of the grid structure is not a valid integer.");
            }

            result.GridSize.X = coordinatesHolder;
        }
 public void Handle(GridAndRoverMovementsCommand command)
 {
     _marsRoverGrid.SetMarsRoverGridBoundary(command.GridSize.Y, command.GridSize.X);
 }