在此扫雷源文件编写中,我采用了二维数组来做(根据需要可以在类的对象数组定义中改变N的值来改变游戏范围),同时此处定义N时,要将其定义为静态成员,不然下面的数组处将会报错(非静态成员引用必须与特定对象相对)。
#include<iostream>
#include<iomanip>
#include<ctime>
#include<cstdlib>
#include<vector>
#include<stdlib.h>
using namespace std;
class Gamesl {
static const int N = 6;
char map[N][N];
public:vector<int>result;
int sum = 0;
int get_N() const { return N; }
void ditu();
void zhongleiditu();
void dilei();
int isbomb(int b);
void notbomb(int b);
int sore(int i);
int end();
void winditu();
};
void Gamesl::ditu() {
for (int i = 0; i < N; ++i) {
cout << "+";
for (int i = 0; i < N; ++i)cout << "---" << "+";
cout << endl; cout << "|";
for (int j = 0; j < N; ++j) {
cout << setw(3) << i*N+j << "|";
}
cout << endl;
if (i == N - 1) {
cout << "+";
for (int i = 0; i < N; ++i)cout << "---" << "+";
}
}
}
void Gamesl::zhongleiditu() {
cout << setw(3) << " ";
for (int w = 0; w < N; ++w)cout << " " << w;
cout << endl;
for (int i = 0; i < N; ++i) {
cout << setw(3) << " ";
cout << "+";
for (int i = 0; i < N; ++i)cout << "---" << "+";
cout << endl;
cout << setw(3) << i;
cout << "|";
for (int j = 0; j < N; ++j) {
if (map[i][j] == '*')cout << setw(3) << "*" << "|";
else cout << setw(3) << i * N + j << "|";
}
cout << endl;
if (i == N - 1) {
cout << setw(3) << " ";
cout << "+";
for (int i = 0; i < N; ++i)cout << "---" << "+";
}
}
}
void Gamesl::dilei() {
double m_rate = double(N * N) / 100;
srand((unsigned)time(NULL));
for (auto& r : map) {
for (auto& c : r) {
double rate = static_cast<double>(rand() % 100)/100 ;
if (rate <= m_rate) c = '*';
}
}
}
int Gamesl::isbomb(int b) {
if (map[b/N][b%N] == '*') {
cout << "恭喜您中地雷了!" << endl;
cout << "Game Over." << endl;
zhongleiditu();
cout << "最终得分为" << sum <<"分"<< endl;
return 1;
}
else {
notbomb(b);
return 0;
}
}
void Gamesl::notbomb(int b) {
vector<int>vi (1,b);
do {
b = vi.front();
int besides[]{ (b / N > 0) ? b -N: -1, (b % N > 0) ? b - 1 : -1, (b / N < N - 1) ? b +N : -1, (b % N < N - 1) ? b + 1 : -1 };
for (auto &l : besides) {
if (l != -1 && map[l / N][l % N] != '*' && map[l / N][l % N] != '1') {
vi.push_back(l);
map[l / N][l % N] = '1';
}
}
result.push_back(b);
vi.erase(vi.begin());
} while (!vi.empty());
int r = 0;
++r;
sum = result.size()-r;
sore(sum);
for (auto it = result.begin(); it != result.end(); ++it) {
int neibor[]{ ((*it) / N > 0) ? (*it) - N : -1, ((*it) % N > 0) ? (*it) - 1 : -1, ((*it) / N < N - 1) ? (*it) + N : -1, ((*it) % N < N - 1) ? (*it) + 1 : -1 };
int a = 0;
for (auto& h : neibor) {
if (h != -1 && map[h/N][h%N]=='*') {
++a;
}
}
if (a == 0)map[(*it) / N][(*it) % N] = '0';
if (a == 1)map[(*it) / N][(*it) % N] = '1';
if (a == 2)map[(*it) / N][(*it) % N] = '2';
if (a == 3)map[(*it) / N][(*it) % N] = '3';
if (a == 4)map[(*it) / N][(*it) % N] = '4';
}
cout << endl;
cout << setw(3) << " ";
for (int w = 0; w < N; ++w)cout << " " << w;
cout << endl;
for (int i = 0; i < N; ++i) {
cout << setw(3) << " ";
cout << "+";
for (int i = 0; i < N; ++i)cout << "---" << "+";
cout << endl;
cout << setw(3) << i;
cout << "|";
for (int j = 0; j < N; ++j) {
if (map[i][j] == '0' || map[i][j] == '1' || map[i][j] == '2' || map[i][j] == '3' || map[i][j] == '4')
cout << setw(3) << map[i][j] << "|";
else cout << setw(3) << "$" << "|";
}
cout << endl;
if (i == N - 1) {
cout << setw(3) << " ";
cout << "+";
for (int i = 0; i < N; ++i)cout << "---" << "+";
}
}
}
int Gamesl::sore(int i) {
cout << "目前的得分是:"<<i<< "分"<<endl;
return i;
}
int Gamesl::end() {
int s = 0;
for (auto& i : map) {
for (auto& j : i) {
if (j == '*') {
++s;
}
}
}
int number = N * N - s;
if (sore(sum) == number) {
cout << "你真NICE!"<<endl;
cout << "游戏结束"<<endl;
cout << "最终得分为" << number << "分"<<endl;
return 1;
}
else return 0;
}
void Gamesl::winditu() {
cout << endl;
cout << setw(3) << " ";
for (int w = 0; w < N; ++w)cout << " " << w;
cout << endl;
for (int i = 0; i < N; ++i) {
cout << setw(3) << " ";
cout << "+";
for (int i = 0; i < N; ++i)cout << "---" << "+";
cout << endl;
cout << setw(3) << i;
cout << "|";
for (int j = 0; j < N; ++j) {
if (map[i][j] == '0' || map[i][j] == '1' || map[i][j] == '2' || map[i][j] == '3' || map[i][j] == '4')
cout << setw(3) << map[i][j] << "|";
else cout << setw(3) << "*" << "|";
}
cout << endl;
if (i == N - 1) {
cout << setw(3) << " ";
cout << "+";
for (int i = 0; i < N; ++i)cout << "---" << "+";
}
}
}
int main() {
Gamesl t;
t.ditu();
int m;
int max = t.get_N() * t.get_N() - 1;
cout << endl<< "请输入方格的编号(0<=m<="<<max<<"),m=";
cin >> m;
system("CLS");
t.dilei();
t.isbomb(m);
while (1) {
if (t.end()) {
system("CLS");
t.end();
t.winditu();
break;
}
if (t.sum == 0)break;
int n;
cout << endl << "请输入方格的编号=";
cin >> n;
int p = 0;
for (auto it = t.result.begin(); it != t.result.end(); ++it) {
if (n == *it) {
++p;
}
}
if (p == 1)continue;
system("CLS");
if (t.isbomb(n))break;
if (t.end()) {
system("CLS");
t.end();
t.winditu();
break;
}
}
return 0;
}