How do we offer namaz on a ship with respect to the direction of qibla?

Imam al-Ṣādiq (as) was asked about prayer on a boat. So he said: ‘Face the qibla, and when the boat shifts directions, if possible to turn again to face the qibla then one should do so. Otherwise, he should pray whatever direction he faces. If he is able to pray standing, then he should do so, and if he cannot, then he should sit and pray.’

Wasāʾil al-shīʿa, v. 3, p. 235, ḥ. 13

A traveller must work out the direction of the qibla. He is not allowed to offer his obligatory prayers while riding on something, unless he can offer them without any defects, as when one is on a plane, train, or boat, and he will not be bothered by their movements. But if the vehicle is not steady, like a boat in turbulent waters or a plane during take-off or landing, etc., then precaution requires the traveller to delay his prayers until he can offer them while he is steady. And so he must always face towards the qibla; adjusting towards it whenever his means of transportation turns. However, if he cannot maintain his position facing the qibla and he fears that he will miss his prayers, he should face it to whatever extent possible in his prayers, even if this is only for the takbīrat al-iḥrām. Otherwise, he can offer his prayers without facing it; and the same applies to someone who is compelled to pray while walking.