跳到正文

算法·理论

一个扫雷游戏,c++自创

SDwangyixiao约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年1月29日
收录
2026年5月23日
更新
2026年11月14日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

勿喷!!!!!

会前端的大佬不知可否做个前端放评论区

自取,欢迎大佬指点 求赞

C++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
//定义变量
	int q[11][11]={},sx,sy,shu=1;
	int p[11][11]={},ls=0,x,y;
	bool a=0;
//暗棋盘
void dayin_yin(){
	cout<<"      | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10";
	cout<<endl<<"      ________________________________"<<endl;
	for(int i=1;i<11;i++){
		cout<<setw(2)<<i;
		cout<<"|   |";
		for(int j=1;j<11;j++){
			if(q[i][j]==0){
				cout<<setw(2)<<"*";
			}else if(q[i][j]==-1){
				cout<<setw(2)<<"X";
			}else{
				cout<<setw(2)<<p[i][j];
			}
			cout<<"|";
		}
		cout<<endl;
		cout<<"---   -------------------------------";
		cout<<endl;
	}
}
//明棋盘
void dayin_ming(){
	cout<<"      | 1| 2| 3| 4| 5| 6| 7| 8| 9| 10";
	cout<<endl<<"      ________________________________"<<endl;
	for(int i=1;i<11;i++){
		cout<<setw(2)<<i;
		cout<<"|   |";
		for(int j=1;j<11;j++){
			cout<<setw(2)<<p[i][j];
			cout<<"|";
		}
		cout<<endl;
		cout<<"---   -------------------------------";
		cout<<endl;
	}
}

int main(){
	srand(time(0));
	//随机生成雷
	while(ls<10){
		x=rand()%10+1;
		y=rand()%10+1;
		if(p[x][y]==0){
			p[x][y]= -1;
			ls++;
		}
	}
	//生成数
	for(int i=1;i<10;i++){
		for(int j=1;j<10;j++){
			if(p[i][j] != -1){
				for(int x=i-1;x<=i+1;x++){
					for(int y=j-1;y<=j+1;y++){
						if(p[x][y]==-1){
							p[i][j]++;
						}
					}
				}
			}
		}
	}
	//打印
	dayin_yin();

	while(1){
		cout<<endl<<"请输入信息:行 列 操作(0或1,0为查看,1为标记):";
		cin>>sx>>sy>>a;
		q[sx][sy]=1;
		if(a){
			q[sx][sy]=-1;
		}
		//打印
		dayin_yin();

		shu++;
		if(p[sx][sy]==-1 && !a){
			cout<<"你输了"<<endl<<endl;
			dayin_ming();
			break;
		}else if(shu>=90){
			cout<<"你赢了"<<endl<<endl;
			dayin_ming();
			break;
		}
	}
	return 0;
}
原文