Ejemplo n.º 1
0
 public Ship(eShipType shipType, eShipOrientation shipOrientation, int id)
 {
     _shipType        = shipType;
     _shipOrientation = shipOrientation;
     _length          = (int)_shipType;
     _health          = _length;
     _shipId          = id;
     _cells           = new List <ICell>();
 }
Ejemplo n.º 2
0
        public bool PlaceShip(eShipType shipType, eShipOrientation shipOrientation, int startX, int startY)
        {
            if (_shipTypes.ContainsKey(shipType) && _ships.Count(x => x.GetShipType() == shipType) < _shipTypes[shipType])
            {
                var shipid        = _randomizer.Next(1, 100);
                var ship          = new Ship(shipType, shipOrientation, shipid);
                var shipTempCells = new List <ICell>();
                for (int i = 0; i < ship.GetShipLenght(); i++)
                {
                    ICell startingCell = null;
                    if (shipOrientation == eShipOrientation.Horizontal)
                    {
                        startingCell = _cells.FirstOrDefault(x => x.GetX() == startX + i && x.GetY() == startY);
                    }
                    else
                    {
                        startingCell = _cells.FirstOrDefault(x => x.GetX() == startX && x.GetY() == startY + i);
                    }

                    if (startingCell != null && startingCell.GetCellType() != eCellType.Ship)
                    {
                        shipTempCells.Add(startingCell);
                    }
                    else
                    {
                        return(false);
                    }
                }
                foreach (var tempCell in shipTempCells)
                {
                    tempCell.SetCellType(eCellType.Ship);
                }

                ship.SetCells(shipTempCells);
                _ships.Add(ship);
                return(true);
            }
            else
            {
                return(false);
            }
        }