我将如何搜索这个包含数组的文件以查找剩余的任何船只?

How would I go about searching this file containing an array for any ships left?

本文关键字:查找 文件 任何船 数组 包含 何搜索 搜索      更新时间:2023-10-16

这个项目的目的是创建一个战舰游戏,其中板来自一个文件并放入2D数组中。板上的"~"是水,"#"是船。然后它询问坐标并确定那里是否有船。如果有船,它会将"#"更改为"H"。在所有船只被摧毁后,我必须寻找剩余的船只。这就是我所拥有的。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
char GameBoard[25][25];
void Fire(int, int);
void FleetSunk(char GameBoard,int& fleet);
int main()
{
int i;
int j;
int row;
int column;
int fleet=0;
ifstream inData;
inData.open("GameBoard.txt");
//Program logic
cout << "Welcome to Battle Ship!" << endl << "Try to sink all of the ships!" << endl;
for (i = 0; i < 25; i++) {
for (j = 0; j < 25; j++) {
inData >> GameBoard[i][j];
}
}
do {
cout << "Please enter a row number 0-24 that you want to hit: ";
cin >> row;
cout << "Please enter a column number 0-24 that you want to hit: ";
cin >> column;
Fire(row, column);
FleetSunk(GameBoard[25][25], fleet);
} while (fleet = 1);
system("pause");
return 0;
}
void Fire(int row, int column){
switch (GameBoard[row][column]) {
case '#':
GameBoard[row][column] = 'H';
cout << "HIT"<<endl;
break;
case 'H':
cout << "HIT AGAIN" << endl;
break;
case '~':
cout << "MISS" << endl;
break;
}
}
void FleetSunk(char GameBoard, int& fleet)
{
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
if (GameBoard[i][j] == '#') {
fleet = 1;
return;
}
}
}
cout << "The Fleet has been destroyed!" << endl;
fleet = 0;
}

这是我的输入文件。

~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~####~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~#~~~~~~~~~~~
~~~~~~~~~~~~~#~~~~~~~~~~~
~~~~~~~~~~~~~#~~~~~~~~~~~
~~~~~~~~~~~~~#~~~~~~~~~~~
####~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~#~~~~~~~~~~~~~~~~~~
~~~~~~#~~~~~~~~~~~~~~~~~~
~~~~~~#~~~~~~~~~~~~~~~~~~
~~~~~~#~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~####~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~#~~~~~~~~~~~~
~~~~~~~~~~~~#~~~~~~~~~~~~
~~~~~~~~~~~~#~~~~~~~~~~~~
~~~~~~~~~~~~#~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~

我认为我的问题出在 FleetSunk(( 函数中,但我不知道如何解决它。任何帮助将不胜感激。

那个人很乱,对问题的描述不是很清楚。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int l = 25;    //just to be easier to debug and change
char GameBoard[l][l];
void Fire(int row, int column){
switch (GameBoard[row][column]) {
case '#':
GameBoard[row][column] = 'H';
cout << "HIT"<<endl;
break;
case 'H':
cout << "HIT AGAIN" << endl;
break;
case '~':
cout << "MISS" << endl;
break;
}
}
int FleetSunk(char GameBoard[][l]){    //you have to pass a char array and not a simple char
for (int i = 0; i < l; i++)
for (int j = 0; j < l; j++)
if (GameBoard[i][j] == '#')
return 1;    //removed some useless brackets
cout << "The Fleet has been destroyed!" << endl;
return 0;
}
int main(){
int i, j;    //clean a bit
int row, column;
int fleet = 0;
ifstream inData;
inData.open("GameBoard.txt");
//Program logic
cout << "Welcome to Battle Ship!" << endl << "Try to sink all of the ships!" << endl;
for (i = 0; i < l; i++)
for (j = 0; j < l; j++)
inData >> GameBoard[i][j];
do {
cout << "Please enter a row number 0-" << l-1 << " that you want to hit: ";
cin >> row;
cout << "Please enter a column number 0-"<<l-1 << " that you want to hit: ";
cin >> column;
Fire(row, column);
fleet = FleetSunk(GameBoard); //you can assign the value of fleet instead of change his value in the function
} while (fleet);    //"while fleet is not 0"
system("pause");
return 0;
}