I need to initialize the board to have all *'s which is why I have
declared it a char
type. I could use a for loop to make the board
consist of *'s.
Make your functions generic so they can work for yours as well as the opponent board. Avoid globals.
The presented below function initializeBoard
can initialize the board
of the structure Board
to any char
value. If you change your mind and you decide to
replace '*'
with any other value all what is need it is a change of the argument.
void initializeBoard(struct Board* board, char c);
board[i][j] does not work since the memory is not allocated.
As long as you createstruct Board
structure (be it on the stack or dynamically on the heap) the board[ROWS][COLUMNS]
will have memory to hold its data. By creating struct Board
structure all elements of the structure would get the needed memory.
I will later change the misses and hits to O's and X's
You will need a set function to change the content of the particular board cell.
This can be done via:
void setCell(struct Board* board, int row, int column, char c);
All you need is to supply the board
, row
, column
coordinates and value c
to be placed at proper place on the the board.
You will very likely need a function to check what do you have under given coordinates. This can be done via:
char getCell(struct Board* board, int row, int column);
Take a look at the presented simple program.
Good luck with your programming. The road to the Kingdom of the Programming Perfection is a long one but always start from the first steps.
#include <stdio.h>
#include <string.h>
#define ROWS 10
#define COLUMNS 10
struct Board
{
char board[ROWS][COLUMNS];
int carrierHits;
int battleshipHits;
int destroyerHits;
int subHits;
int patrolHits;
};
void initializeBoard(struct Board* board, char c)
{
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
board->board[i][j] = c;
}
void showBoard(struct Board* board)
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
printf(" %c", board->board[i][j]);
}
printf("\n");
}
printf("\n");
}
void setCell(struct Board* board, int row, int column, char c)
{
board->board[row][column] = c;
}
char getCell(struct Board* board, int row, int column)
{
return (board->board[row][column]);
}
int main(void)
{
struct Board my_board;
struct Board opponent_board;
initializeBoard(&my_board, '*');
initializeBoard(&opponent_board, '*');
if(getCell(&my_board,1,5) == '*')
setCell(&my_board, 1, 5, ' ' );
setCell(&my_board, 2, 6, 'X' );
setCell(&my_board, 3, 7, 'O' );
setCell(&my_board, 3, 8, 'O' );
setCell(&opponent_board, 1, 2, 'O' );
setCell(&opponent_board, 1, 3, 'O' );
showBoard(&my_board);
showBoard(&opponent_board);
return 0;
}
Output:
* * * * * * * * * *
* * * * * * * * *
* * * * * * X * * *
* * * * * * * O O *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * O O * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *