private void DrawShip(Graphics g, Ship ship, Brush backColor, int cellSize) { var area = new Rectangle(ship.Position.x, ship.Position.y, 1, 1); if (ship.Orientation == ShipOrientation.Horizontal) { area.Width = ship.GetShipLength(); } else { area.Height = ship.GetShipLength(); } var pixelArea = new Rectangle( area.X * cellSize + cellSize, area.Y * cellSize + cellSize, area.Width * cellSize, area.Height * cellSize); g.FillRectangle(backColor, pixelArea); g.DrawRectangle(m_CellShipEdgePen, pixelArea); }
/// <summary> /// Returns a value indicating whether the given Ship can be placed at its current Position and Orientation. /// </summary> /// <param name="ship">The ship that is to be placed.</param> public bool CanPlaceShip(Ship ship) { // within board? var min = ship.Position; var max = ship.Position; if (ship.Orientation == ShipOrientation.Horizontal) { max.x += ship.GetShipLength() - 1; } else { max.y += ship.GetShipLength() - 1; } if (!min.IsWithinBoard()) { return(false); } if (!max.IsWithinBoard()) { return(false); } // no overlap with other ships? for (int x = min.x; x <= max.x; x++) { for (int y = min.y; y <= max.y; y++) { if (IsCellOccupied(new BoardPosition(x, y))) { return(false); } } } return(true); }
private void BeginPlacingShip() { m_Placing = new Ship(m_PlacingType); lblGameStatus.Text = $"Place your ships! Now placing {m_PlacingType} ({m_Placing.GetShipLength()} cells wide)"; }