#include #include #include #include #include using namespace std; // typedef char* CharArrayPtr; class Battleship { private: char **board; int row; int col; public: Battleship(); // Default constructor Battleship(int, int); ~Battleship(); void printBoard(); void printEnemyBoard(); void setLayout( int, int, char, char ); void guess( char, char); }; int charToInt(char); // ============================================================== int main ( void ) { // char input_file[50]; // cout << "Enter the name file that contains the layout: " << endl << "> "; // cin >> input_file; ifstream in; // in.open(input_file); in.open("layout.txt"); if (in.fail()) { cout << "Failed to open input file!"; exit(1); } int r, c; in >> r >> c; Battleship(); Battleship player(r, c); Battleship enemy(r, c); int rows; int column; char direction; char ship; for ( int i = 0; i < r; i++ ) { in >> rows >> column >> direction; if ( i == 0 ) ship = 'C'; else if ( i == 1 ) ship = 'B'; else if ( i == 2 ) ship = 'R'; else if ( i == 3 ) ship = 'S'; else if ( i == 4 ) ship = 'D'; player.setLayout(rows, column, direction, ship ); } for ( int i = 0; i < c; i++ ) { in >> rows >> column >> direction; if ( i == 0 ) ship = 'C'; else if ( i == 1 ) ship = 'B'; else if ( i == 2 ) ship = 'R'; else if ( i == 3 ) ship = 'S'; else if ( i == 4 ) ship = 'D'; enemy.setLayout( rows, column, direction, ship ); } player.printBoard(); enemy.printEnemyBoard(); return 0; } // ================================================================ void Battleship::setLayout( int r, int c, char direction, char ship ) { board[r][c] = ship; int u; if ( ship == 'C' ) u = 5; else if ( ship == 'B' ) u = 4; else if ( ship == 'R' ) u = 3; else if ( ship == 'S' ) u = 3; else if ( ship == 'D' ) u = 2; if ( direction == 'N' ) for ( int d = 0; d < u; d++ ) board[r + d][c] = ship; else if ( direction == 'S' ) for ( int d = 0; d < u; d++ ) board[r - d][c] = ship; else if ( direction == 'E' ) for ( int d = 0; d < u; d++ ) board[r][c - d] = ship; else if ( direction == 'W' ) for ( int d = 0; d < u; d++ ) board[r][c + d] = ship; } Battleship::Battleship() { } Battleship::Battleship( int r, int c ) { char **player = new char*[r]; char **enemy = new char*[r]; for ( int i = 0; i < r; i++ ) { player[i] = new char[c]; enemy[i] = new char[c]; } for ( int i = 0; i < c; i++ ) for ( int j = 0; j < r; j++ ) { player[i][j] = '~'; enemy[i][j] = '~'; } } void Battleship::printEnemyBoard() { char alpha[27] = {"abcdefghijklmnopqrstuvwxyz"}; cout << endl << " "; for ( int a = 0; a < col; a++ ) cout << " " << alpha[a]; cout << endl; for ( int i = 0; i < col; i++ ) { cout << alpha[i]; for ( int j = 0; j < row; j++ ) { if ( islower(board[i][j]) == true ) cout << " X"; else cout << " ~"; } cout << endl; } } void Battleship::printBoard() { char alpha[27] = {"abcdefghijklmnopqrstuvwxyz"}; cout << endl << " "; for ( int a = 0; a < col; a++ ) cout << " " << alpha[a]; cout << endl; for ( int i = 0; i < col; i++ ) { cout << alpha[i] << " "; for ( int j = 0; j < row; j++ ) cout << board[i][j] << ' '; cout << endl; } } int charToInt(char C) { return static_cast(C)-97; } Battleship::~Battleship() { // delete all dynamic variables delete [] board; }