internal ChessBoardPosition(char horizontal, int vertical) { if (PositionValidator.ValidatePosition(horizontal, vertical) == false) { throw new PositionOutOfBoardException($"Positions are out of the board! Horiontal : {horizontal} Vertical : {vertical}"); } this.Horizontal = horizontal; this.Vertical = vertical; }
public MarsRoversResource CalculateRoversPosition(MarsRoversModel model) { var result = new MarsRoversResource(); var rovers = new Dictionary <int, Rover>(); var directions = EnumDescriptionHelper.GetEnumDescriptions <Direction>().ToList(); var commands = EnumDescriptionHelper.GetEnumDescriptions <RoverCommand>().ToList(); var index = 0; try { // InitializeRovers foreach (var roverModel in model.Rovers) { index++; rovers.Add(index, InitializeRover(index, roverModel, directions, commands)); } ; // Validate starting positions foreach (var rover in rovers) { PositionValidator.ValidatePosition(rover.Value, rover.Value.CurrentPosition, rovers, model.PlateauX, model.PlateauY, true); } // ProcessRovers var deployedRovers = rovers.Where(x => x.Value.Deployed).ToDictionary(x => x.Key, x => x.Value);; foreach (var rover in deployedRovers) { ProcessRoverCommands(rover.Value, deployedRovers, model.PlateauX, model.PlateauY); } // Map results result = MapRoversToResource(rovers); } catch (Exception ex) { _logger.LogError(ex, ex.Message); throw; } return(result); }
internal void ProcessRoverCommands(Rover rover, Dictionary <int, Rover> rovers, int horizontalSize, int verticalSize) { var commandFactory = new CommandFactory(); foreach (var command in rover.Commands) { if (!rover.Processed) { var nextPosition = commandFactory.ProcessCommand(command, rover.CurrentPosition); if (command == RoverCommand.Move) { PositionValidator.ValidatePosition(rover, nextPosition, rovers, horizontalSize, verticalSize); } if (!rover.Processed) { rover.CurrentPosition = nextPosition; } } } rover.Processed = true; }