private bool Move(LocationCoordinate newLocationCoordinate) { var newXCoordinate = currentLocationCoordinate.X + newLocationCoordinate.X; var newYCoordinate = currentLocationCoordinate.Y + newLocationCoordinate.Y; var newCoordinate = new LocationCoordinate { X = newXCoordinate, Y = newYCoordinate }; if (newXCoordinate < grid.LowerLeftLocationCoordinate.X || newXCoordinate > grid.UpperRightLocationCoordinate.X || newYCoordinate < grid.LowerLeftLocationCoordinate.Y || newYCoordinate > grid.UpperRightLocationCoordinate.Y) { throw new IllegalMoveLocation($"{Name} cannot be moved to location: {newCoordinate} with defined grid: {grid} - aborting {Name}"); } if (grid.CheckForCollisions && rovers.Where(r => r.Name != this.Name).Any(r => r.PositionWithDirection.LocationCoordinate.ToString() == newCoordinate.ToString())) { throw new IllegalMoveLocationCoordinateAlreadyOccupied( $"{Name} cannot be moved to location: {newCoordinate} as the move location already contains another Rover"); } currentLocationCoordinate = newCoordinate; return(true); }
public Grid(LocationCoordinate gridUpperRightLocationCoordinate, bool checkForCollisions = false) { if (gridUpperRightLocationCoordinate == null || gridUpperRightLocationCoordinate.X != gridUpperRightLocationCoordinate.Y || gridUpperRightLocationCoordinate.X <= 0) { throw new ArgumentException("Grid X, Y upper right coordinates must be equal and greater than 0 - aborting"); } UpperRightLocationCoordinate = gridUpperRightLocationCoordinate; CheckForCollisions = checkForCollisions; }
public Rover(LocationCoordinate startLocationCoordinate, CompassDirection compassStartDirection, IGrid grid) { if (startLocationCoordinate == null || startLocationCoordinate.X < 0 || startLocationCoordinate.Y < 0) { throw new ArgumentException($"Rover X, Y start coordinates must be greater than or equal to 0 - aborting {Name}"); } this.grid = grid ?? throw new ArgumentException($"Rover grid argument cannot be null - aborting {Name}"); Name = $"Rover {Count + 1}"; currentLocationCoordinate = startLocationCoordinate; currentCompassDirection = compassStartDirection; PositionWithDirection = new PositionWithDirection { CompassDirection = currentCompassDirection, LocationCoordinate = currentLocationCoordinate }; rovers.Add(this); }