Question: This is for c programming: i need help with the following functions. How do I go about finding 5 contiguous free spaces either in a
This is for c programming: i need help with the following functions. How do I go about finding 5 contiguous free spaces either in a row or a acolumn and then write 5 of those to the array? Thank you.
MY FUNCTIONS:
void randomPlaceShips(int **board, int count, int symbol)
{
while(count--)
{
int r = rand() % rows;
int c = rand() % cols;
while(board[r][c] != '-')
{
// means this cell is already assigned, try something else..
r = rand() % rows;
c = rand() % cols;
}
// place ship type
board[r][c] = symbol;
}
}
void placeShips(int **board)
{
// put 5 carriers
randomPlaceShips(board, 5, 'C');
// put 4 battleships
randomPlaceShips(board, 4, 'B');
// put 3 destroyer
randomPlaceShips(board, 3, 'D');
// put 3 submarine
randomPlaceShips(board, 3, 'S');
// put 3 patrol boat
randomPlaceShips(board, 2, 'P');
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
