/// <summary> /// Verifies if the informed position is valid for a new ship /// </summary> /// <param name="pos"></param> /// <param name="shipSize"></param> /// <param name="orientation"></param> /// <returns></returns> private bool CheckValidPosition(Coordinates pos, int shipSize, EnumShipOrientation orientation) { var gridWidth = Width; if (shipSize <= 0) { return(false); } if (pos.X > gridWidth || pos.Y > gridWidth) { return(false); } int shipAnchorPos = orientation == EnumShipOrientation.Horizontal ? pos.X : pos.Y; if (shipAnchorPos + shipSize > gridWidth) { return(false); } for (int i = 0; i < shipSize; i++) { var currentShip = orientation == EnumShipOrientation.Horizontal ? Get(pos.X + i, pos.Y) : Get(pos.X, pos.Y + i); if (currentShip != null) { return(false); } } return(true); }
/// <summary> /// Create a new instance of Ship /// </summary> /// <param name="size"></param> /// <param name="orientation"></param> public Ship(string id, int size, EnumShipOrientation orientation) { if (string.IsNullOrEmpty(id)) { throw new ArgumentException("invalid ship id"); } if (size <= 0) { throw new ArgumentException($"invalid ship size: {size}"); } ID = id; Size = size; Orientation = orientation; }
public Battleship(string id, EnumShipOrientation orientation) : base(id, 4, orientation) { }
public AircraftCarrier(string id, EnumShipOrientation orientation) : base(id, 5, orientation) { }
public Destroyer(string id, EnumShipOrientation orientation) : base(id, 2, orientation) { }
public Cruiser(string id, EnumShipOrientation orientation) : base(id, 3, orientation) { }