hi Guys.
My book gives me no direction on how to do this, but its an exercise in a book. I am to create a simple battleship console game.
I have been doing ok, ( I am not allowed to use OOP ) but I have run into a small snag. I am doing this bit by bit, but when I enter the coordinates to place the player ships, I assign them ok. When I go to print the array to see the positions i have entered, they are correct, bu instead of printing ##### for the first ship, it keeps printing a single hash for all the ships, although the coordinates are correct.
Should I be using strings and not char arrays to store the ships?
Code:
// function prototypes
void setUpComputerBoard ( char [][ 20 ], const char*[] );
void setUpPlayerBoard ( char [][ 20 ], char*[] );
void showGameBoard ( char[][ 20 ], char[][ 20 ], char*[] );
// main function - driver //////////////////////////////////////////////////////
//
int main ( void )
{
srand((unsigned)time(0));
const int MAX_ROWS = 20;
const int MAX_COLS = 20;
char playerBoard[ MAX_ROWS ][ MAX_COLS ] = {{ 0 , 0 }};
char *pPlayerShips[ 5 ] = { "#####", "####", "###", "##", "#" };
const char *pComputerShips[ 5 ] = { "#####", "####", "###", "##", "#" };
char computerBoard[ MAX_ROWS ][ MAX_COLS ] = {{ 0, 0 }};
setUpPlayerBoard ( playerBoard, pPlayerShips );
showGameBoard ( playerBoard, computerBoard, pPlayerShips );
std::cin.get(); // freeze console output window
std::cin.ignore();
return 0; // return value from int main
}
// function to set up the computer opponents board
// function to set up the player board
void setUpPlayerBoard ( char plBoard[][ 20 ], char *pPlShips[] )
{
int xCorr = 0;
int yCorr = 0;
for ( int i = 0; i < 5; )
{
std::cout << "Enter y coordinate for ship: " << pPlShips[ i ]
<< ": ";
std::cin >> xCorr;
std::cout << "Enter x coordinate for ship: " << pPlShips[ i ]
<< ": ";
std::cin >> yCorr;
plBoard[ xCorr ][ yCorr ] = *pPlShips[ i ];
i++;
}
}
void showGameBoard ( char plBoard[][ 20 ], char ComBoard[][ 20 ], char *pPlShips[] )
{
for ( int i = 0; i < 20; i++ )
{
for ( int j = 0; j < 20; j++ )
std::cout << plBoard[ i ][ j ];
std::cout << std::endl;
}
}