跳到正文

个人记录

Microsoft Games

wujiawei36约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟

版本

  1. v1当前

扫雷

C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>

#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#define pii pair<int,int>

using namespace std;

const char BOMB='*',FLAG='P',QUESTION='?',HIDE='#';
const int N=10,M=10,BOMB_NUM=10;
const int SHOW_MAX=10;

void COLOR_PRINT(const string &s, int front_color,int back_color)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | back_color*16 |FOREGROUND_INTENSITY | front_color);
	cout<<s;
//	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
	SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | 15*16 |FOREGROUND_INTENSITY | 0);
//	system("color F0");
}
void COLOR_PRINT(const char &c, int front_color,int back_color)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | back_color*16 |FOREGROUND_INTENSITY | front_color);
	cout<<c;
//	SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
	SetConsoleTextAttribute(handle, BACKGROUND_INTENSITY | 15*16 |FOREGROUND_INTENSITY | 0);
//	system("color F0");
}
/*
0 = 黑色 8 = 灰色
1 = 蓝色 9 = 淡蓝色
2 = 绿色 10 = 淡绿色
3 = 浅绿色 11 = 淡浅绿色
4 = 红色 12 = 淡红色
5 = 紫色 13 = 淡紫色
6 = 黄色 14 = 淡黄色
7 = 白色 15 = 亮白色
*/
void print_num(int num)
{
	if(num==0)cout<<" ";
	if(num==1)COLOR_PRINT("1",1,15);
	if(num==2)COLOR_PRINT("2",2,15);
	if(num==3)COLOR_PRINT("3",12,15);
	if(num==4)COLOR_PRINT("4",5,15);
	if(num==5)COLOR_PRINT("5",4,15);
	if(num==6)COLOR_PRINT("6",10,15);
	if(num==7)COLOR_PRINT("7",0,15);
	if(num==8)COLOR_PRINT("8",8,15);
	if(num==9)assert(0);
}
char Map[N+5][M+5];
char Show[N+5][M+5];
mt19937 rnd;
int get_rand(int l,int r)
{
	return (rnd()%(r-l+1)+r-l+1)%(r-l+1)+l;
}
int gox[]={-1,0,0,1,-1,-1,1,1};
int goy[]={0,-1,1,0,-1,1,-1,1};
void update_num(int x,int y)
{
	Map[x][y]='0';
	for(int k=0;k<8;k++)
	{
		int xx=x+gox[k],yy=y+goy[k];
		if(xx>=1&&xx<=N&&yy>=1&&yy<=M&&Map[xx][yy]==BOMB)Map[x][y]++;
	}
}
void put_bomb(int x,int y)
{
	Map[x][y]=BOMB;
	for(int k=0;k<8;k++)
	{
		int xx=x+gox[k],yy=y+goy[k];
		if(xx>=1&&xx<=N&&yy>=1&&yy<=M&&Map[xx][yy]!=BOMB)update_num(xx,yy);
	}
}
int nowx=1,nowy=1;
void show_map(int l=0)
{
	cout<<"+";
	for(int j=1;j<=M;j++)
	{
		if(nowy==j)cout<<"|";
		else cout<<"-";
	}
	cout<<'\n';
	for(int i=1;i<=N;i++,cout<<'\n')
	{
		if(i==l)COLOR_PRINT('-',13,15);
		else
		{
			if(nowx==i)cout<<">";
			else cout<<"|";
		}
		for(int j=1;j<=M;j++)
		{
			if(i==nowx&&j==nowy)COLOR_PRINT(Show[i][j],15,0);
			else if(isdigit(Show[i][j]))print_num(Show[i][j]-'0');
			else if(Show[i][j]==FLAG)COLOR_PRINT(Show[i][j],13,15);
			else cout<<Show[i][j];
		}
	}
}
void init()
{
	assert(N*M>BOMB_NUM);
	system("color F0");
//	system("chcp 65001");
	system("cls");
	srand((unsigned)time(NULL));
	rnd=mt19937(rand()*114514+1919810);
	for(int i=1;i<=N;i++)
		for(int j=1;j<=M;j++)
			Map[i][j]=' ',Show[i][j]=HIDE;
	cout<<"地图初始化中,请稍候...\n";
	for(int _=1;_<=BOMB_NUM;_++)
	{
		int x=0,y=0;
		do
		{
			x=get_rand(1,N),y=get_rand(1,M);
		}while(Map[x][y]==BOMB);
		put_bomb(x,y);
	}
	cout<<"初始化完成!\n";
	Sleep(1000);
	system("cls");
}
void show_block(int x,int y)
{
	queue<pii> q;
	int cnt=1;
	q.push({x,y});
	Show[x][y]=Map[x][y];
	while(q.size())
	{
		int x=q.front().first,y=q.front().second;
		if(Map[x][y]!=' '&&cnt>=SHOW_MAX)
		{
			q.pop();
			continue;
		}
		for(int k=0;k<4;k++)
		{
			int xx=x+gox[k],yy=y+goy[k];
			if(xx>=1&&xx<=N&&yy>=1&&yy<=M&&Map[xx][yy]!=BOMB&&Show[xx][yy]==HIDE)
			{
				cnt++;
				Show[xx][yy]=Map[xx][yy];
				q.push({xx,yy});
			}
		}
		q.pop();
	}
//system("pause");
//cerr<<x<<" "<<y<<'\n';
}
void open_block(int x,int y)
{
	if(Show[x][y]!=HIDE)return;
	if(Show[x][y]==FLAG)return;
	if(Map[x][y]==BOMB)
	{
		for(int i=N;i>=1;i--)
		{
			system("cls");
			for(int j=1;j<=M;j++)Show[i][j]=Map[i][j];
			cout<<"这一格是地雷!!!\n";
			show_map(i);
			Sleep(500);
		}
		cout<<"按下ESC键退出...\n";
		while(!KEY_DOWN(VK_ESCAPE));
		exit(0);
	}
	show_block(x,y);
}
void set_flag(int x,int y)
{
	if(Show[x][y]!=HIDE&&Show[x][y]!=FLAG)return;
	if(Show[x][y]==FLAG)Show[x][y]=HIDE;
	else Show[x][y]=FLAG;
}
void check_win()
{
	int cnt=0;
	for(int i=1;i<=N;i++)
		for(int j=1;j<=M;j++)
			if(Show[i][j]!=HIDE&&Show[i][j]!=FLAG)cnt++;
	if(cnt+BOMB_NUM==N*M)
	{
		cout<<"恭喜,您赢了!!!\n";
		cout<<"按下ESC键退出...\n";
		while(!KEY_DOWN(VK_ESCAPE));
		exit(0);
	}
}
void work()
{
	while(1)
	{
		cout<<"按下方向键移动光标,ESC退出,ENTER翻开该格,SPACE将该格设置/取消设置为标记\n";
		show_map();
		check_win();
		while(!(KEY_DOWN(VK_ESCAPE)||KEY_DOWN(VK_RETURN)||KEY_DOWN(VK_LEFT)||KEY_DOWN(VK_RIGHT)||KEY_DOWN(VK_UP)||KEY_DOWN(VK_DOWN)||KEY_DOWN(VK_SPACE)));
		if(KEY_DOWN(VK_UP))
		{
			if(nowx>1)nowx--;
			while(KEY_DOWN(VK_UP));
		}
		if(KEY_DOWN(VK_DOWN))
		{
			if(nowx<N)nowx++;
			while(KEY_DOWN(VK_DOWN));
		}
		if(KEY_DOWN(VK_LEFT))
		{
			if(nowy>1)nowy--;
			while(KEY_DOWN(VK_LEFT));
		}
		if(KEY_DOWN(VK_RIGHT))
		{
			if(nowy<M)nowy++;
			while(KEY_DOWN(VK_RIGHT));
		}
		if(KEY_DOWN(VK_RIGHT))
		{
			if(nowy<M)nowy++;
			while(KEY_DOWN(VK_RIGHT));
		}
		if(KEY_DOWN(VK_RETURN))
		{
			open_block(nowx,nowy);
			while(KEY_DOWN(VK_RETURN));
		}
		if(KEY_DOWN(VK_SPACE))
		{
			set_flag(nowx,nowy);
			while(KEY_DOWN(VK_SPACE));
		}
		if(KEY_DOWN(VK_ESCAPE))
		{
			cout<<"若确认退出请松开并再次按下ESC键,否则请按BACKSPACE键\n";
			while(KEY_DOWN(VK_ESCAPE));
			while(!(KEY_DOWN(VK_ESCAPE)||KEY_DOWN(VK_BACK)));
			if(KEY_DOWN(VK_ESCAPE))
			{
				cout<<"退出";
				for(int i=1;i<=6;i++)
				{
					Sleep(500);
					cout<<".";
				}
				exit(0);
			}
		}
		system("cls");
		//
//		Sleep(500);
//		cout<<nowx<<" "<<nowy<<'\n';
	}
}
signed main()
{
	init();
	work();
	return 0;
}
原文