C++ 我如何搜索这个包含数组的文件来查找剩下的船只?

C++ 我如何搜索这个包含数组的文件来查找剩下的船只?,c++,arrays,C++,Arrays,这个项目的重点是创建一个战舰游戏,其中棋盘来自一个文件并放入一个2D阵列。船上的“~”是水,是一艘船。然后它询问坐标并确定那里是否有船只。如果有一艘船,它将更改为“H”。在所有的船都被摧毁后,我必须寻找剩下的船。这就是我所拥有的 #include "stdafx.h" #include <iostream> #include <string> #include <fstream> using namespace std; char

这个项目的重点是创建一个战舰游戏,其中棋盘来自一个文件并放入一个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;
}

我认为我的问题在于功能,但我不知道如何解决它。任何帮助都将不胜感激。

这个人很混乱,对问题的描述也不是很清楚

#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;
}

发布输入文件中的一行示例。GameBoard应该是char**GameBoard您对GameBoard的使用有点问题。将其定义为堆栈上的全局数组。然后,只将一个字符传递给一个字符。我只想删除FleetSink的参数。在循环条件中用fleet==1替换fleet=1,它应该可以工作。另外,下次在StackOverflow之前使用,请至少在GCC中工作。我修复了一些东西,并观察它是否工作-这似乎是对答案中提供的代码最糟糕的解释之一…是的,我应该先测试一下,然后再发布它,向他解释变更日志,这是我的错。我将在测试后编辑答案。
#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;
}