我将如何搜索这个包含数组的文件以查找剩余的任何船只?
How would I go about searching this file containing an array for any ships left?
这个项目的目的是创建一个战舰游戏,其中板来自一个文件并放入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;
}
相关文章:
- 在C++中查找文件
- 在UNIX系统中使用DIR查找文件的字节大小
- 通过搜索硬盘驱动器查找文件目录
- 驱动器号上的查找文件失败,但在路径上传递
- 查找文件中的项目数.txt
- 如何使用c 在Windows中使用更改日记帐查找文件修改
- 如何使用C 在Windows中查找文件是否被修改
- 如何在 c++ 中查找文件指针位置
- 数据文件处理-查找文件中的字数
- 在C 中查找文件的平均薪水
- 比 stat() 在 DIR 中查找文件的更好方法
- C 字符串匹配和使用查找文件替换
- 无法打开或查找文件"afxwin.h",使用.dff(构建毁灭战士3源代码)进行修补
- 如何在不使用目录的情况下在磁盘上查找文件
- 如何避免 CFile查找文件扩展名超过三个字符的误报
- Qt无法在子项目中查找文件
- 查找文件时出现Visual Studio编译错误
- VS2010:设置可执行文件在哪个目录中查找文件
- 使用 C++ 查找文件中的所有重复模式
- 当 seekg 查找文件末尾时未设置故障位 (C++,Linux)