For my class, I need to make a pretty basic Battleship game. However, I’m new to programming and am running into some problems. First, let me explain my idea for the program. I’d like to keep two separate 2D arrays for each player’s gameboard. I have a function for placing each ship, and I will make other functions that incorporate those into one “turn” function. The gameboard will begin by being filled with O’s, and each ship placement will replace an O with the first letter of the ship. A hit will be designated as an X. Once the ships have been placed, a winner will be determined when a board only has X’s and O’s left. The main problem I’m running into right now is with my 2D arrays. I want to place them in a struct, but I’m running into a couple of errors that I don’t know how to handle. I’m getting an “expected unqualified-id before ‘.’ token” on the board.firstboard = line. The other error I’m getting is saying I’m missing a } but if I add one, it says it’s an extra. Is this related to the previous error? Thanks for any help! Also, I’m not sure if I provided my code correctly, so let me know if I didn’t and I’ll correct it!
struct board
{
static char firstBoard[10][10] ;
static char secondBoard[10][10] ;
} ;
void initializeBoard()
{
board.firstBoard =
{
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
} ;
board.secondBoard =
{
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
{'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O'},
} ;
}
Making the arrays
staticmembers turns them into global variables. You don’t want that. If they arestatic, then allboardobjects will share the same two arrays.You can’t set arrays by assigning
{ ...data... }to them. You can only initialize arrays this way, and objects can only be initialized once. You can’t initialize globals from inside a function that way.Here is a suggested version:
The changes are:
The arrays are now fields of the
boardstruct, rather than globals (staticmembers).The initialization has been moved to a constructor for the
boardstruct. You will not need to call it explicitly.Array initialization has been replaced with
std::memset. This works well because the array element type ischar. If they weren’tchar, you might have to do something else to initialize them.Defined constants for array width and height.