コード例 #1
0
ファイル: Board.cs プロジェクト: vitormoura/game-seabattle
        /// <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
ファイル: Ship.cs プロジェクト: vitormoura/game-seabattle
        /// <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)
 {
 }