0

I am programming in c and for a homework assignment I am creating a battleship game. I have created a struct for the board (opponent board and my board), but I need a 2D array for the board. This is what I have so far

struct Board
{
        char board[10][10];
        int carrierHits;
        int battleshipHits;
        int destroyerHits;
        int subHits;
        int patrolHits;
};
int initializedBoard() //changed from"myBoard" to "initializedBoard"
{
        struct Board myBoard;
}

I need to initialize the board to have all *'s which is why I have declared it a char type. I was thinking that maybe I could use a for loop to make the board consist of *'s but I do not know how to go about that because board[i][j] does not work since the memory is not allocated. I am really confused as to how to accomplish this task. I will later change the misses and hits to O's and X's. Please help me understand this.

Bob
  • 11
  • 3
  • First thing, please use distinct names. `myBoard` is the name of a function *and* a structure. – Weather Vane Mar 04 '18 at 19:02
  • Second thing, don't mix *form* and *function*. What happens in the game algorithm, and what is displayed, are quite different things. – Weather Vane Mar 04 '18 at 19:05
  • This seems to be already answered here https://stackoverflow.com/questions/15520880/initializing-entire-2d-array-with-one-value – Leni Mar 04 '18 at 19:07

2 Answers2

1

I'm don't know C that well, but this seems to work:

#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;
};

struct Board myboard;

void initializeBoard(struct Board* board)
{
    memset(board->board, '*', ROWS * COLUMNS);
}

void fireAt(int row, int column)
{
    myboard.board[row][column] = '0';
}

int main()
{
    initializeBoard(&myboard);
    fireAt(4, 5);
    fireAt(4, 6);
    fireAt(4, 7);

// SHOW THE BOARD
        for (int i = 0; i < 10; ++i)
        {
            for (int j = 0; j < 10; ++j)
                printf(" %c", myboard.board[i][j]);
            printf("\n");
        }

    return 0;
}

I don't know what you mean by

"board[i][j] does not work since the memory is not allocated."

When you create the instance of Board with struct Board myboard, myboard has allocated memory for the Board struct, including the 10 by 10 chars. You would only need allocate memory if instead of an array you had a pointer, but memory is allocated for arrays.

You can also initialise the board with a loop instead of memset.

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • A friendly comment. Do not abuse globals, Battleship game requires two boards. You would be forced to create two separate functions `void fireAt(int row, int column)` instead of reusing one. – sg7 Mar 05 '18 at 00:12
1

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 * * * * * *                                                                                                                           
 * * * * * * * * * *                                                                                                                           
 * * * * * * * * * *                                                                                                                           
 * * * * * * * * * *                                                                                                                           
 * * * * * * * * * *                                                                                                                           
 * * * * * * * * * *                                                                                                                           
 * * * * * * * * * *                                                                                                                           
 * * * * * * * * * *                                                                                                                           
 * * * * * * * * * *
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sg7
  • 6,108
  • 2
  • 32
  • 40