public bool PlaceShip(ICell[,] board, ShipType shipType, Direction direction, Point initialPosition) { // initial position placed outside of the board if (initialPosition.X >= board.GetLength(0) || initialPosition.Y >= board.GetLength(0)) { return(false); } var significantIndex = direction == Direction.Horizontal ? initialPosition.X : initialPosition.Y; Func <int, Point> getBoardPosition = ix => direction == Direction.Horizontal ? new Point(ix, initialPosition.Y) : new Point(initialPosition.X, ix); // position that would make the ship "stick" out of the board if (significantIndex + shipType.ShipSize() >= board.GetLength(0)) { return(false); } // positions that are already taken (ships can not cross) for (int i = significantIndex; i < significantIndex + shipType.ShipSize(); i++) { var boardPosition = getBoardPosition(i); if (!(board[boardPosition.Y, boardPosition.X] is EmptyCell)) { return(false); } } // valid position, placing the ship var wholeShip = new List <ShipCell>(); for (int i = significantIndex; i < significantIndex + shipType.ShipSize(); i++) { var boardPosition = getBoardPosition(i); var shipCell = new ShipCell(shipType, wholeShip); wholeShip.Add(shipCell); board[boardPosition.Y, boardPosition.X] = shipCell; } return(true); }