闲得无聊于是写了一个扫雷
明明是用c++写的,却一股c的味道
屎山代码呈上
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
const int N = 31;
char map[N][N];//棋盘
bool boob[N][N];//炸弹位置
bool visit[N][N];//是否被访问
int n=15;//棋盘大小
int total_boom = 10;//炸弹总数
int now_boom;//剩余炸弹数
int check;//是否进入游戏
bool first;//是否为第一次扫雷
int ans;//计数空格
int dx[8] = { 1,1,1,0,-1,-1,-1,0 };
int dy[8] = { -1,0,1,1,1,0,-1,-1 };
void begin();
void DFS(int x,int y);
void clear();
void menu() {
cout << "欢迎来到扫雷" << endl;
cout << "输入1开始" << endl;
cout << "输入0结束" << endl;
while (true) {
cin >> check;
getchar();
clear();
if (check == 1) {
cout << "请输入你要挑战的棋盘大小n(将生成n*n大小的棋盘,5<=n<=28):" << endl;
cin >> n;
getchar();
while (n < 5 || n > 28) {
cout << "输入有误,请重新输入:" << endl;
cin >> n;
getchar();
}
cout << "请输入你要挑战的地雷数量(不超过100个):" << endl;
cin >> total_boom;
getchar();
while (total_boom <= 0 || total_boom >= 100) {
cout << "输入有误,请重新输入:";
cin >> total_boom;
getchar();
}
now_boom = total_boom;
begin();
}
else {
break;
}
}
cout << "欢迎下次再来玩"<<endl;
}
void show(){
cout << " ";
for (int i = 1; i <= n; i++) {
if (i < 10) {
cout << " " << i << ' ';
}
else {
cout << " " << i;
}
}
cout<<endl;
for (int i = 1; i <= n; i++) {
cout << " ";
for (int j = 1; j <= n; j++) {
cout << "----";
}
cout << '-';
if (i == 2) {
cout << " " << "输入格式:a x y:打开坐标为x,y的格子(x为纵坐标,y为横坐标)";
}
else if (i == 3) {
cout << " " << " b x y:标记坐标为x,y的格子,标记为!";
}
else if (i == 4) {
cout << " " << " c x y:取消坐标为x,y的格子的标记";
}
else if (i == 5) {
cout << " " << "地雷总数为:" << total_boom;
}
else if (i == 6) {
cout << " " << "剩余地雷数(总地雷数-标记地雷数)为:" << now_boom;
}
cout << endl;
if (i < 10) {
cout << i << ' ';
}
else {
cout << i;
}
for (int j = 1; j <= n; j++) {
cout << "| " << map[i][j] << ' ';
}
cout << '|' << endl;
}
cout << " ";
for (int j = 1; j <= n; j++) {
cout << "----";
}
cout << '-' << endl;
}
void initial() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
map[i][j] = '*';
boob[i][j] = false;
visit[i][j] = false;
}
}
first = true;
ans = 0;
}
void clear() {
system("cls");
}
void getboom() {
srand((unsigned int)time(NULL));
for (int i = 1,x,y; i <= total_boom; i++) {
x = rand() % n + 1; y = rand() % n + 1;
if (!boob[x][y]) boob[x][y] = true;
else {
i--;
}
}
}
bool isBoob(int x,int y) {
if (boob[x][y]) {
if (first) {
bool see = false;
for(int i=1;i<=n;i++){
for (int j = 1; j <= n; j++) {
if (!boob[i][j]) {
boob[i][j] = true;
see = true;
break;
}
}
if (see) {
break;
}
}
boob[x][y] = false;
first = false;
DFS(x, y);
return false;
}
return true;
}
else {
DFS(x, y);
return false;
}
}
void DFS(int x, int y) {
//q.push(MK(x, y));
//pair<int, int> now;
int boob_count = 0;
for (int i = 0, nx, ny; i < 8; i++) {
nx = x + dx[i]; ny = y + dy[i];
if (nx>0&&nx<=n&&ny>0&&ny<=n&&boob[nx][ny]) {
boob_count++;
}
}
if (boob_count != 0) {
map[x][y] = boob_count + '0';
visit[x][y] = true;
ans++;
}
else {
map[x][y] = ' ';
visit[x][y] = true;
ans++;
for (int i = 0, nx, ny; i < 8; i++) {
nx = x + dx[i]; ny = y + dy[i];
if (nx > 0 && nx <= n && ny > 0 && ny <= n&&!visit[nx][ny]) {
DFS(nx, ny);
}
}
}
}
int minput() {
char b[50], c[50];
char in[1000];
int num[5];
char j;
int x = 0, y = 0, p = 0;
char tmp;
while (true) {
scanf("%c", &tmp);
if (tmp == '\n') {
break;
}
else {
in[p++] = tmp;
}
}
int k = 0, count = 0;
while (k < p) {
if (in[k] == ' ') {
num[count++] = k;
}
k++;
if (count > 2) {
break;
}
}
if (count != 2) {
return 5;
}
else {
if (num[0] != 1) {
return 5;
}
j = in[0];
int p1 = 0;
for (; p1 + num[0] + 1 < num[1]; p1++) {
b[p1] = in[p1 + num[0] + 1];
}
int p2 = 0;
for (; p2 + num[1] + 1 < p; p2++) {
c[p2] = in[p2 + num[1] + 1];
}
int base = 1;
for (int i = p1 - 1; i >= 0; i--) {
x += (b[i]-'0') * base;
base *= 10;
}
base = 1;
for (int i = p2 - 1; i >= 0; i--) {
y += (c[i]-'0') * base;
base *= 10;
}
}
if (j >= 'a' && j <= 'c') {
if (x >= 1 && x <= n && y >= 1 && y <= n) {
if (j == 'a') {
if (isBoob(x, y)) {
for (int i = 1; i <= n; i++) {
for (int k= 1; k <= n; k++) {
if (boob[i][k]) {
map[i][k] = '@';
}
else {
map[i][k] = ' ';
}
}
}
return 1;
}
else {
return 2;
}
}
else if (j == 'b') {
if (map[x][y] == '*') {
map[x][y] = '!';
now_boom--;
return 2;
}
else {
return 3;
}
}
else if (j == 'c') {
if (map[x][y] == '!') {
map[x][y] = '*';
now_boom++;
return 2;
}
else {
return 4;
}
}
}
else {
return 5;
}
}
else {
return 5;
}
}
void begin() {
initial();
getboom();
show();
int view;
while (ans != n * n - total_boom) {
view=minput();
if (view==1) {
break;
}
clear();
show();
if (view == 3) {
cout << "标记失败,请确认坐标输入无误" << endl;
}
else if (view == 4) {
cout << "取消标记失败,请确认坐标输入无误" << endl;
}
else if (view == 5) {
cout << "非法输入,请确认输入格式正确" << endl;
}
first = false;
}
if (ans == n * n - total_boom) {
cout << "你赢得了游戏,"<<"输入1再来一把,输入0结束游戏"<<endl;
}
else {
show();
cout << "很遗憾,你输了" << " " << "别放弃,输入1重新开始,输入0结束游戏" << endl;
}
}
int main() {
menu();
return 0;
}