跳到正文

个人记录

扫雷游戏代码

I_do_Cpp约 15 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月13日
篇幅
约 15 分钟

版本

  1. v1当前

看在作者如此辛苦的份上,麻烦点个赞,顺便帮作者找点BUG,谢谢!

C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#define MAX_INDEX 10
#define MAX_SIZE 999
#define ll long long
#define enter char(13) //即直接按下回车
#define up 72
#define down 80
#define left 75
#define right 77
using namespace std;
ll line=1,mx,my;
char game_list[MAX_SIZE][MAX_SIZE];//每个位置的东西
int game_list_find[MAX_SIZE][MAX_SIZE]={0};//每个位置的状态
int choice_front[MAX_SIZE]={0};
int directions[8][2]=
{
	{-1,1},
	{1,-1},
	{1,1},
	{-1,-1},
	{0,1},
	{1,0},
	{-1,0},
	{0,-1}
};
pair<ll,ll> nb=make_pair(0,0);
long long x=0,y=0;//x为长,y为宽

bool OUT_OF_GAME(int x_,int y_,int mode=1)
{
	if(mode==1)
	{
		return (x_>0 && x_<=x && y_>0 && y_<=y)&&(game_list[x_][y_]=='B');
	}
	if(mode==2)
	{
		return (x_>0 && x_<=x && y_>0 && y_<=y);
	}
}
int CHAR_TO_INT(char c)
{
	return (c-'0');
}
char INT_TO_CHAR(int i)
{
	return (i+'0');
}
void COLOR_PRINT(const char* s, int color)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY|color);
	printf("%s",s);
	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY|7);
}
void ERROR_PRINT()
{
	COLOR_PRINT("输入无效",4);
	Sleep(500);
}
bool DRAW_MAIN(long long line_)
{
	bool rn=true;
	if(line_>MAX_INDEX+2)
	{
		cout<<"输入无效";
		Sleep(500);
		line_=line;
		rn=false;
	}
	else if(line_<1)
	{
		cout<<"输入无效";
		Sleep(500);
		line_=line;
		rn=false;
	}
	system("cls");
	COLOR_PRINT("扫雷游戏\n",14);
	for(int i=1;i<=MAX_INDEX;i++)
	{
		if(i==line_)
		{

			COLOR_PRINT("→",14);
		}
		else
		{
			COLOR_PRINT("  ",3);
		}
		if(choice_front[i]==0)COLOR_PRINT("●",3);
		else COLOR_PRINT("√",2);
		stringstream s;
		s<<i;
		cout<<"关卡"<<s.str()<<endl;
	}
	if(line_==MAX_INDEX+1)
	{
		COLOR_PRINT("→",14);
	}
	else
	{
		COLOR_PRINT("  ",14);
	}
	COLOR_PRINT("■",1);
	cout<<"制作组\n";
	if(line_==MAX_INDEX+2)
	{
		COLOR_PRINT("→",14);
	}
	else
	{
		COLOR_PRINT("  ",14);
	}
	COLOR_PRINT("■",1);
	cout<<"退出\n";
	cout<<"上按w,下按s(也可以使用方向键),进入按Enter\n";
	return rn;
}

void DRAW_CHAR(int i,int j)
{
	if(i==mx && j==my)
	{
		COLOR_PRINT("我",20);
		return;
	}
	else if(game_list_find[i][j]==false)
	{
		COLOR_PRINT("? ",11);
		return;
	}
	else if(game_list_find[i][j]==2)
	{
		COLOR_PRINT("危",30);
		return;
	}
	char c=game_list[i][j];
	if(c>='0' && c<='8')
	{
		switch(c)
		{
			case '0':COLOR_PRINT("0 ",0);break;
			case '1':COLOR_PRINT("1 ",1);break;
			case '2':COLOR_PRINT("2 ",2);break;
			case '3':COLOR_PRINT("3 ",28);break;
			case '4':COLOR_PRINT("4 ",4);break;
			case '5':COLOR_PRINT("5 ",5);break;
			case '6':COLOR_PRINT("6 ",6);break;
			case '7':COLOR_PRINT("7 ",7);break;
			case '8':COLOR_PRINT("8 ",8);break;
		}
		return;
	}
	else if(c=='B')
	{
		COLOR_PRINT("炸",68);
		return;
	}
}
void DRAW_LEVEL()
{
	system("cls");
	COLOR_PRINT("主页按m,上按w,下按s,左按a,右按d(也可以使用方向键),翻面按Enter,\n标记按f,在按过f的格子再按一次还原。\n",2);
	cout<<"一共有"<<x*y-nb.first<<"颗地雷。\n";
	for(int i=1;i<=x;i++)
	{
		for(int j=1;j<=y;j++)
		{
			DRAW_CHAR(i,j);
			//cout<<game_list[i][j];
		}
		cout<<endl;
	}
}
void UP()
{
	if(OUT_OF_GAME(mx-1,my,2))
	{
		mx--;
	}
	else
	{
		ERROR_PRINT();
	}
}
void DOWN()
{
	if(OUT_OF_GAME(mx+1,my,2))
	{
		mx++;
	}
	else
	{
		ERROR_PRINT();
	}
}
void LEFT()
{
	if(OUT_OF_GAME(mx,my-1,2))
	{
		my--;
	}
	else
	{
		ERROR_PRINT();
	}

}
void RIGHT()
{
	if(OUT_OF_GAME(mx,my+1,2))
	{
		my++;
	}
	else
	{
		ERROR_PRINT();
	}
}
void DFS(int x_,int y_)
{
	if(game_list[x_][y_]!='0')
	{
		return;
	}
	for(int i=0;i<8;i++)
	{
		int x__=x_+directions[i][0];
		int y__=y_+directions[i][1];
		if(game_list_find[x__][y__]==true)
		{
			continue;
		}
		if(OUT_OF_GAME(x__,y__,2))
		{
			nb.second++;
			game_list_find[x__][y__]=true;
			DFS(x__,y__);
		}
	}
	return;
}
void init()
{
	for(int i=1;i<=x;i++)
	{
		for(int j=1;j<=y;j++)
		{
			game_list[i][j]=0;
			game_list_find[i][j]=false;
		}
	}
}
int main()
{
	srand(time(0));
	for(int i=0;i<=MAX_INDEX;i++)
	{
		choice_front[i]=0;
	}
	DRAW_MAIN(1);
	while(1)
	{
		char str=' ';
		int num;
		str=getch();
		if(str=='s')
		{
			if(DRAW_MAIN(line+1))line++;
		}
		else if(str=='w')
		{
			if(DRAW_MAIN(line-1))line--;
		}
		else if(str==enter)//即直接按下回车
		{
			if(line==MAX_INDEX+1)
			{
				system("cls");
				COLOR_PRINT("代码编写、地图制作",3);
				COLOR_PRINT("I_do_Cpp(洛谷)、爱做题的小轩(hydro)\n",14);
				COLOR_PRINT("感谢",3);
				COLOR_PRINT("A86562U(洛谷)",14);
				COLOR_PRINT("和",3);
				COLOR_PRINT("MCmuyou(洛谷)",14);
				COLOR_PRINT("提供了COLOR_PRINT()函数和创作思路\n\n版本号:v1.0.1测试版\n",3);
				system("pause");
				DRAW_MAIN(line);
			}
			else if(line==MAX_INDEX+2)
			{
				system("cls");
				COLOR_PRINT("游玩结束,欢迎下次再来!\n",3);
				return 0;
			}
			else
			{
				long long xindex=1,yindex=1;
				x=4,y=5;
				for(int i=1;i<=line;i++)
				{
					if(i%2==1)x++;
					else y++;
				}
				init();
				nb=make_pair(0,0);
				init();
				do
				{
					nb.first=0;
					nb.second=0;
					for(int i=1;i<=x;i++)
					{
						for(int j=1;j<=y;j++)
						{
							if(rand()%2)
							{
								game_list[i][j]='0';
								nb.first++;
							}
							else
							{
								game_list[i][j]='B';
							}
						}
					}
				}
				while((x*y-nb.first)>=(x*y/4));
				int ans=0;
				for(int i=1;i<=x;i++)
				{
					for(int j=1;j<=y;j++)
					{
						ans=0;
						if(game_list[i][j]!='B')
						{
							for(int k=0;k<8;k++)
							{
								int x__=i+directions[k][0],y__=j+directions[k][1];

								if(OUT_OF_GAME(x__,y__,1))
								{
									ans++;
								}
							}
							game_list[i][j]=INT_TO_CHAR(ans);
						}
					}
					ans=0;
				}
				mx=x/2;
				my=y/2;
				DRAW_LEVEL();
				while(1)
				{
					if(nb.second==nb.first)
					{
						COLOR_PRINT("你赢啦!",2);
						choice_front[line]=1;
						system("pause");
						break;
					}
					char mode=getch();
					if(mode=='m')
					{
						break;
					}
					else if(mode==enter)
					{
						if(game_list_find[mx][my]!=1)nb.second++;
						game_list_find[mx][my]=true;
						if(game_list[mx][my]=='B')
						{
							system("cls");
							for(int i=1;i<=x;i++)
							{
								for(int j=1;j<=y;j++)
								{
									if(game_list[i][j]=='B')
									{
										COLOR_PRINT("炸",68);
									}
									else
									{
										DRAW_CHAR(i,j);
									}
								}
								cout<<"\n";
							}
							COLOR_PRINT("你翻开了炸弹!\n",4);
							system("pause");
							break;
						}
						DFS(mx,my);
					}
					else if(mode=='w')
					{
						UP();
					}
					else if(mode=='s')
					{
						DOWN();
					}
					else if(mode=='a')
					{
						LEFT();
					}
					else if(mode=='d')
					{
						RIGHT();
					}
					else if(mode=='f')
					{
						if(game_list_find[mx][my]==1)
						{
							COLOR_PRINT("已经翻开\n",60);
							Sleep(500);
						}
						else
						{
							game_list_find[mx][my]=2-game_list_find[mx][my];
						}
					}
					else if(mode)
					{
						mode=getch();
						switch(mode)
						{
							case left:LEFT();break;
							case right:RIGHT();break;
							case up:UP();break;
							case down:DOWN();break;
							default:ERROR_PRINT();break;
						}
					}
					else
					{
						ERROR_PRINT();
					}
					DRAW_LEVEL();
				}
				system("cls");
				DRAW_MAIN(line);
			}
		}
		else if(str)
		{
			str=getch();
			switch(str)
			{
				case up:if(DRAW_MAIN(line-1))line--;break;
				case down:if(DRAW_MAIN(line+1))line++;break;
				default:DRAW_MAIN(MAX_INDEX+50);break;
			}
		}
		else
		{
			DRAW_MAIN(MAX_INDEX+50);
		}
	}
	return 0;
}
原文