/// <remarks> /// We should add ships without mutual overlay and without out of game board. /// Ships position should be maximally random. /// </remarks> private static bool TryGetShipToAdd(ShipClass shipClass, IEnumerable <Ship> existingShips, out Ship newShip) { foreach (Tuple <ShipCoordinate, ShipOrientation> item in GetRandomShipCoordinateAndOrientationInBorder(shipClass.Length)) { var newShipCandidate = Ship.ConstructShipWithCoordinate(shipClass, item.Item2, item.Item1); if (ShipAllocationLogic.TestNoOverlap(newShipCandidate, existingShips)) { newShip = newShipCandidate; return(true); } } newShip = null; return(false); }
public static Tuple <bool, Ship> TryConstructShipWithCoordinate( ShipClass shipClass, ShipOrientation orientation, ShipCoordinate headCoordinate ) { var cellsResult = ShipAllocationLogic.TryGetCellsOnBoard(headCoordinate, shipClass.Length, orientation); if (cellsResult.Item1) { return(new Tuple <bool, Ship>(true, new Ship(shipClass, orientation, headCoordinate, cellsResult.Item2))); } else { return(new Tuple <bool, Ship>(false, null)); } }
public Ship( ShipClass shipClass, ShipOrientation orientation, ShipCoordinate headCoordinate, List <ShipCoordinate> cells ) { ShipClass = shipClass ?? throw new ArgumentNullException(nameof(shipClass)); Orientation = orientation; HeadCoordinate = headCoordinate ?? throw new ArgumentNullException(nameof(headCoordinate)); if (!ShipAllocationLogic.ValidateShipBarier(headCoordinate, shipClass.Length, orientation)) { throw new ArgumentException("Ship out of the board."); } Cells = cells.Select(item => new ShipCell(item)).ToList(); }
private static IEnumerable <Tuple <ShipCoordinate, ShipOrientation> > GetRandomShipCoordinateAndOrientationInBorder(int shipLength) { return(GetRandomShipCoordinateAndOrientation().Where(item => ShipAllocationLogic.ValidateShipBarier(item.Item1, shipLength, item.Item2))); }