/// <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); } } }
/// <summary> /// MoveShips allows for ships to be placed on the seagrid /// </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 = null; foreach (KeyValuePair <ShipName, Ship> kvp in _Ships) { if (kvp.Key == ship) { newShip = kvp.Value; } } newShip.Remove(); AddShip(row, col, direction, newShip); }
/// <summary> /// MoveShips allows for ships to be placed on the seagrid /// </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]; try { newShip.Remove(); AddShip(row, col, direction, newShip); } catch (Exception e) { AddShip(newShip.Row, newShip.Column, newShip.Direction, newShip); throw new ApplicationException(e.Message); } }
/// <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); } } }