跳到正文

算法·理论

分享一个刚正式学C++一个月的蒟蒻编的简易扫雷游戏

cjh115约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年1月11日
收录
2026年10月18日
更新
2026年11月16日
篇幅
约 6 分钟

版本

  1. v1当前

首先分析一下我下面代码的效果:

  • 1.输入地图
  • 2.隐匿地图
  • 3.将无雷空格填充数字 此处要用到坐标系知识
  • 4.玩家通过输入翻开格子
  • 5.当翻开雷后,游戏终止
  • 6.计分

话不多说,直接上代码

C++
#include<iostream>
#include<string>
int main(){
	char a[100][100];
	bool k[100][100]={false};//判定a[i][j]是否出现
	bool flag=true;//雷的判定
	int p;//玩家数量
	std::cout<<"请输入玩家数量\n";
	std::cin>>p;
	int player[p+1];
	for(int i=1;i<p+1;++i)
		player[i]=0;
	int score=0;
	int maxn=0;
	std::string b[100][100];//a[i][j]变成[]时的哑元
	int n,m;//地图大小
	int x,y;//输入坐标
	std::cin>>n>>m;
	char c;
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j)
			std::cin>>a[i][j];
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j)
			b[i][j]="[]";
	for(int i=0;i<n;++i){
		for(int j=0;j<m;++j){
			if(a[i][j]=='*')
				a[i][j]='*';
			else{
				int cnt=0;
				int dx[8]={-1,-1,-1,0,0,1,1,1};
				int dy[8]={-1,0,1,1,-1,-1,0,1};
				for(int k=0;k<8;++k){
					int nx=i+dx[k];
					int ny=j+dy[k];
					if(0<=nx and nx<n and 0<=ny and ny<m)
						if(a[nx][ny]=='*')
							cnt++;
					a[i][j]=cnt+'0';
				}
			}
		}
	}
	if(p==1){//单人
		while(std::cin>>c){
		    if(c=='b'){
			    std::cout<<"Game start!\n";
				for(int i=0;i<n;++i){
					for(int j=0;j<m;++j){
						std::cout<<b[i][j];
					}
					std::cout<<"\n";
				}
			}
			else if(c=='a'){
				std::cin>>x>>y;
				for(int i=0;i<n;++i){
					for(int j=0;j<m;++j){
						if(i==x&&j==y){
							if(a[x][y]=='*'){
					    	    std::cout<<' '<<a[x][y];
								flag=false;
							}
							else{
								std::cout<<a[x][y]<<" ";
							}
							k[i][j]=true;
						}
						else if(k[i][j])
							std::cout<<a[i][j]<<' ';
						else
							std::cout<<b[i][j];
					}
					std::cout<<"\n";
				}
			}
			if(not flag){
				std::cout<<"Boon!"<<' ';
				std::cout<<"Your score is "<<score<<'.'<<"\n";
		    	break;
			}
			else
				score++;
		}
	}
	else if(p>=2){//多人
		int f=0;//控制玩家的变化
		while(std::cin>>c){
			if(c=='b'){
				std::cout<<"Game start!\n";
				for(int i=0;i<n;++i){
					for(int j=0;j<m;++j){
						std::cout<<b[i][j];
					}
					std::cout<<"\n";
				}
			}
			else if(c=='a'){
				std::cin>>x>>y;
				for(int i=0;i<n;++i){
					for(int j=0;j<m;++j){
						if(i==x&&j==y){
							if(a[x][y]=='*'){
								std::cout<<' '<<a[x][y];
								flag=false;
							}
							else{
								std::cout<<a[x][y]<<" ";
							}
							k[i][j]=true;
							player[(f+1)%p+1]+=a[x][y]-'0';
						}
						else if(k[i][j])
							std::cout<<a[i][j]<<' ';
						else
							std::cout<<b[i][j];
					}
					std::cout<<"\n"; 
				}
			}
			if(not flag){
				std::cout<<"Boon!"<<"\n";
				break;
				player[(f+1)%p+1]/=2;
			}
			else
				f++;
		}
	}
	for(int i=1;i<p+1;++i)
		std::cout<<"Player"<<i<<' ';
	std::cout<<"\n";
	for(int i=1;i<p+1;++i)
		std::cout<<"   "<<player[i]<<"   ";
	std::cout<<"\n";
	for(int i=1;i<p+1;++i)
		if(player[i]>maxn)
			maxn=player[i];
	for(int i=1;i<p+1;++i)
		if(player[i]==maxn)
			std::cout<<"The winner is player"<<i<<'.';
}

作为一名蒟蒻,写出这样的代码已着实不易了

毕竟我才六年级

Tips:6有些问题呢

最后给出例题啦

网址: https://iai.sh.cn/problem/32

希望各路大神们多多指导啦(^▽^)

原文