跳到正文

剪贴板

剪贴板 wgjmxtiv

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
#include <windows.h>
#define int long long
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
POINT p;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HWND h=GetForegroundWindow();
CONSOLE_FONT_INFO consoleCurrentFont;
void HideTheCursor() {
	CONSOLE_CURSOR_INFO cciCursor;
	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

	if(GetConsoleCursorInfo(hStdOut, &cciCursor)) {
		cciCursor.bVisible = FALSE;
		SetConsoleCursorInfo(hStdOut, &cciCursor);
	}
}
bool T,F;
char mp[102][102];
int a[102][102];
int n, m, lei, t,ku;
struct father {
	int x,y;
} fa[102][102];
void init() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			fa[i][j].x=i;
			fa[i][j].y=j;
			if (a[i][j] == -1) continue;
			a[i][j]=0;
			if (a[i - 1][j - 1] == -1) a[i][j]++;
			if (a[i - 1][j + 1] == -1) a[i][j]++;
			if (a[i + 1][j - 1] == -1) a[i][j]++;
			if (a[i + 1][j + 1] == -1) a[i][j]++;
			if (a[i - 1][j] == -1) a[i][j]++;
			if (a[i][j - 1] == -1) a[i][j]++;
			if (a[i + 1][j] == -1) a[i][j]++;
			if (a[i][j + 1] == -1) a[i][j]++;
			// if(a[i][j]==0){
			// if(a[i-1][j]==0)fa[i][j]=fa[i-1][j];
			// else if(a[i+1][j]==0)fa[i][j]=fa[i+1][j];
			// else if(a[i][j-1]==0)fa[i][j]=fa[i][j-1];
			// else if(a[i][j+1]==0)fa[i][j]=fa[i][j+1];
			// else fa[i][j].x=i,fa[i][j].y=j;
			// }
			if(a[i][j]==0) {
				if(a[i-1][j]==0)fa[i][j].x=fa[i-1][j].x,fa[i][j].y=fa[i-1][j].y;
				else if(a[i][j-1]==0)fa[i][j].x=fa[i][j-1].x,fa[i][j].y=fa[i][j-1].y;
				else if(a[i+1][j]==0)fa[i][j].x=fa[i+1][j].x,fa[i][j].y=fa[i+1][j].y;
				else if(a[i][j+1]==0)fa[i][j].x=fa[i][j+1].x,fa[i][j].y=fa[i][j+1].y;
			}
		}
	}
}
void run() {
	system("cls");
	srand(time(NULL));
	printf("输入雷区的长、宽和雷数\n");
	cin >> n >> m >> lei;
	for(int i=0; i<=n+1; i++) {
		for(int j=0; j<=m+1; j++) {
			a[i][j]=1;
			mp[i][j]='*';
//			printf("%2d",a[i][j]);
		}
//		printf("\n");
	}
	system("cls");
	for (int i = 1; i <= lei; i++) {
		int x = rand() % n, y = rand() % m;
		a[x + 1][y + 1] = -1;
//		printf("(%d,%d)\n",x+1,y+1);
	}
	init();
	t=time(NULL);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			mp[i][j]='*';
			cout << "*";
		}
		cout << '\n';
	}
	printf("剩余雷:%d\n",lei);
	printf("┌┄┄┄┄┐\n");
	printf("┆  退出  ┆\n");
	printf("└┄┄┄┄┘\n");
	printf("┌┄┄┄┄┐\n");
	printf("┆ 新游戏 ┆\n");
	printf("└┄┄┄┄┘\n");
	printf("当前模式:打开\n");
	ku=n*m;
	int o=0;
	while(ku >lei) {
		int x,y,i;
		ku=0;
		int p=o;
		while(1) {
			if(KEY_DOWN(VK_LBUTTON)) {
				POINT p;
				GetCursorPos(&p);
				ScreenToClient(h,&p);
				GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont);
				x=p.x/=consoleCurrentFont.dwFontSize.X;
				y=p.y/=consoleCurrentFont.dwFontSize.Y;
				i=o;
				if(x<=10&&y<=n+3&&x>=0&&y>=n+1) {
					F=0;
					Sleep(50);
					return;
				}
				if(x<=10&&y<=n+6&&x>=0&&y>=n+4) {
					T=true;
					Sleep(50);
					return;
				}
				break;
			}
			if(KEY_DOWN(VK_CONTROL)) {
				o=1-o;
				Sleep(50);
				break;
			}
		}
		Sleep(100);
		if(o!=p) {
			system("cls");
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= m; j++) {
					if(mp[i][j]=='*')ku++;
					cout<<mp[i][j];
				}
				cout << '\n';
			}
			printf("剩余雷:%d\n",lei);
			printf("┌┄┄┄┄┐\n");
			printf("┆  退出  ┆\n");
			printf("└┄┄┄┄┘\n");
			printf("┌┄┄┄┄┐\n");
			printf("┆ 新游戏 ┆\n");
			printf("└┄┄┄┄┘\n");
			printf("当前模式:");
			if(o==1)printf("插旗\n");
			else printf("打开\n");
			Sleep(100);
			continue;
		}
		x++;
		y++;
		swap(x,y);
		if(i==1) {
			system("cls");
			if(mp[x][y]=='*')mp[x][y]='F',lei--;
			else if(mp[x][y]=='F')mp[x][y]='*',lei++;
		} else if(a[x][y]==-1) {
			system("cls");
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= m; j++) {
					if(a[i][j]==0)
						cout << " ";
					else if(a[i][j]==-1)cout<<"@";
					else cout<<a[i][j];
				}
				cout << '\n';
			}
			cout<<"您死了!\n";
			printf("按下任意键返回主菜单");
			Sleep(50);
			getchar();
			F=0;
			getchar();
			return;
		} else {
			system("cls");
			if(a[x][y]==0) {
				father f=fa[x][y];
				for(int i=1; i<=n; i++) {
					for(int j=1; j<=m; j++) {
						if(f.x==fa[i][j].x&&f.y==fa[i][j].y&&mp[i][j]!='F') {
							mp[i][j]=' ';
							mp[i+1][j]=!a[i+1][j]?' ':a[i+1][j]+48;
							mp[i-1][j]=!a[i-1][j]?' ':a[i-1][j]+48;
							mp[i][j-1]=!a[i][j-1]?' ':a[i][j-1]+48;
							mp[i][j+1]=!a[i][j+1]?' ':a[i][j+1]+48;
						}
					}
				}
			} else {
				mp[x][y]=a[x][y]+48;
			}
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				if(mp[i][j]=='*')ku++;
				cout<<mp[i][j];
			}
			cout << '\n';
		}
		printf("剩余雷:%d\n",lei);
		printf("┌┄┄┄┄┐\n");
		printf("┆  退出  ┆\n");
		printf("└┄┄┄┄┘\n");
		printf("┌┄┄┄┄┐\n");
		printf("┆ 新游戏 ┆\n");
		printf("└┄┄┄┄┘\n");
		if(o==1)printf("当前模式:插旗\n");
		else printf("当前模式:打开\n");
//		printf("%d/%d\n",lei,ku);
	}
	system("cls");
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if(a[i][j]==-1)cout<<"@";
			else if(a[i][j]!=0)cout<<a[i][j];
			else cout<<mp[i][j];
		}
		cout << '\n';
	}
	cout<<"您赢了!\n";
	printf("您的用时%d秒\n",time(NULL)-t);
	F=0;
	printf("按下任意键返回主菜单");
	Sleep(50);
	getchar();
	getchar();
}
signed main() {
	HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
	DWORD mode;
	GetConsoleMode(hStdin, &mode);
	mode &= ~ENABLE_QUICK_EDIT_MODE;
	SetConsoleMode(hStdin, mode);
	while(1) {
		system("cls");
		F=1;
		printf("欢迎游玩扫雷游戏\n作者:陈宇航&竹昱阳\n");
		int x=0;
		printf("┌┄┄┄┄┐\n");
		printf("┆开始游戏┆\n");
		printf("└┄┄┄┄┘\n");
		printf("┌┄┄┄┄┐\n");
		printf("┆游戏说明┆\n");
		printf("└┄┄┄┄┘\n");
		printf("┌┄┄┄┄┐\n");
		printf("┆  退出  ┆\n");
		printf("└┄┄┄┄┘\n");
		T=0;
		while(1) {
			if(KEY_DOWN(VK_LBUTTON)) {
				POINT p;
				GetCursorPos(&p);
				ScreenToClient(h,&p);
				GetCurrentConsoleFont(hOutput, FALSE, &consoleCurrentFont);
				int x=p.x/=consoleCurrentFont.dwFontSize.X;
				int y=p.y/=consoleCurrentFont.dwFontSize.Y;
				if(x<=10&&y<=4&&x>=0&&y>=2) {
					system("cls");
					run();
					while(T&&F)run();
					break;
				} else if(x<=10&&y<=7&&x>=0&&y>=5) {
					system("cls");
					printf("按下ctrl键切换打开和插旗模式(游戏开始时默认打开)\n旗子:F\n雷:@\n");
					printf("按下回车键(Enter)返回菜单\n");
					getchar();
					break;

				} else if(x<=10&&y<=10&&x>=0&&y>=8) {
					exit(0);
				}
			}
			Sleep(20);
		}
	}
}