private bool TryAddAllShips(IList <ShipType> ships, Predicate <CellPosition> canUseCell) { if (!ships.Any()) { return(true); } var ship = ships.Last(); ships.RemoveAt(ships.Count - 1); var availablePlaces = builder.EnumeratePositions() .SelectMany(position => new[] { new { Position = position, Vertical = true }, new { Position = position, Vertical = false } }) .Where(x => builder.CanBeAddedSafely(ship, x.Position, x.Vertical, canUseCell)) .Shuffle(); foreach (var place in availablePlaces) { builder.TryAddFullShip(ship, place.Position, place.Vertical); if (TryAddAllShips(ships, canUseCell)) { return(true); } builder.TryRemoveFullShip(ship, place.Position, place.Vertical); } ships.Add(ship); return(false); }
public void NotModifyBuilder_BeforeGenerating() { var oldBuilder = FromLines(Rules, SampleField); foreach (var position in builder.EnumeratePositions()) { builder[position].Should().Be(oldBuilder[position]); } }
private void FillSelfFieldUsingBuilder() { foreach (var position in builder.EnumeratePositions()) { selfFieldCells.GetValue(position).Fill = builder[position] ? SelfFieldUndamagedShipCellColor : SelfFieldUndamagedEmptyCellColor; } }