Esempio n. 1
0
        private IEnumerable <Tuple <ShipType, CellPosition, bool> > GenerateContinuesForDamagedShip(
            IList <CellPosition> damagedShipCells, IGameFieldBuilder builder, bool vertical, ShipType ship)
        {
            if (builder.ShipsLeft[ship] == 0)
            {
                yield break;
            }

            var topLeftCell = damagedShipCells.Min();
            var delta       = vertical ? CellPosition.DeltaDown : CellPosition.DeltaRight;

            var start = vertical
                ? new CellPosition(0, topLeftCell.Column)
                : new CellPosition(topLeftCell.Row, 0);

            for (; builder.Contains(start); start += delta)
            {
                if (!builder.CanBeAddedSafely(ship, start, vertical, x => OpponentFieldKnowledge[x] != false))
                {
                    continue;
                }
                var newShipCells = Enumerable.Range(0, ship.GetLength()).Select(x => start + delta * x).ToList();
                if (damagedShipCells.Any(x => !newShipCells.Contains(x)))
                {
                    continue;
                }
                yield return(Tuple.Create(ship, start, vertical));
            }
        }
Esempio n. 2
0
        private static IEnumerable <CellPosition> GenerateShipCells(
            ShipType ship, CellPosition start, bool vertical)
        {
            var delta = vertical ? CellPosition.DeltaDown : CellPosition.DeltaRight;

            for (var i = 0; i < ship.GetLength(); i++)
            {
                yield return(start + delta * i);
            }
        }