E#includeciostream> 2 #includecfstream> #includecstring.h> using namespace std; // function prototypes void Fire(char board[25][25]); void Fleetsunk(char board[25][25], int&); 10 11 aint main() 12 char board[25][25]; ifstream infile; infile.open("board.dat"); if (!infile) { 13 14 15 16 17 cout « "Can not open file." <« endl; 18 for (int i = 0; i« 25; i++) { for (int j = e; j< 25; j++) { 19 20 char bs; 22 infile >> bs; 23 board[i][j] = bs; 24 25 26 int fs = e; do { Fire(board); Fleetsunk (board, fs); } while (fs == 0); system("PAUSE"); 27 28 29 30 31 32 33 34 35 36 avoid Fire(char board[25][25]) 37 38 int row = 0, row1 = 0; int col = e, coll = 0; cout <« "Enter the Row and Column that you would like to try and hit :"; cin >> row1; cin >> col1; row = row1 - 1; col = coli - 1; switch (board[row][col]) { 39 40 41 42 43 44 45 46 47 case '#': if (board[row - 1][col] == 'H') { cout « "HIT AGAIN" << endl; board[row][col] - 'H'; 48 49 50 51 else if (board[row + 1][col] == 'H') { cout <« "HIT AGAIN" << endl; 52 53 board[row][col] = 'H'; 54 %3D 55 else if (board[row][col - 1] cout <« "HIT AGAIN" << endl; board[row][col] = 'H'; 56 'H') { 57 58 59 else if (board[row][col + 1] cout <« "HIT AGAIN" <« endl; board[row][col] - 'H'; 60 "н) { 61 62 63 64 else { 65 cout <« "HIT" <« endl; board[row][col] - 'H'; 66 %3D 67 68 break; 69 case '-': cout « "MISS" <« endl; break; 70 71 72 case 'H': cout « "You already destroyed these coordinates." « endl; break; 73 74 75 [} 76 77 Evoid Fleetsunk (char board[25][25], int& fs) { // checking if all ships have sunk for (int i = 0; i < 25; i++) { for (int j = e; j< 25; j++) { if (board[i][j] = fs = e; 78 79 80 81 82 '#') { 83 84 return; 85 86 87 cout <« "The Fleet has been destroyed!" <« endl; fs = 1; 88 89 90 0-0-0-
Imagine we are using a two-dimensional array as the basis for creating the game battle- ship. In the game of battleship a ‘~’ character entry in the array represents ocean (i.e., not a ship), a ‘#’ character represents a place in the ocean where part of a ship is present, and a ‘H’ character represents a place in the ocean where part of a ship is present and has been hit by a torpedo. Thus, a ship with all ‘H’ characters means the ship has been sunk. Declare a two-dimensional char array that is 25 by 25 that represents the entire ocean and an If statement that prints “HIT” if a torpedo hits a ship given the coordinates X and Y. Write a C++ program that will read in a file representing a game board with 25 lines where each line has 25 characters corresponding to the description above. An example file might look like:
Question: Once I run no chart shows and "Can not open file." Please help!
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 2 images