How to set AI's ships in random positions for battleship game

Hi, so far iv been able to create an 2d array, and I also have a random number generator. I think I have been able to place a ship of 1 length by using my random generator on my 2d array.
I would like to know how would I make the ships bigger, I need a ship of length 2,3,4 and 5 for my game. I would like to use the starting coordinate from my random number generator to place the first part of the ship, I would like to know how I can place the rest of the ship.
Thank you =)

#include <iostream>
#include <cstdlib>
#include <time.h>

const int LOW = 0;
const int HIGH = 4;

using namespace std;

int main()
{
int board [10][10]=//this is the 2d array for the board
{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

for(int row=0;row<10;row++){//this is the loop to display the board
for(int column=0;column<10;column++)
{
cout << board[row][column] << " ";
}
cout <<endl;
}

{//this is the random value generator
int row, column;
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
row = rand() % (HIGH - LOW + 1) + LOW;
column = rand() % (HIGH - LOW + 1) + LOW;
board[row][column] = 1;

cout<< "coordinate is (" << row << ", "
<< column << "}" << endl << endl;

}

return 0;
}
First choose the size and orientation of the ship by defining variables width, height (one of them will be 1)
Then generate coordinates x and y that are in range 10-width and 10-height (so that your ship doesn't go out of the array). Then for i from 0 to length of your ship, fill board[x+i][y] if the ship is horizontal or board[x][y+i] if it is vertical.
Topic archived. No new replies allowed.