예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <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;
        }
예제 #3
0
 public Battleship(string id, EnumShipOrientation orientation) : base(id, 4, orientation)
 {
 }
예제 #4
0
 public AircraftCarrier(string id, EnumShipOrientation orientation) : base(id, 5, orientation)
 {
 }
예제 #5
0
 public Destroyer(string id, EnumShipOrientation orientation) : base(id, 2, orientation)
 {
 }
예제 #6
0
 public Cruiser(string id, EnumShipOrientation orientation) : base(id, 3, orientation)
 {
 }