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

Edited 4 Years Ago by myrongainz: n/a

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

Edited 4 Years Ago by myrongainz: n/a

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.

This article has been dead for over six months. Start a new discussion instead.