i have done coding for the board but now I'm stuck on getting randomizing ship placement for player1 and player2


So far for ship placement all i have is the function prototype.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>



int placeship( int x, int z[ ][ 10 ] ); // function

each player has 5 ships

carrier = 5 spaces
battleship = 4 spaces
destroyer = 3 spaces
submarine = 3 spaces
patrol = 2 spaces

I need to build a randomizer for ship placement on a 10x10 board(i've built the board)
i've to built it in a way that they don't overlapp each other, don't go off the board.

how would i go about doing this.

I've been stuck on this for days and i'm seriously getting depressed from this.

please help

Dani AI

Generated

the key is: pick a random start cell, pick a direction, validate the full run of cells, and only then write the ship. That covers both off-board and overlap checks that mentioned. Using a char board with '.' for empty and one letter per ship keeps placement simple; you can always graduate to a Ship class later as suggested.

Call srand((unsigned)time(0)); once in main(), then use helpers like these:

#include <cstdlib>
#include <ctime>

static const int N = 10;

bool can_place(char b[N][N], int r, int c, int len, int dr, int dc) {
    for (int k = 0; k < len; ++k, r += dr, c += dc) {
        if (r < 0 || r >= N || c < 0 || c >= N) return false;
        if (b[r][c] != '.') return false;
    }
    return true;
}

void write_ship(char b[N][N], int r, int c, int len, int dr, int dc, char mark) {
    for (int k = 0; k < len; ++k, r += dr, c += dc) b[r][c] = mark;
}

bool place_ship_random(char b[N][N], int len, char mark) {
    for (int tries = 0; tries < 200; ++tries) {
        int horiz = rand() % 2;
        int dr = horiz ? 0 : 1, dc = horiz ? 1 : 0;
        int r = rand() % N, c = rand() % N;
        if (can_place(b, r, c, len, dr, dc)) { write_ship(b, r, c, len, dr, dc, mark); return true; }
    }
    return false; // optional: fall back to a deterministic scan here
}

Usage: initialize the board to '.', then loop lengths {5,4,3,3,2} with marks {'C','B','D','S','P'} and call place_ship_random for each. Tip: if you want ships not to touch, extend can_place to also check the 8 neighbors of each candidate cell. If placements ever fail, clear the board and retry to avoid rare dead-ends.

Recommended Answers

All 3 Replies

My guess would be to have it check to see if it goes off the board eg: if(location == occupied) Try_Again(); or something. I know it's a long hard process (lots of typing) but it will be worth it. You will have to find your own way of doing that though because you gave me soo little code.

My guess would be to have it check to see if it goes off the board eg: if(location == occupied) Try_Again(); or something. I know it's a long hard process (lots of typing) but it will be worth it. You will have to find your own way of doing that though because you gave me soo little code.

yes the reason i posted so little is because that's where i need help in.

What I've done so far:

- make the board for each player
- when the player win
- have each player take turns

What I need to do:
- get random ship placements for each player

int placeship( int x, int z[ ][ 10 ] )
{
    int xCoord, yCoord, direction;
    xCoord = rand() % 10;
	yCoord = rand() % 10;
	direction = rand() % 2;

I got this so far for ship placement :/
Any help is appreciated

let's say you have a board declared as as multidimensional array of char. If there isn't a ship in the cell the value stored in the cell is a space char. If there has been a hit there is an H. If there is a ship there then it has the value of the first letter of the ship. To place a ship you need to know the size of the ship, a random cell and a diretion. If the value of the random cell has the value of space then check each cell in the specified diretion to be sure that each cell needed is a space and is on the board. If they are then place the appropriate letter in each cell that was checked. Repeat the proess for each ship that is used.

I would probably use a ship class that has the char identifier, the size, an array of cells the ship occupies and a boolean indicating whether it is sunk or not. The game is over once enough hits has occured or once all the ships have been sunk, whichever you want to keep track of.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.