I have to make a game with 15 x 26 grid sigular array (array of 390 charecters)
and im supose to load the ships which are charecters horozontally.
only 4 are needed and the function im stuck on is suppose to load them one at a time. the main requirements are that the ships dont overlap eachother and the random placements make sure that the ship fits on the line and dosent go to the next. what i have rite now is
void load_ships(char board[ ], char ship[ ])
{
/*int shiplen;*/
char iship[11] =;
int i, row, col, pos, isoccu=-1;
/*shiplen = strlen(ship);*/
strcpy(iship, ship);
char checkstring[11] = "~~~~~~~~~~" , checkboard[11];
srand(time(NULL)); /* would first be called (but only once, at the top of the
'load_ships' function), and then*/
do{
row = rand( ) % 15; /* could be called to generate a random number between 0 and 14
representing row index 0 to 14 and store it in 'row'.*/
do
{
col = rand( ) % 26; /* stores a random number from 0 to 25 in 'col'*/
} while ( (26 - col - 1) < shiplen );
pos = ( (row * 26) + col);
for(i=0;i<shiplen;i++)
{
checkstring[i] = '~';
checkboard[i] = board[(pos + i)];
}
if (strcmp(checkstring, checkboard) == 0)
isoccu = 1;
} while (isoccu == -1);
if (isoccu == 1)
{
for(i=0;i<shiplen;i++)
{
board[(pos+i)] = ship[i];
}
}
}
i have problems cause it dosent seem to allow me to do call strlen or strcpy before any varuble declaration. if i do so when i compile it it displays an error saying that the stuff below does not exist.
any idea how i should write this.. i changed it arround manytimes so the current version dosent work at all. please help me make this work