For a school assignment I have to create a battleship game where one random 4 length battleship (horizontal or vertical) is generated in a 8x8 gameboard. The player has 15 torpedoes to try and sink the ship. I used a 2D vector and have finished most of the code:
As you can probably see I am struggling with the "generateship" function. My goal is to generate RANDOMLY one 4x1 ship (horizontal or vertical) that is completely within my 8x8 2D vector. Any advice/help/comments appreciated!
I would generate first the direction (vertical or horizontal). Then I generate the beginning (x and y) of the battleship. I assume that either x or y increases by 4. So if you get that the battleship is vertical, you can have maximum y for the beginning of the battleship only 4 (y=4,5,6,7), while x is between 0 and 7. If the ship is horizontal, x is between 0 and 4, y is between 0 and 7. So all you need is 3 random numbers.