#include
#include
#include
#include
using namespace std;
vector
vector
int totalThunder = 0;
/** 统计地雷总数 **/
int checkThunderNum()
{
int total = 0;
for (int i = 0; i < thunderMap.size(); i++)
{
for (int j = 0; j < thunderMap[i].size(); j++)
{
if (thunderMap[i][j] == 'B')
{
total++;
}
}
}
return total;
}
/** 初始化扫雷地图 **/
void initThunderMap(int row, int col)
{
if (row < 1 || col < 1)
{
return;
}
for (int i = 0; i < row; i++)
{
string rowMap = "";
for (int j = 0; j < col; j++)
{
rowMap.append("*");
}
showThunderMap.push_back(rowMap);
}
}
/** 打印雷图 **/
void printThunderMap()
{
cout << "----- Thunder Map Begin -----" << endl << endl;
for (int row = 0; row < showThunderMap.size(); row++)
{
cout << " " << showThunderMap[row] << endl;
}
cout << endl;
cout << "----- Thunder Map end -----" << endl << endl;
}
/** 检测雷图 **/
void checkThunderMap(int row, int col)
{
int total = 0;
for (int i = row - 1; i <= row + 1; i++)
{
for (int j = col - 1; j <= col + 1; j++)
{
if (i < 0 || i > 9 || j < 0 || j > 19)
{
continue;
}
if (i == row && j == col)
{
continue;
}
if (thunderMap[i][j] == 'B')
{
total++;
}
}
}
if (total != 0)
{
showThunderMap[row][col] = total + 48;
return;
}
if (total == 0)
{
showThunderMap[row][col] = 0 + 48;
for (int i = row - 1; i <= row + 1; i++)
{
for (int j = col - 1; j <= col + 1; j++)
{
if (i < 0 || i > 9 || j < 0 || j > 19)
{
continue;
}
if (i == row && j == col)
{
continue;
}
if (showThunderMap[i][j] != '*')
{
continue;
}
checkThunderMap(i, j);
}
}
}
}
/** 判断结果 **/
int judgeRuselt()
{
int labelTotal = 0;
int unCheckTotal = 0;
for (int i = 0; i < showThunderMap.size(); i++)
{
for (int j = 0; j < showThunderMap[i].size(); j++)
{
if (showThunderMap[i][j] == '@' && thunderMap[i][j] == 'B')
{
labelTotal++;
}
if (showThunderMap[i][j] == '*') {
unCheckTotal++;
}
}
}
if (labelTotal == totalThunder && unCheckTotal == 0)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
/** 读取地图阵txt **/
fstream infile;
infile.open("C:\\Users\\Administrator\\Desktop\\Question2\\地图阵.txt", ios::in);
vector
string buff;
while (getline(infile, buff))
{
vect.push_back(buff);
}
infile.close();
thunderMap = vect;
for (int i = 0; i < thunderMap.size(); i++)
{
for (int j = 0; j < thunderMap[i].size(); j++)
{
if (thunderMap[i][j] == 'C')
{
thunderMap[i][j] = 'A';
}
}
}
/** 字符C替换为A,输出到文件挑战二.txt **/
fstream outfile;
outfile.open("C:\\Users\\Administrator\\Desktop\\Question2\\挑战二.txt", ios::out);
for (string str : thunderMap)
{
outfile << str << endl;
}
outfile.close();
totalThunder = checkThunderNum();
cout << "当前地图阵含有雷数:" << totalThunder << endl << endl;
initThunderMap(10, 20);
printThunderMap();
while (true)
{
int operation = 0;
cout << "请输入操作类型:"<< "1 扫雷" << "2 标记" << endl;
cout << "operation:";
cin >> operation;
if (operation != 1 && operation != 2)
{
cout << "操作类型无效,请重新输入!" << endl;
continue;
}
int row = 0;
int col = 0;
if (operation == 1)
{
cout << "请输入扫雷坐标 (row, col);row取值范围:0 - 9;col取值范围:0 - 19" << endl;
cout << "row: ";
cin >> row;
cout << "col: ";
cin >> col;
if (row < 0 || row > 9 || col < 0 || col > 19)
{
cout << "(" << row << " , " << col << ")" << "为无效坐标" << endl << endl;
continue;
}
if (thunderMap[row][col] == 'B')
{
/** 0 有雷优先结束 **/
cout << "游戏结束" << endl;
system("pause");
return 0;
}
checkThunderMap(row, col);
printThunderMap();
}
if (operation == 2)
{
cout << "请输入标记坐标 (row, col);row取值范围:0 - 9;col取值范围:0 - 19" << endl;
cout << "row: ";
cin >> row;
cout << "col: ";
cin >> col;
if (row < 0 || row > 9 || col < 0 || col > 19)
{
cout << "(" << row << " , " << col << ")" << "为无效坐标" << endl << endl;
continue;
}
if (showThunderMap[row][col] != '*')
{
cout << "(" << row << " , " << col << ")" << " 已探测坐标无法标记,请重新输入!" << endl << endl;
printThunderMap();
continue;
}
showThunderMap[row][col] = '@';
printThunderMap();
}
int result = judgeRuselt();
if (result == 0)
{
cout << "游戏胜利" << endl << endl;
break;
}
else
{
cout << "游戏继续" << endl << endl;
}
}
system("pause");
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)