Example #1
0
        public bool CanPlace(Point point, Direction direction, PlayerMap playerMap)
        {
            var startCell = playerMap.GetCellAtPoint(point);

            var length = 0;
            var cell   = startCell;

            while (length < _cells.Length)
            {
                if (!cell.CanPlace())
                {
                    return(false);
                }

                length++;
                if (!cell.HasNeighbour(direction))
                {
                    break;
                }

                cell = cell.Neighbour(direction);
            }

            return(length == _cells.Length);
        }
Example #2
0
        public void Place(Point point, Direction direction, PlayerMap playerMap)
        {
            var startCell = playerMap.GetCellAtPoint(point);

            var cells = new List <Cell>();

            try
            {
                var length = 0;
                var cell   = startCell;
                while (length < _cells.Length)
                {
                    cells.Add(cell);
                    cell.Place(direction, this);
                    _cells[length] = cell;
                    cell           = cell.Neighbour(direction);
                    length++;
                }

                Placed = true;
            }
            catch
            {
                cells.ForEach(x => x.OccupiedBy = null);
                throw;
            }
        }
Example #3
0
        public void Place(Point point, Direction direction, PlayerMap playerMap)
        {
            var startCell = playerMap.GetCellAtPoint(point);

            var cells = new List <Cell>();

            try
            {
                var length = 0;
                var cell   = startCell;
                while (length < _cells.Length)
                {
                    cells.Add(cell);
                    cell.Place(direction, this);
                    _cells[length] = cell;

                    if (!cell.HasNeighbour(direction))
                    {
                        break;
                    }

                    cell = cell.Neighbour(direction);
                    length++;
                }

                if (length < _cells.Length - 1)
                {
                    throw new InvalidOperationException(
                              $"There are not enough cells left in the {direction} direction to finish placing the {ShipType}");
                }

                Placed = true;
            }
            catch
            {
                cells.ForEach(x => x.OccupiedBy = null);
                throw;
            }
        }