[英]How would I go about searching this file containing an array for any ships left?
The point of this project is to create a battle ship game where the board comes from a file and put into a 2D array. 该项目的重点是创建一个战舰游戏,其中棋盘来自文件并放入2D阵列中。 On the board '~' is water and '#' is a ship. 在板上,“〜”是水,“#”是船。 Then it asks for coordinates and determines if there is a ship there or not. 然后,它询问坐标并确定那里是否有一艘船。 If there is a ship, it changes the '#' to 'H'. 如果有船,它将“#”更改为“ H”。 After all of the ships have been destroyed, I have to search for any remaining ships. 在所有船只都被摧毁之后,我必须寻找所有剩余的船只。 This is what I have. 这就是我所拥有的。
#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;
}
This is my input file. 这是我的输入文件。
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~####~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~#~~~~~~~~~~~
~~~~~~~~~~~~~#~~~~~~~~~~~
~~~~~~~~~~~~~#~~~~~~~~~~~
~~~~~~~~~~~~~#~~~~~~~~~~~
####~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~#~~~~~~~~~~~~~~~~~~
~~~~~~#~~~~~~~~~~~~~~~~~~
~~~~~~#~~~~~~~~~~~~~~~~~~
~~~~~~#~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~####~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~#~~~~~~~~~~~~
~~~~~~~~~~~~#~~~~~~~~~~~~
~~~~~~~~~~~~#~~~~~~~~~~~~
~~~~~~~~~~~~#~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
I think my problem is in the FleetSunk() function, but I do not know how to fix it. 我认为我的问题出在FleetSunk()函数中,但我不知道如何解决它。 Any help would be greatly appreciated. 任何帮助将不胜感激。
Man that was messy and the description of the problem wasn't very clear. 那个杂乱无章的人,问题的描述不是很清楚。
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.