What kind of battleship game is it?
if it uses a 2d array to store the board, and you have to put ship parts on it, then you could
const int WIDTH = 8;
const int HEIGHT = 8;
// tries to add the ship to the board.
// returns whether the ship was placed or not
bool placeShip(
int board[WIDTH][HEIGHT], // the 8*8 board itself
int length, // the ship length
int type, // the ship type; 0:empty, 1:carrier, etc...
int x, int y, // the location of the ship's tail-end
int direction // the direction; 0:right, 1:down
) {
// if the ship goes right:
if (direction == 0) {
// first, check if you can even place the ship.
for (int i = 0; i < length; i++) {
if (x + 1 >= WIDTH) return 0; // out of bounds; can't place.
if (board[x + i][y] != 0) return 0; // occupied; can't place.
}
// we've reached this point, now place!
for (int i = 0; i < length; i++) {
board[x + i][y] = type;
}
return 1; // successful!
}
// if the ship goes down:
if (direction == 1) {
// first, check if you can even place the ship.
for (int i = 0; i < length; i++) {
if (y + i >= HEIGHT) return 0; // out of bounds; can't place.
if (board[x][y + i] != 0) return 0; // occupied; can't place.
}
// we've reached this point, now place!
for (int i = 0; i < length; i++) {
board[x][y + i] = type;
}
return 1; // successful!
}
}
To use this, you could set up a function that takes a length and ship type, and chooses a random direction from 0 to 1 and a random location from (0, 0) to (7, 7):
// randomly places a ship with a specified length and type.
// Note 1: you need to #include <stdlib.h> to use rand(), which this function uses.
// Note 2: each time you run the program, the result of rand() is always the same, so you have to take user input at some point, either to scramble rand() or to manually place the ships.
void randomPlace(
int board[WIDTH][HEIGHT],
int length,
int type
) {
// lesser used form of a while loop: runs the code *before* checking the condition.
// in this case, it creates the random values *before* seeing if a ship was successfully placed.
do: {
// rand() % n gets a number from 0 to n-1
int direction = rand() % 2;
int x = rand() % 8;
int y = rand() % 8;
} while(placeShip(board, length, type, x, y, direction);
}
Now, you can use these functions to (randomly) place ships on an 8*8 board.
But first, you have to make the board.
int main() {
// make a 2d array containing "int"s of size "WIDTH" * "HEIGHT"
int board[WIDTH][HEIGHT];
// arrays aren't automatically set to contain 0's, so we have to do that ourselves.
for (int x = 0; x < WIDTH; x++) { // for each column in the board:
for (int y = 0; y < HEIGHT; y++) { // for each tile in the column:
board[x][y] = 0; // make the tile empty.
}
}
// now we can place ships.
randomPlace(board, 5, 1); // randomly place a length 5 "carrier"
randomPlace(board, 5, 1); // randomly place another length 5 "carrier"
randomPlace(board, 4, 2); // randomly place a length 4 "battleship" or whatever
}
I've pretty much done everything concerning a 2d board.
However, if you meant populating an array to have information about the placed objects, then I'm too tired to do that after what I just gave you.