Ejemplo n.º 1
0
        public void AddShip(int numberOfCells, ShipDirection direction, Coordinates startingPoint)
        {
            List<Cell> shipCells = new List<Cell>();
            for (int i = 0; i < numberOfCells; i++)
            {
                var matchedCell = direction == ShipDirection.Horizontal ? Map.Where(c => c.X == startingPoint.x + i && c.Y == startingPoint.y).FirstOrDefault() : Map.Where(c => c.X == startingPoint.x && c.Y == startingPoint.y + i).FirstOrDefault();

                if (matchedCell != null)
                {
                    matchedCell.State = CellState.Ship;
                    shipCells.Add(matchedCell);
                }
            }

            ShipsOnMap.Add(new Ship() { Cells = shipCells });
        }
Ejemplo n.º 2
0
        public CellState AttackPoint(Coordinates coord)
        {
            var matchedCell = Map.FirstOrDefault(c => c.X == coord.x && c.Y == coord.y);
            if (matchedCell != null)
            {
                if (matchedCell.State != CellState.None)
                {
                    var matchedShip = this.ShipsOnMap.FirstOrDefault(s => s.Cells.Contains(matchedCell));
                    if (matchedShip != null)
                    {
                        matchedShip.AttackShip(matchedCell);
                        if (matchedShip.IsDown)
                        {
                            matchedShip.SinkShip();
                            return CellState.Sank;
                        }
                        return CellState.Hit;
                    }
                }
            }

            return CellState.None;
        }