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-

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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!

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
Transcribed Image Text: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-
Transcribed Image Text: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-
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education