/* * Method tries to add ship to board * @author Stanislav Yurchenko * @version 03/12/2017 */ public void AddShip(Ship s, int row, int column) { int[] coordinates = new int[s.GetLength()]; // check if vertical ship is too close to bottom if (s.Vertical && row + s.GetLength() > 10) { throw new IndexOutOfRangeException("Too close to border"); // check if horizontal ship is too close to right side } else if (!s.Vertical && column + s.GetLength() > 10) { throw new IndexOutOfRangeException("Too close to border"); } // Find every coordinate for ship and assign them to the property as long as they don't overlap for (int i = 0; i < coordinates.Length; i++) { coordinates[i] = row * 10 + column; foreach (Ship boat in ships) { if (boat.Coordinates != null && !s.GetName().Equals(boat.GetName()) && boat.Overlap(coordinates[i], true)) { s.Coordinates = null; throw new IndexOutOfRangeException("Overlaps with other ship"); } } if (s.Vertical) { row++; } else { column++; } } s.Coordinates = coordinates; s.GenerateFrame(); }