Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

populate an array with different sized objects

0 votes
asked Oct 16, 2019 by TylerCooper (120 points)
I am making a battleship game for a project in c and I need to populate the board with different sized objects for example carrier 5, battleship 4, carrier 3,  submarine 3, destroyer 2 how would I do this?

1 Answer

0 votes
answered Oct 27, 2019 by Molero Raffi (540 points)

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.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...