示例#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;
                int dCol;
                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;
                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 = (currentCol + dCol);
                    currentRow = (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);
                }
            }
        }
示例#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;
                int dCol;
                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;
                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");
                    }

                    //problem occurs here Chris - solved?
                    //now after that the size of ship does not change - solved
                    _GameTiles[currentRow, currentCol].Ship = newShip;

                    currentCol = (currentCol + dCol);
                    currentRow = (currentRow + dRow);
                }

                newShip.Deployed(direction, row, col);
            }
            catch (Exception e)
            {
                newShip.Remove();
                // if fails remove the ship
                throw new ApplicationException(e.Message);
            }
            finally
            {
                //this made it work
                Changed?.Invoke(this, EventArgs.Empty);
            }
        }
示例#3
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       = System.Convert.ToInt32(newShip.Size);
                int currentRow = row;
                int currentCol = col;
                int dRow       = 0;
                int dCol       = 0;

                if (direction == Direction.Left || direction == Direction.Right)
                {
                    dRow = 0;
                    dCol = 1;
                }
                else if (direction == Direction.Up || direction == Direction.Down)
                {
                    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 if further moved"));
                    }

                    _GameTiles[currentRow, currentCol].Ship = newShip;

                    currentCol += dCol;
                    currentRow += dRow;
                }

                newShip.Deployed(direction, row, col);
            }
            catch (Exception e)
            {
                throw (new InvalidOperationException(e.Message));
            }
            finally
            {
                if (Changed != null)
                {
                    Changed(this, EventArgs.Empty);
                }
            }
        }