/* <summary> * MoveShips allows for ships to be placed on the seagrid also saves the previous directions and location of the ship * </summary> * <param name="row">the row selected</param> * <param name="col">the column selected</param> * <param name="ship">the ship selected</param> * <param name="direction">the direction the ship is going</param> */ public void MoveShip(int row, int col, ShipName ship, Direction direction) { Ship newShip = _Ships[ship]; newShip.Remove(); _oldCol = _Ships[ship].Column; _oldRow = _Ships[ship].Row; _oldDir = _Ships[ship].Direction; AddShip(row, col, direction, newShip); }
/* <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 = 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 = (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); } } }
/// <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); } } }