Question
This is for c programming: Please help me with the following errors. What am I doing incorrectly? Thank you. MY ERRORS: MY CODE: #include #include
This is for c programming: Please help me with the following errors. What am I doing incorrectly? Thank you.
MY ERRORS:
MY CODE:
#include
#include
#include
// number of rows and cols
const int rows = 10, cols = 10;
// below function initializes the board(any kind of that)
void initializeBoard(int ***board)
{
// create the 2D rows
*board = (int **)malloc(sizeof(int *)*rows);
for (int i = 0; i
{
// create 1D row in each 2D array
(*board)[i] = (int *)malloc(sizeof(int)*cols);
for (int j = 0; j
{
(*board)[i][j] = '-';
}
}
}
// this function prints the board
void printBoard(int **board)
{
// print first row
printf("%3s|", "");
for (char c = 'A'; c
{
printf("%c |", c);
}
printf(" ");
for (int i = 0; i
{
printf("%2d |", i + 1);
for (int j = 0; j
{
printf("%c ", board[i][j]);
if (i == 10)
puts(""); //only triggers if i=10, 10x10grid
printf("|");
}
printf(" ");
}
printf(" ");
}
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');
}
//This decides and returns which which player goes first
int whoStartsFirst(void)
{
int player = 0,
select = 0;
select = rand() % 2;
printf(" ");
if(select == 0)
{
printf("You go first. You are 'Player A'. ");
player = 1;
}
else
{
printf("I go first. I am 'Player B'. ");
player = 2;
}
return player;
}
// this function allows user to set up shot
void giveShot(int shot[2])
{
printf("ROW: ");
scanf("%d",&shot[0]);
shot[0]--;
printf("COLUMN: ");
scanf("%d",&shot[1]);
shot[1]--;
}
int hitShip(int shot[2], int symbol)
{
int ship;
for(ship=0 ; ship
if( shot[0]==symbol[ship][0] && shot[1]==symbol[ship][1])
{
printf("You hit a ship with the shot (%d,%d) ",shot[0]+1,shot[1]+1);
return 1;
}
}
return 0;
}
void tip(int shot[2], int symbol[][2], int attempt)
{
//count how many ships there is row/column
for(row=0 ; row
if(symbol[row][0]==shot[0])
rows++;
if(symbol[row][1]==shot[1])
column++;
}
printf(" Stats: %d: row %d -> %d ships column %d -> %d ships ",attempt,shot[0]+1,line,shot[1]+1,column);
}
void changeBoard(int shot[2], int symbol[][2], int board[][10]){
if(hitShip(shot,symbol))
board[shot[0]][shot[1]]=1;
else
board[shot[0]][shot[1]]=0;
int main(void)
{
//welcomeBattleship();
//printGameRules ();
// declare boards
int **playerBoard, **compBoard;
// initialize them
initializeBoard(&playerBoard);
initializeBoard(&compBoard);
int player = 0;
srand(time(NULL));//required for "randomness"
player = whoStartsFirst();
printf("SALVOS ");
printf("PLAYER 'A' ");
printBoard(playerBoard);
printf("PLAYER 'B' ");
printBoard(compBoard);
placeShips(playerBoard);
placeShips(compBoard);
printf("SHIPS ");
printf("PLAYER 'A': ");
printBoard(playerBoard);
//printf("PLAYER 'B': ");
//printBoard(compBoard);
int shot[2];
int attempts=0,
hits = 0;
do
{
printBoard(playerBoard);
giveShot(shot);
attempts++;
if(hitShip(shot, symbol))
{
tip(shot, symbol, attempts);
hits++;
}
else
tip(shot, symbol, attempts);
changeBoard(shot, symbol, board);
}while(hits!=5);
printf(" You hit the ships in %d attempts", attempts);
printBoard(playerBoard);
return 0;
}
bash: syntax error near unexpected tokenStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started