Ejemplo n.º 1
0
        /// <summary>
        /// AddShip add a ship to the SeaGrid
        /// </summary>
        /// <param name="row">row coordinate</param>
        /// <param name="col">col coordinate</param>
        /// <param name="direction">direction of ship</param>
        /// <param name="newShip">the ship</param>
        private void AddShip(int row, int col, Direction direction, Ship newShip)
        {
            try
            {
                int size       = newShip.Size;
                int currentRow = row;
                int currentCol = col;
                int dRow       = 0;
                int dCol       = 0;

                if (direction == Direction.LeftRight)
                {
                    dRow = 0;
                    dCol = 1;
                }
                else
                {
                    dRow = 1;
                    dCol = 0;
                }

                //place ship's tiles in array and into ship object
                int i = 0;
                for (i = 0; i <= size - 1; i++)
                {
                    if (currentRow < 0 | currentRow >= Width | currentCol < 0 | currentCol >= Height)
                    {
                        throw new InvalidOperationException("Ship can't fit on the board");
                    }

                    _GameTiles[currentRow, currentCol].Ship = newShip;

                    currentCol += dCol;
                    currentRow += dRow;
                }

                newShip.Deployed(direction, row, col);
            }
            catch (Exception e)
            {
                newShip.Remove();
                //if fails remove the ship
                throw new ApplicationException(e.Message);
            }
            finally
            {
                if (Changed != null)
                {
                    Changed(this, EventArgs.Empty);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// AddShip add a ship to the SeaGrid
        /// </summary>
        /// <param name="row">row coordinate</param>
        /// <param name="col">col coordinate</param>
        /// <param name="direction">direction of ship</param>
        /// <param name="newShip">the ship</param>
        private void AddShip(int row, int col, Direction direction, Ship newShip)
        {
            try {
            int size = newShip.Size;
            int currentRow = row;
            int currentCol = col;
            int dRow = 0;
            int dCol = 0;

            if (direction == Direction.LeftRight) {
                dRow = 0;
                dCol = 1;
            } else {
                dRow = 1;
                dCol = 0;
            }

            //place ship's tiles in array and into ship object
            int i = 0;
            for (i = 0; i <= size - 1; i++) {
                if (currentRow < 0 | currentRow >= Width | currentCol < 0 | currentCol >= Height) {
                    throw new InvalidOperationException("Ship can't fit on the board");
                }

                _GameTiles[currentRow,currentCol].Ship = newShip;

                currentCol += dCol;
                currentRow += dRow;
            }

            newShip.Deployed(direction, row, col);
            } catch (Exception e) {
            newShip.Remove();
            //if fails remove the ship
            throw new ApplicationException(e.Message);

            } finally {
            if (Changed != null) {
                Changed(this, EventArgs.Empty);
            }
            }
        }