I am doing a battleship game for my project. I am still trying to make a better algorithm.. This is what I have so far. Here is a code that will pick random coordinates for the computers ship, then draws the board and show where the ship is located...

Assuming that I had ten ships (1 ship per index) How will I generate ten coordinates and make the program remember that this index has already a ship so it won't generate the same coordinates??

int getcoord ();
void main()
int x,y ;
clrscr ();


getcoord (x,y);

getch ();

getcoord (x,y) {
int array[10][10]={0},i,j;

for (j=0;j<10;j++)  {

	for ( i=0;i<10;i++) {
	printf ("  %d", array[j][i]);
	printf ("\n\n");



#include <stdio.h>

int placeShip ( int sea[][10], int id, 
                int bowx, int boy, 
                int sternx, int sterny ) {
    // checks the sea to see if a ship can be placed between
    // the stated coords
    // yes, place the ship and return true
    // no, return false and don't change the sea

int main ( ) {
    int sea[10][10] = { 0 };
    placeShip( sea, 1, 5, 5, 10, 5 );
    return 0;

Except you would call it in a loop to place a number of ships.

You would then enter another loop to
- throw shells at the sea
- determine hits
- determine sinkings
- determine a winner

there is an easy way to show that given Coords are occupied is that by using a 2D bool array that is initialized to false at first (this array represents ur grid with all slots are free), then when u have a Ship placed at given X & Y evaluate its Coords in the 2D array to true, Then when placing another ship u check the 2D bool array if the generated Coord is free or not, if free place it and update ur array else Generate new Coords and try to place ur ship with the new Coords and so on ...

You mention project. Is this a C class final project?

Watch your [x][y] indexing. You called your roughed in getcoord(x,y) but in getcoord you did a [j]. There's nothing wrong with that but it does indicate that you might make a mistake later in your code due to confusion. Be consistent. If your orientation is [x][y] then your indexing should be the same [x][y], or [j], [r][s], etc. Note the letter sequencing! It's a little thing but I've seen this kind of mistake made over the years in others code by accidentally reversing the coordinates.
I'm assumig you're not trying to replicate the existing game? Because their ships occupy from 2 to (5?) row/column slots!

You are using a 10x10 (which should be defines at the top of your code to be more flexible) but your randomizing x,y for 10,9 ?

I like using true RNG's but you may want to put the srand declaration at the top of your code to seed it!
srand( time(0) );